Common.php 3.5 KB

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