AuthenticatedService.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. * @Name
  4. * @Description
  5. * @Author 刘学玺
  6. * @Date 2024/9/26 12:11
  7. */
  8. namespace App\Http\Services\Frontend\Client\Auth;
  9. use App\Exceptions\ApiException;
  10. use App\Http\Services\Frontend\Client\Common\AuthService;
  11. use App\Http\Services\Frontend\Client\Common\SmsService;
  12. use App\Http\Services\Service;
  13. use App\Models\Member\User;
  14. class AuthenticatedService extends Service
  15. {
  16. /**
  17. * @throws ApiException
  18. */
  19. public function login(array $data)
  20. {
  21. // 手机号
  22. $mobile = $data['mobile'];
  23. // 手机验证码
  24. $code = $data['code'];
  25. // 判断验证码
  26. $verifyCodeResult = (new SmsService())->verifyCode($mobile, $code);
  27. !$verifyCodeResult && self::error('验证码错误!', 400);
  28. // 判断手机号
  29. $where = ['mobile' => $data['mobile']];
  30. $userQuery = User::query();
  31. $user = $userQuery->where($where)->first();
  32. !$user && self::error('账户不存在!', 400);
  33. $token = (new AuthService())->store($user);
  34. return ['token' => $token];
  35. }
  36. /**
  37. * @throws ApiException
  38. */
  39. public function send($data): void
  40. {
  41. // 手机号
  42. $mobile = $data['mobile'];
  43. $category = $data['category'] ?? 1;
  44. $sms = new SmsService();
  45. $sms->send($mobile, $category);
  46. }
  47. }