Uservue.php
5.9 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
<?php
namespace addons\litestore\controller\api;
use app\common\controller\Api;
use addons\third\library\Application;
use addons\litestore\library\Service;
use think\Cookie;
use think\Hook;
use think\Session;
use addons\third\model\Third;
use app\common\model\User;
use think\Db;
use think\exception\PDOException;
class Uservue extends Api
{
protected $noNeedLogin = ['callback','connect'];
protected $noNeedRight = ['*'];
public function _initialize()
{
parent::_initialize();
$config = get_addon_config('third');
if (!$config)
{
$this->error(__('Invalid parameters'));
}
$options = array_intersect_key($config, array_flip(['qq', 'weibo', 'wechat']));
foreach ($options as $k => &$v)
{
$v['callback'] = addon_url('litestore/api.uservue/callback', [':platform' => $k], false, true);
$options[$k] = $v;
}
unset($v);
$this->app = new Application($options);
}
public function index()
{
$third_data = Third::where('user_id',$this->auth->id)->find();
$this->success('', [
'user' => $this->auth->getUser(),
'third' => $third_data
]);
}
/**
* 发起授权
*/
public function connect()
{
$platform = $this->request->param('platform');
$url = $this->request->param('url');
if (!$this->app->{$platform}) {
$this->error(__('Invalid parameters'));
}
if ($url) {
Session::set("redirecturl", $url);
}
// 跳转到登录授权页面
$AuthorizeUrl = $this->app->{$platform}->getAuthorizeUrl();
header('Location: '.$AuthorizeUrl);
exit();
return;
}
/**
* 通知回调
*/
public function callback()
{
$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());
});
Hook::add('user_logout_successed', function ($user) use ($auth) {
Cookie::delete('uid');
Cookie::delete('token');
});
$platform = $this->request->param('platform');
if(!$platform){
$platform = 'wechat';
}
// 成功后返回之前页面
$url = Session::has("redirecturl") ? Session::pull("redirecturl") : url('index/user/index');
// 授权成功后的回调
$result = $this->app->{$platform}->getUserInfo();
if ($result) {
//这里根据 本身是否存在用户登录的情况 如果用户登录没有登录微信 那么自动绑定流程 如果没有登录,那么先注册账号 然后再登录
if($this->auth->getUser()){
$loginret = $this->bind_connect($platform, $result);
}else{
$extend = ['mobile'=>'NoLoginData' ,'gender' => '0', 'nickname' => '游客', 'avatar' =>'/assets/img/avatar.png'];
$loginret = Service::connect_hawk($platform, $result,$extend);
}
if ($loginret) {
$synchtml = '';
////////////////同步到Ucenter////////////////
if (defined('UC_STATUS') && UC_STATUS) {
$uc = new \addons\ucenter\library\client\Client();
$synchtml = $uc->uc_user_synlogin($this->auth->id);
}
$UrlSetCookie = explode("/octothorpe",$url)[0];
$UrlSetCookie = explode("?",$UrlSetCookie)[0] . '#/SetToken';
header('Location: '.$UrlSetCookie.'?token='.$auth->getToken().'&url='.urlencode($url));
exit();
}
}
$this->error(__('操作失败'), $url);
}
private function bind_connect($platform, $params = [], $extend = [], $keeptime = 0)
{
$time = time();
$values = [
'platform' => $platform,
'openid' => $params['openid'],
'openname' => isset($params['userinfo']['nickname']) ? $params['userinfo']['nickname'] : '',
'access_token' => $params['access_token'],
'refresh_token' => $params['refresh_token'],
'expires_in' => $params['expires_in'],
'logintime' => $time,
'expiretime' => $time + $params['expires_in'],
];
$third = Third::get(['platform' => $platform, 'openid' => $params['openid']]);
if ($third) {
$user = User::get($third['user_id']);
if (!$user) {
return FALSE;
}
$third->save($values);
return TRUE;
} else {
Db::startTrans();
try {
$user = $this->auth->getUser();
$fields = ['email' => 'u' . $user->id . '@fastadmin.net'];
if (isset($params['userinfo']['nickname']))
$fields['nickname'] = $params['userinfo']['nickname'];
if (isset($params['userinfo']['avatar']))
$fields['avatar'] = $params['userinfo']['avatar'];
// 更新会员资料
$user = User::get($user->id);
$user->save($fields);
// 保存第三方信息
$values['user_id'] = $user->id;
Third::create($values);
Db::commit();
} catch (PDOException $e) {
Db::rollback();
$this->auth->logout();
return FALSE;
}
return TRUE;
}
}
}