| 123456789101112131415161718192021222324252627282930313233343536373839 |
- <?php
- declare(strict_types=1);
- namespace app\miniapi\service;
- use app\miniapi\auth\TestPhoneIdentityProvider;
- use app\miniapi\exception\ApiException;
- use app\miniapi\http\Fields;
- use app\miniapi\model\MiniUser;
- final class TestPhoneLogin
- {
- public function assertSourceAllowed(string $remoteIp): void
- {
- if (!in_array($remoteIp, config('mini_api.test_phone_allowed_ips', []), true)) {
- throw new ApiException(40301, '测试手机号登录仅允许指定来源', 403);
- }
- }
- public function login(array $input): array
- {
- if (!TestPhoneIdentityProvider::enabled()) {
- throw new ApiException(50301, '测试手机号登录未开启', 503);
- }
- Fields::only($input, ['phone', 'code']);
- $phone = Fields::text($input, 'phone', 11);
- $code = Fields::text($input, 'code', 5);
- if (!in_array($phone, TestPhoneIdentityProvider::PHONES, true)) {
- throw new ApiException(40102, '测试手机号或验证码错误', 401);
- }
- $identity = (new TestPhoneIdentityProvider($phone))->exchange($code);
- if (!MiniUser::query()->where($identity)->exists()) {
- throw new ApiException(50301, '测试账号尚未初始化', 503);
- }
- return (new AuthService())->issue($identity);
- }
- }
|