Third.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace app\index\controller;
  3. use addons\third\library\Application;
  4. use addons\third\library\Service;
  5. use app\common\controller\Frontend;
  6. use app\common\library\Sms;
  7. use fast\Random;
  8. use think\Cookie;
  9. use think\Hook;
  10. use think\Lang;
  11. use think\Session;
  12. /**
  13. * 第三方登录控制器
  14. */
  15. class Third extends Frontend
  16. {
  17. protected $noNeedLogin = ['prepare'];
  18. protected $noNeedRight = ['*'];
  19. protected $app = null;
  20. protected $options = [];
  21. protected $layout = 'default';
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $config = get_addon_config('third');
  26. $this->app = new Application($config);
  27. $auth = $this->auth;
  28. //监听注册登录事件
  29. Hook::add('user_login_successed', function ($user) use ($auth) {
  30. $expire = input('post.keeplogin') ? 30 * 86400 : 0;
  31. Cookie::set('uid', $user->id, $expire);
  32. Cookie::set('token', $auth->getToken(), $expire);
  33. });
  34. Hook::add('user_register_successed', function ($user) use ($auth) {
  35. Cookie::set('uid', $user->id);
  36. Cookie::set('token', $auth->getToken());
  37. });
  38. }
  39. /**
  40. * 准备绑定
  41. */
  42. public function prepare()
  43. {
  44. $platform = $this->request->request('platform', '');
  45. if (!in_array($platform, ['wechat', 'weibo', 'qq'])) {
  46. $this->error("未找到指定平台");
  47. }
  48. $url = $this->request->get('url', '/', 'trim,xss_clean');
  49. // 授权成功后的回调
  50. $thirdinfo = Session::get("third-{$platform}");
  51. if (!$thirdinfo) {
  52. $this->error("操作失败,请返回重试");
  53. }
  54. $apptype = Service::getApptype();
  55. $openid = $thirdinfo['openid'] ?? '';
  56. $unionid = $thirdinfo['unionid'] ?? '';
  57. // 如果是登录状态,直接跳到绑定
  58. if ($this->auth->id) {
  59. $isBind = Service::isBindThird($platform, $openid, '', $unionid);
  60. if ($isBind) {
  61. $this->error("已经绑定其它账号,无法进行绑定");
  62. }
  63. $this->redirect(url("index/third/bind") . "?" . http_build_query(['platform' => $platform, 'url' => $url]));
  64. }
  65. if ($this->request->isPost()) {
  66. $mobile = $this->request->post("mobile");
  67. $platform = $this->request->post("platform");
  68. $captcha = $this->request->post("captcha");
  69. $nickname = $thirdinfo['userinfo']['nickname'] ?? '';
  70. if (!Sms::check($mobile, $captcha, 'bind')) {
  71. $this->error(__('验证码不正确'));
  72. }
  73. $user = \app\common\model\User::where('mobile', $mobile)->find();
  74. if ($user) {
  75. $isBind = \addons\third\model\Third::where('platform', $platform)->where('user_id', $user['id'])->find();
  76. if ($isBind) {
  77. $this->error('该手机号已经占用');
  78. }
  79. $result = $this->auth->direct($user->id);
  80. } else {
  81. $extend = array_filter(['nickname' => $nickname]);
  82. $result = $this->auth->register($mobile, Random::alnum(), '', $mobile, $extend);
  83. }
  84. // 账号创建成功则添加绑定第三方绑定
  85. if ($result) {
  86. \addons\third\model\Third::create(['user_id' => $this->auth->id, 'platform' => $platform, 'apptype' => $apptype, 'openid' => $openid, 'unionid' => $unionid, 'openname' => $nickname], true);
  87. }
  88. //删除临时Session
  89. Session::delete("third-{$platform}");
  90. //绑定成功,跳转到之前页面
  91. $this->success("绑定成功", $url);
  92. }
  93. $this->view->assign('userinfo', $thirdinfo['userinfo']);
  94. $this->view->assign('platform', $platform);
  95. $this->view->assign('url', $url);
  96. $this->view->assign('bindurl', url("index/third/bind") . '?' . http_build_query(['platform' => $platform, 'url' => $url]));
  97. $this->view->assign('captchaType', config('fastadmin.user_register_captcha'));
  98. $this->view->assign('title', "账号绑定");
  99. return $this->view->fetch();
  100. }
  101. /**
  102. * 绑定账号
  103. */
  104. public function bind()
  105. {
  106. $platform = $this->request->request('platform', '');
  107. if (!in_array($platform, ['wechat', 'weibo', 'qq'])) {
  108. $this->error("未找到指定平台");
  109. }
  110. $url = $this->request->get('url', $this->request->server('HTTP_REFERER', '', 'trim'), 'trim');
  111. if (!$platform) {
  112. $this->error("参数不正确");
  113. }
  114. $apptype = $platform == 'wechat' ? Service::getApptype() : '';
  115. // 授权成功后的回调
  116. $thirdinfo = Session::get("third-{$platform}");
  117. if (!$thirdinfo) {
  118. $this->redirect(addon_url('third/index/connect', [':platform' => $platform]) . '?url=' . urlencode($url));
  119. }
  120. $third = \addons\third\model\Third::where('user_id', $this->auth->id)->where('platform', $platform)->find();
  121. if ($third) {
  122. $this->error("已绑定账号,请勿重复绑定");
  123. }
  124. $time = time();
  125. $values = [
  126. 'platform' => $platform,
  127. 'apptype' => $apptype,
  128. 'user_id' => $this->auth->id,
  129. 'unionid' => $thirdinfo['unionid'] ?? '',
  130. 'openid' => $thirdinfo['openid'],
  131. 'openname' => $thirdinfo['userinfo']['nickname'] ?? '',
  132. 'access_token' => $thirdinfo['access_token'],
  133. 'refresh_token' => $thirdinfo['refresh_token'],
  134. 'expires_in' => $thirdinfo['expires_in'],
  135. 'logintime' => $time,
  136. 'expiretime' => $time + $thirdinfo['expires_in'],
  137. ];
  138. $isBind = Service::isBindThird($platform, $values['openid'], '', $values['unionid']);
  139. if ($isBind) {
  140. $this->error("已经绑定其它账号,无法进行绑定");
  141. }
  142. $third = \addons\third\model\Third::create($values);
  143. if ($third) {
  144. //删除临时Session
  145. Session::delete("third-{$platform}");
  146. $this->success("账号绑定成功", $url);
  147. } else {
  148. $this->error("账号绑定失败,请重试", $url);
  149. }
  150. }
  151. /**
  152. * 解绑账号
  153. */
  154. public function unbind()
  155. {
  156. $platform = $this->request->request('platform', '');
  157. if (!in_array($platform, ['wechat', 'weibo', 'qq'])) {
  158. $this->error("未找到指定平台");
  159. }
  160. $apptype = $platform == 'wechat' ? Service::getApptype() : '';
  161. $third = \addons\third\model\Third::where('user_id', $this->auth->id)
  162. ->where('platform', $platform)
  163. ->where(function ($query) use ($platform, $apptype) {
  164. if ($platform == 'wechat') {
  165. $query->where('apptype', $apptype);
  166. }
  167. })
  168. ->find();
  169. if (!$third) {
  170. $this->error("未找到指定的账号绑定信息");
  171. }
  172. Session::delete("third-{$platform}");
  173. $third->delete();
  174. $this->success("账号解绑成功");
  175. }
  176. }