Index.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace addons\third\controller;
  3. use addons\third\library\Application;
  4. use addons\third\library\Service;
  5. use addons\third\model\Third;
  6. use think\addons\Controller;
  7. use think\Config;
  8. use think\Cookie;
  9. use think\Hook;
  10. use think\Lang;
  11. use think\Session;
  12. /**
  13. * 第三方登录插件
  14. */
  15. class Index extends Controller
  16. {
  17. protected $app = null;
  18. protected $options = [];
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $config = get_addon_config('third');
  23. $this->app = new Application($config);
  24. }
  25. /**
  26. * 插件首页
  27. */
  28. public function index()
  29. {
  30. if (!\app\admin\library\Auth::instance()->id) {
  31. $this->error('当前插件暂无前台页面');
  32. }
  33. $platformList = [];
  34. if ($this->auth->id) {
  35. $platformList = Third::where('user_id', $this->auth->id)->column('platform');
  36. }
  37. $this->view->assign('platformList', $platformList);
  38. return $this->view->fetch();
  39. }
  40. /**
  41. * 发起授权
  42. */
  43. public function connect()
  44. {
  45. $platform = $this->request->param('platform');
  46. $config = get_addon_config('third');
  47. if (!$config['status']) {
  48. $this->error("第三方登录已关闭");
  49. }
  50. $status = explode(',', $config['status']);
  51. if (!in_array($platform, $status)) {
  52. $this->error("该登录方式已关闭");
  53. }
  54. $url = $this->request->request('url', $this->request->server('HTTP_REFERER', '/', 'trim'), 'trim');
  55. if (!$this->app->{$platform}) {
  56. $this->error('参数错误');
  57. }
  58. if ($url) {
  59. Session::set("redirecturl", $url);
  60. }
  61. // 跳转到登录授权页面
  62. $this->redirect($this->app->{$platform}->getAuthorizeUrl());
  63. return;
  64. }
  65. /**
  66. * 通知回调
  67. */
  68. public function callback()
  69. {
  70. $auth = $this->auth;
  71. //监听注册登录注销的事件
  72. Hook::add('user_login_successed', function ($user) use ($auth) {
  73. $expire = input('post.keeplogin') ? 30 * 86400 : 0;
  74. Cookie::set('uid', $user->id, $expire);
  75. Cookie::set('token', $auth->getToken(), $expire);
  76. });
  77. Hook::add('user_register_successed', function ($user) use ($auth) {
  78. Cookie::set('uid', $user->id);
  79. Cookie::set('token', $auth->getToken());
  80. });
  81. Hook::add('user_logout_successed', function ($user) use ($auth) {
  82. Cookie::delete('uid');
  83. Cookie::delete('token');
  84. });
  85. $platform = $this->request->param('platform');
  86. // 成功后返回之前页面,但忽略登录/注册页面
  87. $url = Session::has("redirecturl") ? Session::pull("redirecturl") : url('index/user/index');
  88. $url = preg_match("/\/user\/(register|login|resetpwd)/i", $url) ? url('index/user/index') : $url;
  89. // 授权成功后的回调
  90. $userinfo = $this->app->{$platform}->getUserInfo();
  91. if (!$userinfo) {
  92. $this->error(__('操作失败'), $url);
  93. }
  94. Session::set("{$platform}-userinfo", $userinfo);
  95. //判断是否启用账号绑定
  96. $third = Third::get(['platform' => $platform, 'openid' => $userinfo['openid']]);
  97. if (!$third) {
  98. $config = get_addon_config('third');
  99. //要求绑定账号或会员当前是登录状态
  100. if ($config['bindaccount'] || $this->auth->id) {
  101. $this->redirect(url('index/third/prepare') . "?" . http_build_query(['platform' => $platform, 'url' => $url]));
  102. }
  103. }
  104. //直接登录
  105. $loginret = Service::connect($platform, $userinfo);
  106. if ($loginret) {
  107. $this->redirect($url);
  108. } else {
  109. $this->error("登录失败,请返回重试", $url);
  110. }
  111. }
  112. /**
  113. * 绑定账号
  114. */
  115. public function bind()
  116. {
  117. $platform = $this->request->request('platform', $this->request->param('platform', ''));
  118. $url = $this->request->get('url', $this->request->server('HTTP_REFERER', '', 'trim'), 'trim');
  119. $redirecturl = url("index/third/bind") . "?" . http_build_query(['platform' => $platform, 'url' => $url]);
  120. $this->redirect($redirecturl);
  121. return;
  122. }
  123. /**
  124. * 解绑账号
  125. */
  126. public function unbind()
  127. {
  128. $platform = $this->request->request('platform', $this->request->param('platform', ''));
  129. $url = $this->request->get('url', $this->request->server('HTTP_REFERER', '', 'trim'), 'trim');
  130. $redirecturl = url("index/third/unbind") . "?" . http_build_query(['platform' => $platform, 'url' => $url]);
  131. $this->redirect($redirecturl);
  132. return;
  133. }
  134. }