Api.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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\Lang;
  10. use think\Config;
  11. use think\Session;
  12. use think\Validate;
  13. /**
  14. * 第三方登录插件
  15. */
  16. class Api extends commonApi
  17. {
  18. protected $noNeedLogin = ['getAuthUrl', 'callback', 'account']; // 无需登录即可访问的方法,同时也无需鉴权了
  19. protected $noNeedRight = ['*']; // 无需鉴权即可访问的方法
  20. protected $app = null;
  21. protected $options = [];
  22. protected $config = null;
  23. public function _initialize()
  24. {
  25. //跨域检测
  26. check_cors_request();
  27. //设置session_id
  28. Config::set('session.id', $this->request->server("HTTP_SID"));
  29. parent::_initialize();
  30. $this->config = get_addon_config('third');
  31. $this->app = new Application($this->config);
  32. }
  33. /**
  34. * H5获取授权链接
  35. * @return void
  36. */
  37. public function getAuthUrl()
  38. {
  39. $url = $this->request->param('url');
  40. $platform = $this->request->param('platform');
  41. if (!$url || !$platform || !isset($this->config[$platform])) {
  42. $this->error('参数错误');
  43. }
  44. $this->config[$platform]['callback'] = $url;
  45. $this->app = new Application($this->config); //
  46. if (!$this->app->{$platform}) {
  47. $this->error(__('Invalid parameters'));
  48. }
  49. $this->success('', $this->app->{$platform}->getAuthorizeUrl());
  50. }
  51. /**
  52. * 公众号:wechat 授权回调的请求【非第三方,自己的前端请求】
  53. * @return void
  54. */
  55. public function callback()
  56. {
  57. $platform = $this->request->param('platform');
  58. if (!$this->app->{$platform}) {
  59. $this->error(__('Invalid parameters'));
  60. }
  61. $userinfo = $this->app->{$platform}->getUserInfo($this->request->param());
  62. if (!$userinfo) {
  63. $this->error(__('操作失败'));
  64. }
  65. $userinfo['apptype'] = 'mp';
  66. $userinfo['platform'] = $platform;
  67. $third = [
  68. 'avatar' => $userinfo['userinfo']['avatar'],
  69. 'nickname' => $userinfo['userinfo']['nickname']
  70. ];
  71. $user = null;
  72. if ($this->auth->isLogin() || Service::isBindThird($userinfo['platform'], $userinfo['openid'], $userinfo['apptype'], $userinfo['unionid'])) {
  73. Service::connect($userinfo['platform'], $userinfo);
  74. $user = $this->auth->getUserinfo();
  75. } else {
  76. $user = false;
  77. Session::set('third-userinfo', $userinfo);
  78. }
  79. $this->success("授权成功!", ['user' => $user, 'third' => $third]);
  80. }
  81. /**
  82. * 登录或创建账号
  83. */
  84. public function account()
  85. {
  86. if ($this->request->isPost()) {
  87. $params = Session::get('third-userinfo');
  88. $mobile = $this->request->post('mobile', '');
  89. $code = $this->request->post('code');
  90. $token = $this->request->post('__token__');
  91. $rule = [
  92. 'mobile' => 'require|regex:/^1\d{10}$/',
  93. '__token__' => 'require|token',
  94. ];
  95. $msg = [
  96. 'mobile' => 'Mobile is incorrect',
  97. ];
  98. $data = [
  99. 'mobile' => $mobile,
  100. '__token__' => $token,
  101. ];
  102. $ret = Sms::check($mobile, $code, 'bind');
  103. if (!$ret) {
  104. $this->error(__('验证码错误'));
  105. }
  106. $validate = new Validate($rule, $msg);
  107. $result = $validate->check($data);
  108. if (!$result) {
  109. $this->error(__($validate->getError()), ['__token__' => $this->request->token()]);
  110. }
  111. $userinfo = \app\common\model\User::where('mobile', $mobile)->find();
  112. if ($userinfo) {
  113. $result = $this->auth->direct($userinfo->id);
  114. } else {
  115. $result = $this->auth->register($mobile, Random::alnum(), '', $mobile);
  116. }
  117. if ($result) {
  118. Service::connect($params['platform'], $params);
  119. $this->success(__('绑定账号成功'), ['userinfo' => $this->auth->getUserinfo()]);
  120. } else {
  121. $this->error($this->auth->getError(), ['__token__' => $this->request->token()]);
  122. }
  123. }
  124. }
  125. }