Index.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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("third-redirecturl", $url);
  60. }
  61. // 跳转到登录授权页面
  62. $this->redirect($this->app->{$platform}->getAuthorizeUrl());
  63. }
  64. /**
  65. * 通知回调
  66. */
  67. public function callback()
  68. {
  69. $auth = $this->auth;
  70. //监听注册登录注销的事件
  71. Hook::add('user_login_successed', function ($user) use ($auth) {
  72. $expire = input('post.keeplogin') ? 30 * 86400 : 0;
  73. Cookie::set('uid', $user->id, $expire);
  74. Cookie::set('token', $auth->getToken(), $expire);
  75. });
  76. Hook::add('user_register_successed', function ($user) use ($auth) {
  77. Cookie::set('uid', $user->id);
  78. Cookie::set('token', $auth->getToken());
  79. });
  80. Hook::add('user_logout_successed', function ($user) use ($auth) {
  81. Cookie::delete('uid');
  82. Cookie::delete('token');
  83. });
  84. $platform = $this->request->param('platform');
  85. // 成功后返回之前页面,但忽略登录/注册页面
  86. $url = Session::has("third-redirecturl") ? Session::pull("third-redirecturl") : url('index/user/index');
  87. $url = preg_match("/\/user\/(register|login|resetpwd)/i", $url) ? url('index/user/index') : $url;
  88. // 授权成功后的回调
  89. $thirdinfo = $this->app->{$platform}->getUserInfo();
  90. if (!$thirdinfo) {
  91. $this->error(__('操作失败'), $url);
  92. }
  93. $thirdinfo['platform'] = $platform;
  94. $thirdinfo['apptype'] = $thirdinfo['apptype'] ?? '';
  95. $thirdinfo['unionid'] = $thirdinfo['unionid'] ?? '';
  96. //判断是否需要绑定
  97. $user = null;
  98. $isBind = Service::isBindThird($thirdinfo['platform'], $thirdinfo['openid'], '', $thirdinfo['unionid']);
  99. if ($this->auth->isLogin()) {
  100. if ($isBind) {
  101. $this->error("已经绑定其它账号,无法进行绑定", $url);
  102. }
  103. Service::connect($thirdinfo['platform'], $thirdinfo);
  104. $user = $this->auth->getUserinfo();
  105. }
  106. if (!$user) {
  107. $config = get_addon_config('third');
  108. //要求绑定账号或会员当前是登录状态
  109. if (!$isBind && ($config['bindaccount'] || $this->auth->id)) {
  110. Session::set("third-{$platform}", $thirdinfo);
  111. $this->redirect(url('index/third/prepare') . "?" . http_build_query(['platform' => $platform, 'url' => $url]));
  112. } else {
  113. //直接创建账号并跳转
  114. Service::connect($thirdinfo['platform'], $thirdinfo);
  115. $this->redirect($url);
  116. }
  117. } else {
  118. $this->redirect($url);
  119. }
  120. }
  121. /**
  122. * 绑定账号
  123. */
  124. public function bind()
  125. {
  126. $platform = $this->request->request('platform', $this->request->param('platform', ''));
  127. $url = $this->request->get('url', $this->request->server('HTTP_REFERER', '', 'trim'), 'trim');
  128. $redirecturl = url("index/third/bind") . "?" . http_build_query(['platform' => $platform, 'url' => $url]);
  129. $this->redirect($redirecturl);
  130. }
  131. /**
  132. * 解绑账号
  133. */
  134. public function unbind()
  135. {
  136. $platform = $this->request->request('platform', $this->request->param('platform', ''));
  137. $url = $this->request->get('url', $this->request->server('HTTP_REFERER', '', 'trim'), 'trim');
  138. $redirecturl = url("index/third/unbind") . "?" . http_build_query(['platform' => $platform, 'url' => $url]);
  139. $this->redirect($redirecturl);
  140. }
  141. }