Service.php
4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace addons\third\library;
use addons\third\model\Third;
use app\common\model\User;
use fast\Random;
use think\Db;
use think\Exception;
/**
* 第三方登录服务类
*
*/
class Service
{
/**
* 第三方登录
* @param string $platform 平台
* @param array $params 参数
* @param array $extend 会员扩展信息
* @param int $keeptime 有效时长
* @return boolean
*/
public static function connect($platform, $params = [], $extend = [], $keeptime = 0)
{
$time = time();
$nickname = $params['nickname'] ?? ($params['userinfo']['nickname'] ?? '');
$avatar = $params['avatar'] ?? ($params['userinfo']['avatar'] ?? '');
$values = [
'platform' => $platform,
'openid' => $params['openid'],
'openname' => $nickname,
'access_token' => $params['access_token'],
'refresh_token' => $params['refresh_token'],
'expires_in' => $params['expires_in'],
'logintime' => $time,
'expiretime' => $time + $params['expires_in'],
];
$values = array_merge($values, $params);
$auth = \app\common\library\Auth::instance();
$auth->keeptime($keeptime);
//是否有自己的
$third = Third::get(['platform' => $platform, 'openid' => $params['openid']], 'user');
if ($third) {
if (!$third->user) {
$third->delete();
} else {
$third->allowField(true)->save($values);
// 写入登录Cookies和Token
return $auth->direct($third->user_id);
}
}
//存在unionid就需要判断是否需要生成新记录
if (isset($params['unionid']) && !empty($params['unionid'])) {
$third = Third::get(['platform' => $platform, 'unionid' => $params['unionid']], 'user');
if ($third) {
if (!$third->user) {
$third->delete();
} else {
// 保存第三方信息
$values['user_id'] = $third->user_id;
$third = Third::create($values, true);
// 写入登录Cookies和Token
return $auth->direct($third->user_id);
}
}
}
if ($auth->id) {
if (!$third) {
$values['user_id'] = $auth->id;
Third::create($values, true);
}
$user = $auth->getUser();
} else {
// 先随机一个用户名,随后再变更为u+数字id
$username = Random::alnum(20);
$password = Random::alnum(6);
$domain = request()->host();
Db::startTrans();
try {
// 默认注册一个会员
$result = $auth->register($username, $password, $username . '@' . $domain, '', $extend);
if (!$result) {
throw new Exception($auth->getError());
}
$user = $auth->getUser();
$fields = ['username' => 'u' . $user->id, 'email' => 'u' . $user->id . '@' . $domain];
if ($nickname) {
$fields['nickname'] = $nickname;
}
if ($avatar) {
$fields['avatar'] = htmlspecialchars(strip_tags($avatar));
}
// 更新会员资料
$user = User::get($user->id);
$user->save($fields);
// 保存第三方信息
$values['user_id'] = $user->id;
Third::create($values, true);
Db::commit();
} catch (\Exception $e) {
Db::rollback();
$auth->logout();
return false;
}
}
// 写入登录Cookies和Token
return $auth->direct($user->id);
}
public static function isBindThird($platform, $openid, $apptype = '', $unionid = '')
{
$conddtions = [
'platform' => $platform,
'openid' => $openid
];
if ($apptype) {
$conddtions['apptype'] = $apptype;
}
$third = Third::get($conddtions, 'user');
//第三方存在
if ($third) {
//用户失效
if (!$third->user) {
$third->delete();
return false;
}
return true;
}
if ($unionid) {
$third = Third::get(['platform' => $platform, 'unionid' => $unionid], 'user');
if ($third) {
//
if (!$third->user) {
$third->delete();
return false;
}
return true;
}
}
return false;
}
}