Index.php
4.7 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
<?php
namespace addons\third\controller;
use addons\third\library\Application;
use addons\third\library\Service;
use addons\third\model\Third;
use think\addons\Controller;
use think\Config;
use think\Cookie;
use think\Hook;
use think\Lang;
use think\Session;
/**
* 第三方登录插件
*/
class Index extends Controller
{
protected $app = null;
protected $options = [];
public function _initialize()
{
parent::_initialize();
$config = get_addon_config('third');
$this->app = new Application($config);
}
/**
* 插件首页
*/
public function index()
{
if (!\app\admin\library\Auth::instance()->id) {
$this->error('当前插件暂无前台页面');
}
$platformList = [];
if ($this->auth->id) {
$platformList = Third::where('user_id', $this->auth->id)->column('platform');
}
$this->view->assign('platformList', $platformList);
return $this->view->fetch();
}
/**
* 发起授权
*/
public function connect()
{
$platform = $this->request->param('platform');
$config = get_addon_config('third');
if (!$config['status']) {
$this->error("第三方登录已关闭");
}
$status = explode(',', $config['status']);
if (!in_array($platform, $status)) {
$this->error("该登录方式已关闭");
}
$url = $this->request->request('url', $this->request->server('HTTP_REFERER', '/', 'trim'), 'trim');
if (!$this->app->{$platform}) {
$this->error('参数错误');
}
if ($url) {
Session::set("redirecturl", $url);
}
// 跳转到登录授权页面
$this->redirect($this->app->{$platform}->getAuthorizeUrl());
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');
// 成功后返回之前页面,但忽略登录/注册页面
$url = Session::has("redirecturl") ? Session::pull("redirecturl") : url('index/user/index');
$url = preg_match("/\/user\/(register|login|resetpwd)/i", $url) ? url('index/user/index') : $url;
// 授权成功后的回调
$userinfo = $this->app->{$platform}->getUserInfo();
if (!$userinfo) {
$this->error(__('操作失败'), $url);
}
Session::set("{$platform}-userinfo", $userinfo);
//判断是否启用账号绑定
$third = Third::get(['platform' => $platform, 'openid' => $userinfo['openid']]);
if (!$third) {
$config = get_addon_config('third');
//要求绑定账号或会员当前是登录状态
if ($config['bindaccount'] || $this->auth->id) {
$this->redirect(url('index/third/prepare') . "?" . http_build_query(['platform' => $platform, 'url' => $url]));
}
}
//直接登录
$loginret = Service::connect($platform, $userinfo);
if ($loginret) {
$this->redirect($url);
} else {
$this->error("登录失败,请返回重试", $url);
}
}
/**
* 绑定账号
*/
public function bind()
{
$platform = $this->request->request('platform', $this->request->param('platform', ''));
$url = $this->request->get('url', $this->request->server('HTTP_REFERER', '', 'trim'), 'trim');
$redirecturl = url("index/third/bind") . "?" . http_build_query(['platform' => $platform, 'url' => $url]);
$this->redirect($redirecturl);
return;
}
/**
* 解绑账号
*/
public function unbind()
{
$platform = $this->request->request('platform', $this->request->param('platform', ''));
$url = $this->request->get('url', $this->request->server('HTTP_REFERER', '', 'trim'), 'trim');
$redirecturl = url("index/third/unbind") . "?" . http_build_query(['platform' => $platform, 'url' => $url]);
$this->redirect($redirecturl);
return;
}
}