TestPhoneIdentityProvider.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\miniapi\auth;
  4. use app\miniapi\exception\ApiException;
  5. /** Explicit 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.test_phone_login', false) === true;
  20. }
  21. public function exchange(string $code): array
  22. {
  23. if (!self::enabled()) {
  24. throw new ApiException(50301, '测试手机号登录未开启', 503);
  25. }
  26. if (!in_array($this->phone, self::PHONES, true) || !hash_equals('66666', $code)) {
  27. throw new ApiException(40102, '测试手机号或验证码错误', 401);
  28. }
  29. return ['appid' => self::APP_ID, 'openid' => $this->phone];
  30. }
  31. }