PassportController.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Api\v1;
  4. use App\Controller\AbstractController;
  5. use App\Model\Arts\SmsCodeModel;
  6. use App\Model\Arts\UserModel;
  7. use App\Request\Api\v1\Passport\LoginMobileRequest;
  8. use App\Utils\AppResult;
  9. use App\Utils\Encrypt\TokenFast;
  10. use Hyperf\Stringable\Str;
  11. /**
  12. * 通行证
  13. * 示例
  14. */
  15. class PassportController extends AbstractController
  16. {
  17. // 日志模块名称
  18. const LOG_MODULE = 'v1/PassportController';
  19. /**
  20. * 手机号登录注册
  21. * @param LoginMobileRequest $request
  22. * @return \Psr\Http\Message\MessageInterface|\Psr\Http\Message\ResponseInterface
  23. * @throws \Psr\Container\ContainerExceptionInterface
  24. * @throws \Psr\Container\NotFoundExceptionInterface
  25. */
  26. public function login_mobile(LoginMobileRequest $request)
  27. {
  28. $params = $request->validated();
  29. // 校验验证码
  30. $sms = new SmsCodeModel();
  31. if (!$sms->check($params['mobile'], $params['captcha'], $params['event'], 50)) {
  32. return AppResult::error($sms->getMessage());
  33. }
  34. $UserModel = new UserModel();
  35. if ($user = $UserModel->getByMobile($params['mobile'])){
  36. if ($user['status'] != 1){
  37. return AppResult::error('账号被锁定,无法登录');
  38. }
  39. }else{
  40. if (!$UserModel->register(mobile: $params['mobile'])){
  41. return AppResult::error('注册失败');
  42. }
  43. $user = $UserModel->getData();
  44. }
  45. $token = (string)Str::uuid();
  46. if (!TokenFast::set($token,$user['id'],2592000)){
  47. return AppResult::error('获取token失败');
  48. }
  49. $sms->flush($params['mobile'],$params['event']);
  50. return AppResult::success('登录成功',[
  51. 'token' => $token
  52. ]);
  53. }
  54. }