Api.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace addons\third\controller;
  3. use addons\third\library\Application;
  4. use app\common\controller\Api as commonApi;
  5. use addons\third\library\Service;
  6. use addons\third\model\Third;
  7. use app\common\library\Sms;
  8. use fast\Random;
  9. use think\Config;
  10. use think\Session;
  11. use think\Validate;
  12. /**
  13. * 第三方登录插件
  14. */
  15. class Api extends commonApi
  16. {
  17. protected $noNeedLogin = ['getAuthUrl', 'callback', 'account', 'getOpenidCallback']; // 无需登录即可访问的方法,同时也无需鉴权了
  18. protected $noNeedRight = ['*']; // 无需鉴权即可访问的方法
  19. protected $app = null;
  20. protected $options = [];
  21. protected $config = null;
  22. public function _initialize()
  23. {
  24. //跨域检测
  25. check_cors_request();
  26. //设置session_id
  27. Config::set('session.id', $this->request->server("HTTP_SID"));
  28. parent::_initialize();
  29. $this->config = get_addon_config('third');
  30. $name = $this->request->request('addon');
  31. if (!$name && get_addon_info($name)) {
  32. //判断是否有插件标识,如果有插件标识则取指定插件中的配置
  33. $config = get_addon_config($name);
  34. $this->config = array_merge($this->config, array_intersect_key($config, array_flip(['qq', 'wechat', 'wechatweb', 'weibo'])));
  35. }
  36. //判断是否静默授权
  37. $scope = $this->request->param('scope', '', 'trim');
  38. if ($scope) {
  39. $this->config['scope'] = $scope;
  40. }
  41. $this->app = new Application($this->config);
  42. }
  43. /**
  44. * 获取授权链接
  45. */
  46. public function getAuthUrl()
  47. {
  48. $url = $this->request->param('url', '', 'trim');
  49. $platform = $this->request->param('platform');
  50. if (!$url || !$platform || !isset($this->config[$platform])) {
  51. $this->error('参数错误');
  52. }
  53. $this->config[$platform]['callback'] = $url;
  54. $this->app = new Application($this->config);
  55. if (!$this->app->{$platform}) {
  56. $this->error(__('参数错误'));
  57. }
  58. $this->success('', $this->app->{$platform}->getAuthorizeUrl());
  59. }
  60. /**
  61. * 授权回调的请求
  62. */
  63. public function callback()
  64. {
  65. $platform = $this->request->param('platform');
  66. $apptype = $this->request->param('apptype');
  67. $base = $this->request->param('base', 0); //只返回基础信息
  68. $scope = $this->request->param('scope', '');
  69. $bind = $this->request->param('bind', 1); //默认要求绑定账号
  70. if (!$this->app->{$platform}) {
  71. $this->error(__('参数错误'));
  72. }
  73. $userinfo = $this->app->{$platform}->getUserInfo($this->request->param('', null, null));
  74. if (!$userinfo) {
  75. $this->error(__('操作失败'));
  76. }
  77. if ($base) {
  78. $this->success("授权成功!", $userinfo['userinfo']);
  79. }
  80. $userinfo['apptype'] = $apptype ?: Service::getApptype();
  81. $userinfo['platform'] = $platform;
  82. $third = [
  83. 'avatar' => $userinfo['userinfo']['avatar'],
  84. 'nickname' => $userinfo['userinfo']['nickname']
  85. ];
  86. $user = null;
  87. $config = get_addon_config('third');
  88. if (!$bind || $this->auth->isLogin() || Service::isBindThird($userinfo['platform'], $userinfo['openid'], $userinfo['apptype'], $userinfo['unionid']) || !$config['bindaccount']) {
  89. $result = Service::connect($userinfo['platform'], $userinfo);
  90. if (!$result) {
  91. $this->error('授权登录失败');
  92. }
  93. $user = $this->auth->getUserinfo();
  94. } else {
  95. $user = false;
  96. Session::set('third-userinfo', $userinfo);
  97. }
  98. $this->success("授权成功!", ['user' => $user, 'third' => $third, 'openid' => $userinfo['openid']]);
  99. }
  100. /**
  101. * 获取Openid回调
  102. */
  103. public function getOpenidCallback()
  104. {
  105. $platform = $this->request->param('platform');
  106. if (!$this->app->{$platform}) {
  107. $this->error(__('参数错误'));
  108. }
  109. $userinfo = $this->app->{$platform}->getUserInfo($this->request->param('', null, null));
  110. if (!$userinfo) {
  111. $this->error(__('操作失败'));
  112. }
  113. $this->success('', ['openid' => $userinfo['openid']]);
  114. }
  115. /**
  116. * 登录或创建账号
  117. */
  118. public function account()
  119. {
  120. if ($this->request->isPost()) {
  121. $params = Session::get('third-userinfo');
  122. $mobile = $this->request->post('mobile', '');
  123. $code = $this->request->post('code', $this->request->post('captcha'));
  124. $token = $this->request->post('__token__');
  125. $rule = [
  126. 'mobile' => 'require|regex:/^1\d{10}$/',
  127. '__token__' => 'require|token',
  128. ];
  129. $msg = [
  130. 'mobile' => 'Mobile is incorrect',
  131. ];
  132. $data = [
  133. 'mobile' => $mobile,
  134. '__token__' => $token,
  135. ];
  136. $ret = Sms::check($mobile, $code, 'bind');
  137. if (!$ret) {
  138. $this->error(__('验证码错误'));
  139. }
  140. $validate = new Validate($rule, $msg);
  141. $result = $validate->check($data);
  142. if (!$result) {
  143. $this->error(__($validate->getError()), ['__token__' => $this->request->token()]);
  144. }
  145. $userinfo = \app\common\model\User::where('mobile', $mobile)->find();
  146. if ($userinfo) {
  147. $isBind = Third::where('platform', $params['platform'])->where('user_id', $userinfo['id'])->find();
  148. if ($isBind) {
  149. $this->error('该手机号已经占用');
  150. }
  151. $result = $this->auth->direct($userinfo->id);
  152. } else {
  153. $result = $this->auth->register($mobile, Random::alnum(), '', $mobile, $params['userinfo'] ?? []);
  154. }
  155. if ($result) {
  156. $result = Service::connect($params['platform'], $params);
  157. if (!$result) {
  158. $this->error('授权登录失败');
  159. }
  160. $this->success(__('绑定账号成功'), ['userinfo' => $this->auth->getUserinfo()]);
  161. } else {
  162. $this->error($this->auth->getError(), ['__token__' => $this->request->token()]);
  163. }
  164. }
  165. }
  166. /**
  167. * 获取第三方登录绑定列表
  168. */
  169. public function getBindList()
  170. {
  171. $thirdList = Third::where('user_id', $this->auth->id)->field('user_id,platform,apptype')->select();
  172. $this->success('', collection($thirdList)->toArray());
  173. }
  174. /**
  175. * 解绑
  176. */
  177. public function unbind()
  178. {
  179. $platform = $this->request->post("platform");
  180. $apptype = $this->request->post("apptype");
  181. $exist = Third::where('user_id', $this->auth->id)->where('platform', $platform)->where('apptype', $apptype)->find();
  182. if ($exist) {
  183. $this->success("解绑成功");
  184. } else {
  185. $this->error("未找到相关数据");
  186. }
  187. }
  188. }