JwtRequestValidation.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Module\OpenAPI\Validations;
  3. use App\Module\OpenAPI\Validators\AppExistenceValidator;
  4. use App\Module\OpenAPI\Validators\AppStatusValidator;
  5. use UCore\ValidationCore;
  6. /**
  7. * JWT令牌请求验证类
  8. */
  9. class JwtRequestValidation extends ValidationCore
  10. {
  11. /** @var \App\Module\OpenAPI\Models\OpenApiApp|null 应用对象,由 AppExistenceValidator 设置 */
  12. public ?\App\Module\OpenAPI\Models\OpenApiApp $app = null;
  13. /**
  14. * 验证规则
  15. */
  16. public function rules($rules = []): array
  17. {
  18. return [
  19. // 基础验证
  20. ['app_id', 'required'],
  21. ['app_secret', 'required'],
  22. ['user_id', 'integer', 'min' => 0],
  23. // 业务验证(按顺序执行)
  24. [
  25. 'app_id', new AppExistenceValidator($this, ['app_secret', 'app']),
  26. 'msg' => '应用不存在或密钥错误'
  27. ],
  28. [
  29. 'app_id', new AppStatusValidator($this, ['app']),
  30. 'msg' => '应用状态异常'
  31. ],
  32. ];
  33. }
  34. /**
  35. * 默认值
  36. */
  37. public function default(): array
  38. {
  39. return [
  40. 'user_id' => 0,
  41. ];
  42. }
  43. }