TestPhoneLogin.php 989 B

12345678910111213141516171819202122232425262728293031
  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. if (!in_array($phone, TestPhoneIdentityProvider::PHONES, true)) {
  18. throw new ApiException(40102, '测试手机号错误', 401);
  19. }
  20. $identity = (new TestPhoneIdentityProvider($phone))->exchange('');
  21. if (!MiniUser::query()->where($identity)->exists()) {
  22. throw new ApiException(50301, '测试账号尚未初始化', 503);
  23. }
  24. return (new AuthService())->issue($identity);
  25. }
  26. }