Common.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 loginLw()
  27. {
  28. $user = User::get(9);
  29. //登入成功 ,返回 前端
  30. return (Permissions::createJwt($user->id, $user->login_time, 3600 * 24));
  31. $return_url = 'https://localhost:8080/';
  32. $this->redirect($return_url . '?jwt=' . Permissions::createJwt($user->id, $user->login_time, 3600 * 24));
  33. }
  34. public function loginNotify()
  35. {
  36. $mystate = session('state', '', 'login');
  37. $state = $this->request->param('state');
  38. if (!$mystate || !$state || $mystate != $state) {
  39. $this->json_error('登入失败,请重新登入');
  40. }
  41. (new WebService())->record('登入信息');
  42. $post = $this->request->param();
  43. $validate = new \think\Validate([
  44. ['openid', 'max:50'],
  45. ['unionid', 'max:50'],
  46. // ['nickname|昵称', 'max:50'],
  47. // ['headimgurl|头像', 'max:255'],
  48. // ['sex|性别', 'in:0,1,2'],
  49. // ['country|国家', 'max:50'],
  50. // ['province|省份', 'max:50'],
  51. // ['city|城市', 'max:50'],
  52. ]);
  53. if (!$validate->check($post)) {
  54. $this->json_error('提交失败:' . $validate->getError());
  55. }
  56. $unionid = $this->request->param('unionid');
  57. $passport = $unionid;
  58. $user = User::get(['user_type' => User::TYPE_WECHAT, 'passport' => $unionid]);
  59. if (!$user) {
  60. $openid = $this->request->param('openid');
  61. $passport = $openid;
  62. $user = User::get(['user_type' => User::TYPE_WECHAT, 'passport' => $openid, 'unionid' => $unionid]);
  63. if (!$user) {
  64. $user = User::get(['user_type' => User::TYPE_WECHAT, 'passport' => $openid]);
  65. }
  66. }
  67. if (!$passport) {
  68. $this->json_error('openid 不能为空');
  69. }
  70. if (!$user) {
  71. //注册
  72. $user = new User();
  73. $data = [
  74. 'openid' => $post['openid']??'',
  75. 'unionid' => $post['unionid']??'',
  76. 'passport' => $passport,
  77. 'nickname' => limitStrLen('nickname', $post['nickname']??'', 50),
  78. 'user_type' => User::TYPE_WECHAT,
  79. 'user_cate' => User::CATE_USER,
  80. 'head_pic' => limitStrLen('headimgurl', $post['headimgurl']??'', 255),
  81. 'status' => User::STATUS_PASS,
  82. 'ip' => $this->request->ip(),
  83. 'sex' => $post['sex']??0,
  84. 'country' => $post['country']??'',
  85. 'province' => $post['province']??'',
  86. 'city' => $post['city']??'',
  87. "login_time" => time(),
  88. "create_time" => time()
  89. ];
  90. if (false == $user->allowField(true)->save($data)) {
  91. $this->json_error('添加失败');
  92. }
  93. } else {
  94. $data = [
  95. "login_time" => time(),
  96. 'openid' => $post['openid']??'',
  97. 'unionid' => $post['unionid']??'',
  98. 'head_pic' => limitStrLen('headimgurl', $post['headimgurl']??'', 255),
  99. ];
  100. $user->allowField(true)->save($data);
  101. }
  102. //登入成功 ,返回 前端
  103. $return_url = session('return_url', '', 'login');
  104. $this->redirect($return_url . '?jwt=' . Permissions::createJwt($user->id, $user->login_time, 3600 * 24));
  105. }
  106. }