TestPhoneLogin.php 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\miniapi\service;
  4. use app\miniapi\auth\TestPhoneIdentityProvider;
  5. use app\miniapi\exception\ApiException;
  6. use app\miniapi\http\Fields;
  7. use app\miniapi\model\MiniUser;
  8. final class TestPhoneLogin
  9. {
  10. public function login(array $input): array
  11. {
  12. if (!TestPhoneIdentityProvider::enabled()) {
  13. throw new ApiException(50301, '测试手机号登录未开启', 503);
  14. }
  15. Fields::only($input, ['phone', 'code']);
  16. $phone = Fields::text($input, 'phone', 11);
  17. $code = Fields::text($input, 'code', 5);
  18. if (!in_array($phone, TestPhoneIdentityProvider::PHONES, true)) {
  19. throw new ApiException(40102, '测试手机号或验证码错误', 401);
  20. }
  21. $identity = (new TestPhoneIdentityProvider($phone))->exchange($code);
  22. if (!MiniUser::query()->where($identity)->exists()) {
  23. throw new ApiException(50301, '测试账号尚未初始化', 503);
  24. }
  25. return (new AuthService())->issue($identity);
  26. }
  27. }