Auth.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\User;
  5. use app\common\service\UserService;
  6. use think\exception\HttpResponseException;
  7. use think\Validate;
  8. use EasyWeChat\Factory;
  9. /**
  10. * 登录接口
  11. */
  12. class Auth extends Api
  13. {
  14. protected $noNeedLogin = ['wxLogin'];
  15. protected $noNeedRight = ['*'];
  16. /**
  17. * 手机号登录
  18. *
  19. * @param string $mobile 手机号
  20. * @param string $code 验证码
  21. */
  22. public function mobileLogin()
  23. {
  24. $mobile = $this->request->post('mobile');
  25. $code = $this->request->post('code');
  26. $validate = new Validate([
  27. 'mobile' => 'require|regex:/^1[3-9]\d{9}$/',
  28. 'code' => 'require|number|length:6'
  29. ]);
  30. if (!$validate->check(['mobile' => $mobile, 'code' => $code])) {
  31. $this->error($validate->getError());
  32. }
  33. // 验证码校验逻辑 - 此处需要和您的验证码发送系统对接
  34. if (!validate_sms_code($mobile, $code)) {
  35. $this->error('验证码错误');
  36. }
  37. $user = User::where('mobile', $mobile)->find();
  38. if (!$user) {
  39. // 新用户自动注册
  40. $user = new User;
  41. $user->mobile = $mobile;
  42. $user->nickname = substr($mobile, 0, 3) . '****' . substr($mobile, -4);
  43. $user->save();
  44. }
  45. $this->auth->direct($user->id);
  46. $this->success('登录成功', ['userinfo' => $user, 'token' => $this->auth->getToken()]);
  47. }
  48. /**
  49. * 微信小程序登录
  50. */
  51. public function wxLogin(): void
  52. {
  53. try {
  54. $code = $this->request->post('code');
  55. if (!$code) {
  56. $this->error('参数错误');
  57. }
  58. $user = UserService::wxLogin($code);
  59. $this->auth->direct($user->id);
  60. $userInfo = $this->auth->getUserinfo();
  61. $this->success('登录成功', ['userinfo' => $this->auth->getUserinfo(), 'token' => $userInfo['token']]);
  62. } catch (\Exception $e) {
  63. if ($e instanceof HttpResponseException) {
  64. throw $e;
  65. }
  66. $this->error('微信登录异常:' . $e->getMessage());
  67. }
  68. }
  69. /**
  70. * 微信小程序更新用户信息
  71. */
  72. public function updateUserInfo()
  73. {
  74. try {
  75. $userInfo = $this->request->post();
  76. if (!$this->auth->isLogin()) {
  77. $this->error('请先登录');
  78. }
  79. $user = $this->auth->getUser();
  80. $updateData = array_filter([
  81. 'nickname' => $userInfo['nickname'] ?? '',
  82. 'avatar' => $userInfo['avatar'] ?? '',
  83. 'gender' => $userInfo['gender'] ?? '',
  84. 'mobile' => $userInfo['mobile'] ?? '',
  85. ]);
  86. if ($updateData && $user->save($updateData)) {
  87. $this->success('更新成功');
  88. } else {
  89. $this->error('更新失败');
  90. }
  91. } catch (\Exception $e) {
  92. if ($e instanceof HttpResponseException) {
  93. throw $e;
  94. }
  95. $this->error('微信登录异常:' . $e->getMessage());
  96. }
  97. }
  98. }