Third.php
7.1 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
namespace app\index\controller;
use addons\third\library\Application;
use addons\third\library\Service;
use app\common\controller\Frontend;
use app\common\library\Sms;
use fast\Random;
use think\Cookie;
use think\Hook;
use think\Lang;
use think\Session;
/**
* 第三方登录控制器
*/
class Third extends Frontend
{
protected $noNeedLogin = ['prepare'];
protected $noNeedRight = ['*'];
protected $app = null;
protected $options = [];
protected $layout = 'default';
public function _initialize()
{
parent::_initialize();
$config = get_addon_config('third');
$this->app = new Application($config);
$auth = $this->auth;
//监听注册登录事件
Hook::add('user_login_successed', function ($user) use ($auth) {
$expire = input('post.keeplogin') ? 30 * 86400 : 0;
Cookie::set('uid', $user->id, $expire);
Cookie::set('token', $auth->getToken(), $expire);
});
Hook::add('user_register_successed', function ($user) use ($auth) {
Cookie::set('uid', $user->id);
Cookie::set('token', $auth->getToken());
});
}
/**
* 准备绑定
*/
public function prepare()
{
$platform = $this->request->request('platform', '');
if (!in_array($platform, ['wechat', 'weibo', 'qq'])) {
$this->error("未找到指定平台");
}
$url = $this->request->get('url', '/', 'trim,xss_clean');
// 授权成功后的回调
$thirdinfo = Session::get("third-{$platform}");
if (!$thirdinfo) {
$this->error("操作失败,请返回重试");
}
$apptype = Service::getApptype();
$openid = $thirdinfo['openid'] ?? '';
$unionid = $thirdinfo['unionid'] ?? '';
// 如果是登录状态,直接跳到绑定
if ($this->auth->id) {
$isBind = Service::isBindThird($platform, $openid, '', $unionid);
if ($isBind) {
$this->error("已经绑定其它账号,无法进行绑定");
}
$this->redirect(url("index/third/bind") . "?" . http_build_query(['platform' => $platform, 'url' => $url]));
}
if ($this->request->isPost()) {
$mobile = $this->request->post("mobile");
$platform = $this->request->post("platform");
$captcha = $this->request->post("captcha");
$nickname = $thirdinfo['userinfo']['nickname'] ?? '';
if (!Sms::check($mobile, $captcha, 'bind')) {
$this->error(__('验证码不正确'));
}
$user = \app\common\model\User::where('mobile', $mobile)->find();
if ($user) {
$isBind = \addons\third\model\Third::where('platform', $platform)->where('user_id', $user['id'])->find();
if ($isBind) {
$this->error('该手机号已经占用');
}
$result = $this->auth->direct($user->id);
} else {
$extend = array_filter(['nickname' => $nickname]);
$result = $this->auth->register($mobile, Random::alnum(), '', $mobile, $extend);
}
// 账号创建成功则添加绑定第三方绑定
if ($result) {
\addons\third\model\Third::create(['user_id' => $this->auth->id, 'platform' => $platform, 'apptype' => $apptype, 'openid' => $openid, 'unionid' => $unionid, 'openname' => $nickname], true);
}
//删除临时Session
Session::delete("third-{$platform}");
//绑定成功,跳转到之前页面
$this->success("绑定成功", $url);
}
$this->view->assign('userinfo', $thirdinfo['userinfo']);
$this->view->assign('platform', $platform);
$this->view->assign('url', $url);
$this->view->assign('bindurl', url("index/third/bind") . '?' . http_build_query(['platform' => $platform, 'url' => $url]));
$this->view->assign('captchaType', config('fastadmin.user_register_captcha'));
$this->view->assign('title', "账号绑定");
return $this->view->fetch();
}
/**
* 绑定账号
*/
public function bind()
{
$platform = $this->request->request('platform', '');
if (!in_array($platform, ['wechat', 'weibo', 'qq'])) {
$this->error("未找到指定平台");
}
$url = $this->request->get('url', $this->request->server('HTTP_REFERER', '', 'trim'), 'trim');
if (!$platform) {
$this->error("参数不正确");
}
$apptype = $platform == 'wechat' ? Service::getApptype() : '';
// 授权成功后的回调
$thirdinfo = Session::get("third-{$platform}");
if (!$thirdinfo) {
$this->redirect(addon_url('third/index/connect', [':platform' => $platform]) . '?url=' . urlencode($url));
}
$third = \addons\third\model\Third::where('user_id', $this->auth->id)->where('platform', $platform)->find();
if ($third) {
$this->error("已绑定账号,请勿重复绑定");
}
$time = time();
$values = [
'platform' => $platform,
'apptype' => $apptype,
'user_id' => $this->auth->id,
'unionid' => $thirdinfo['unionid'] ?? '',
'openid' => $thirdinfo['openid'],
'openname' => $thirdinfo['userinfo']['nickname'] ?? '',
'access_token' => $thirdinfo['access_token'],
'refresh_token' => $thirdinfo['refresh_token'],
'expires_in' => $thirdinfo['expires_in'],
'logintime' => $time,
'expiretime' => $time + $thirdinfo['expires_in'],
];
$isBind = Service::isBindThird($platform, $values['openid'], '', $values['unionid']);
if ($isBind) {
$this->error("已经绑定其它账号,无法进行绑定");
}
$third = \addons\third\model\Third::create($values);
if ($third) {
//删除临时Session
Session::delete("third-{$platform}");
$this->success("账号绑定成功", $url);
} else {
$this->error("账号绑定失败,请重试", $url);
}
}
/**
* 解绑账号
*/
public function unbind()
{
$platform = $this->request->request('platform', '');
if (!in_array($platform, ['wechat', 'weibo', 'qq'])) {
$this->error("未找到指定平台");
}
$apptype = $platform == 'wechat' ? Service::getApptype() : '';
$third = \addons\third\model\Third::where('user_id', $this->auth->id)
->where('platform', $platform)
->where(function ($query) use ($platform, $apptype) {
if ($platform == 'wechat') {
$query->where('apptype', $apptype);
}
})
->find();
if (!$third) {
$this->error("未找到指定的账号绑定信息");
}
Session::delete("third-{$platform}");
$third->delete();
$this->success("账号解绑成功");
}
}