Index.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. $url = $this->request->request('url', $this->request->server('HTTP_REFERER', '/'), 'trim');
  47. if (!$this->app->{$platform}) {
  48. $this->error(__('Invalid parameters'));
  49. }
  50. if ($url) {
  51. Session::set("redirecturl", $url);
  52. }
  53. // 跳转到登录授权页面
  54. $this->redirect($this->app->{$platform}->getAuthorizeUrl());
  55. return;
  56. }
  57. /**
  58. * 通知回调
  59. */
  60. public function callback()
  61. {
  62. $auth = $this->auth;
  63. //监听注册登录注销的事件
  64. Hook::add('user_login_successed', function ($user) use ($auth) {
  65. $expire = input('post.keeplogin') ? 30 * 86400 : 0;
  66. Cookie::set('uid', $user->id, $expire);
  67. Cookie::set('token', $auth->getToken(), $expire);
  68. });
  69. Hook::add('user_register_successed', function ($user) use ($auth) {
  70. Cookie::set('uid', $user->id);
  71. Cookie::set('token', $auth->getToken());
  72. });
  73. Hook::add('user_logout_successed', function ($user) use ($auth) {
  74. Cookie::delete('uid');
  75. Cookie::delete('token');
  76. });
  77. $platform = $this->request->param('platform');
  78. // 成功后返回之前页面
  79. $url = Session::has("redirecturl") ? Session::pull("redirecturl") : url('index/user/index');
  80. // 授权成功后的回调
  81. $userinfo = $this->app->{$platform}->getUserInfo();
  82. if (!$userinfo) {
  83. $this->error(__('操作失败'), $url);
  84. }
  85. Session::set("{$platform}-userinfo", $userinfo);
  86. //判断是否启用账号绑定
  87. $third = Third::get(['platform' => $platform, 'openid' => $userinfo['openid']]);
  88. if (!$third) {
  89. $config = get_addon_config('third');
  90. //要求绑定账号或会员当前是登录状态
  91. if ($config['bindaccount'] || $this->auth->id) {
  92. $this->redirect(url('index/third/prepare') . "?" . http_build_query(['platform' => $platform, 'url' => $url]));
  93. }
  94. }
  95. $loginret = Service::connect($platform, $userinfo);
  96. if ($loginret) {
  97. $this->redirect($url);
  98. }
  99. }
  100. /**
  101. * 绑定账号
  102. */
  103. public function bind()
  104. {
  105. $platform = $this->request->request('platform', $this->request->param('platform', ''));
  106. $url = $this->request->get('url', $this->request->server('HTTP_REFERER'));
  107. $redirecturl = url("index/third/bind") . "?" . http_build_query(['platform' => $platform, 'url' => $url]);
  108. $this->redirect($redirecturl);
  109. return;
  110. }
  111. /**
  112. * 解绑账号
  113. */
  114. public function unbind()
  115. {
  116. $platform = $this->request->request('platform', $this->request->param('platform', ''));
  117. $url = $this->request->get('url', $this->request->server('HTTP_REFERER'));
  118. $redirecturl = url("index/third/unbind") . "?" . http_build_query(['platform' => $platform, 'url' => $url]);
  119. $this->redirect($redirecturl);
  120. return;
  121. }
  122. }