Third.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace app\index\controller;
  3. use addons\third\library\Application;
  4. use app\common\controller\Frontend;
  5. use think\Lang;
  6. use think\Session;
  7. /**
  8. * 第三方登录控制器
  9. */
  10. class Third extends Frontend
  11. {
  12. protected $noNeedLogin = ['prepare'];
  13. protected $noNeedRight = ['*'];
  14. protected $app = null;
  15. protected $options = [];
  16. protected $layout = 'default';
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $config = get_addon_config('third');
  21. $this->app = new Application($config);
  22. }
  23. /**
  24. * 准备绑定
  25. */
  26. public function prepare()
  27. {
  28. $platform = $this->request->request('platform');
  29. $url = $this->request->get('url', '/');
  30. if ($this->auth->id) {
  31. $this->redirect(url("index/third/bind") . "?" . http_build_query(['platform' => $platform, 'url' => $url]));
  32. }
  33. // 授权成功后的回调
  34. $userinfo = Session::get("{$platform}-userinfo");
  35. if (!$userinfo) {
  36. $this->error("操作失败,请返回重度");
  37. }
  38. Lang::load([
  39. APP_PATH . 'index' . DS . 'lang' . DS . $this->request->langset() . DS . 'user' . EXT,
  40. ]);
  41. $this->view->assign('userinfo', $userinfo['userinfo']);
  42. $this->view->assign('platform', $platform);
  43. $this->view->assign('url', $url);
  44. $this->view->assign('bindurl', url("index/third/bind") . '?' . http_build_query(['platform' => $platform, 'url' => $url]));
  45. $this->view->assign('captchaType', config('fastadmin.user_register_captcha'));
  46. $this->view->assign('title', "账号绑定");
  47. return $this->view->fetch();
  48. }
  49. /**
  50. * 绑定账号
  51. */
  52. public function bind()
  53. {
  54. $platform = $this->request->request('platform');
  55. $url = $this->request->get('url', $this->request->server('HTTP_REFERER'));
  56. if (!$platform) {
  57. $this->error("参数不正确");
  58. }
  59. // 授权成功后的回调
  60. $userinfo = Session::get("{$platform}-userinfo");
  61. if (!$userinfo) {
  62. $this->redirect(addon_url('third/index/connect', [':platform' => $platform]) . '?url=' . urlencode($url));
  63. }
  64. $third = \addons\third\model\Third::where('user_id', $this->auth->id)->where('platform', $platform)->find();
  65. if ($third) {
  66. $this->error("已绑定账号,请勿重复绑定");
  67. }
  68. $time = time();
  69. $values = [
  70. 'platform' => $platform,
  71. 'user_id' => $this->auth->id,
  72. 'openid' => $userinfo['openid'],
  73. 'openname' => isset($userinfo['userinfo']['nickname']) ? $userinfo['userinfo']['nickname'] : '',
  74. 'access_token' => $userinfo['access_token'],
  75. 'refresh_token' => $userinfo['refresh_token'],
  76. 'expires_in' => $userinfo['expires_in'],
  77. 'logintime' => $time,
  78. 'expiretime' => $time + $userinfo['expires_in'],
  79. ];
  80. $third = \addons\third\model\Third::create($values);
  81. if ($third) {
  82. $this->success("账号绑定成功", $url);
  83. } else {
  84. $this->error("账号绑定失败,请重试", $url);
  85. }
  86. }
  87. /**
  88. * 解绑账号
  89. */
  90. public function unbind()
  91. {
  92. $platform = $this->request->request('platform');
  93. $third = \addons\third\model\Third::where('user_id', $this->auth->id)->where('platform', $platform)->find();
  94. if (!$third) {
  95. $this->error("未找到指定的账号绑定信息");
  96. }
  97. $third->delete();
  98. $this->success("账号解绑成功");
  99. }
  100. }