Common.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 中闽 < 1464674022@qq.com >
  5. * Date: 2019/12/5
  6. * Time: 17:44
  7. */
  8. namespace app\api\controller;
  9. use app\api\controller\base\Base;
  10. use app\api\controller\base\Permissions;
  11. use app\common\model\User;
  12. use app\common\service\WebService;
  13. class Common extends Base
  14. {
  15. //登入
  16. public function login()
  17. {
  18. $return_url = $this->request->param('return_url', '');
  19. session('return_url', $return_url, 'login');
  20. $callback = urlencode(url('/api/common/loginNotify', '', false, true));
  21. $state = md5('appointment' . time());
  22. session('state', $state, 'login');
  23. $url = "https://www.jucai.gov.cn/api/auth/wechat_auth?url=$callback&state=$state";
  24. $this->redirect($url);
  25. }
  26. public function loginNotify()
  27. {
  28. $mystate = session('state', '', 'login');
  29. $state = $this->request->param('state');
  30. if (!$mystate || !$state || $mystate != $state) {
  31. $this->json_error('登入失败,请重新登入');
  32. }
  33. (new WebService())->record('登入信息');
  34. $post = $this->request->param();
  35. $validate = new \think\Validate([
  36. ['openid', 'max:50'],
  37. ['unionid', 'max:50'],
  38. // ['nickname|昵称', 'max:50'],
  39. // ['headimgurl|头像', 'max:255'],
  40. // ['sex|性别', 'in:0,1,2'],
  41. // ['country|国家', 'max:50'],
  42. // ['province|省份', 'max:50'],
  43. // ['city|城市', 'max:50'],
  44. ]);
  45. if (!$validate->check($post)) {
  46. $this->json_error('提交失败:' . $validate->getError());
  47. }
  48. $unionid = $this->request->param('unionid');
  49. $passport = $unionid;
  50. $user = User::get(['user_type' => User::TYPE_WECHAT, 'passport' => $unionid]);
  51. if (!$user) {
  52. $openid = $this->request->param('openid');
  53. $passport = $openid;
  54. $user = User::get(['user_type' => User::TYPE_WECHAT, 'passport' => $openid, 'unionid' => $unionid]);
  55. if (!$user) {
  56. $user = User::get(['user_type' => User::TYPE_WECHAT, 'passport' => $openid]);
  57. }
  58. }
  59. if (!$passport) {
  60. $this->json_error('openid 不能为空');
  61. }
  62. if (!$user) {
  63. //注册
  64. $user = new User();
  65. $data = [
  66. 'openid' => $post['openid']??'',
  67. 'unionid' => $post['unionid']??'',
  68. 'passport' => $passport,
  69. 'nickname' => limitStrLen('nickname', $post['nickname']??'', 50),
  70. 'user_type' => User::TYPE_WECHAT,
  71. 'user_cate' => User::CATE_USER,
  72. 'head_pic' => limitStrLen('headimgurl', $post['headimgurl']??'', 255),
  73. 'status' => User::STATUS_PASS,
  74. 'ip' => $this->request->ip(),
  75. 'sex' => $post['sex']??0,
  76. 'country' => $post['country']??'',
  77. 'province' => $post['province']??'',
  78. 'city' => $post['city']??'',
  79. "login_time" => time(),
  80. "create_time" => time()
  81. ];
  82. if (false == $user->allowField(true)->save($data)) {
  83. $this->json_error('添加失败');
  84. }
  85. } else {
  86. $data = [
  87. "login_time" => time(),
  88. 'openid' => $post['openid']??'',
  89. 'unionid' => $post['unionid']??'',
  90. 'head_pic' => limitStrLen('headimgurl', $post['headimgurl']??'', 255),
  91. ];
  92. $user->allowField(true)->save($data);
  93. }
  94. //登入成功 ,返回 前端
  95. $return_url = session('return_url', '', 'login');
  96. $this->redirect($return_url . '?jwt=' . Permissions::createJwt($user->id, $user->login_time, 3600 * 24));
  97. }
  98. }