TestPhoneIdentityProvider.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\miniapi\auth;
  4. use app\miniapi\exception\ApiException;
  5. /** Explicit local fixture identities, never verified phone numbers or WeChat accounts. */
  6. final class TestPhoneIdentityProvider implements IdentityProvider
  7. {
  8. public const APP_ID = 'local-test-phone';
  9. public const PHONES = [
  10. 'enterprise_admin' => '13012345678',
  11. 'driver' => '13012345679',
  12. 'agent' => '13012345680',
  13. ];
  14. public function __construct(private readonly string $phone)
  15. {
  16. }
  17. public static function enabled(): bool
  18. {
  19. return config('mini_api.environment') === 'local'
  20. && config('mini_api.test_phone_login', false) === true;
  21. }
  22. public function exchange(string $code): array
  23. {
  24. if (!self::enabled()) {
  25. throw new ApiException(50301, '测试手机号登录未开启', 503);
  26. }
  27. if (!in_array($this->phone, self::PHONES, true) || !hash_equals('66666', $code)) {
  28. throw new ApiException(40102, '测试手机号或验证码错误', 401);
  29. }
  30. return ['appid' => self::APP_ID, 'openid' => $this->phone];
  31. }
  32. }