BusinessIdValidator.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace App\Module\Transfer\Validators;
  3. use App\Module\Transfer\Models\TransferApp;
  4. use App\Module\Transfer\Models\TransferOrder;
  5. use UCore\Validation\ValidatorCore;
  6. /**
  7. * 业务ID验证器
  8. */
  9. class BusinessIdValidator extends ValidatorCore
  10. {
  11. private string $businessId;
  12. private int $appId;
  13. public function __construct(string $businessId, int $appId)
  14. {
  15. $this->businessId = $businessId;
  16. $this->appId = $appId;
  17. }
  18. /**
  19. * 执行验证
  20. */
  21. public function validate(): bool
  22. {
  23. // 验证业务ID格式
  24. if (!$this->validateFormat()) {
  25. return false;
  26. }
  27. // 验证业务ID唯一性
  28. if (!$this->validateUniqueness()) {
  29. return false;
  30. }
  31. return true;
  32. }
  33. /**
  34. * 验证业务ID格式
  35. */
  36. private function validateFormat(): bool
  37. {
  38. // 检查长度
  39. if (strlen($this->businessId) < 3) {
  40. $this->addError('业务ID长度不能少于3个字符');
  41. return false;
  42. }
  43. if (strlen($this->businessId) > 100) {
  44. $this->addError('业务ID长度不能超过100个字符');
  45. return false;
  46. }
  47. // 检查字符格式(只允许字母、数字、下划线、中划线)
  48. if (!preg_match('/^[a-zA-Z0-9_-]+$/', $this->businessId)) {
  49. $this->addError('业务ID只能包含字母、数字、下划线和中划线');
  50. return false;
  51. }
  52. // 检查是否以字母或数字开头
  53. if (!preg_match('/^[a-zA-Z0-9]/', $this->businessId)) {
  54. $this->addError('业务ID必须以字母或数字开头');
  55. return false;
  56. }
  57. // 检查是否包含连续的特殊字符
  58. if (preg_match('/[_-]{2,}/', $this->businessId)) {
  59. $this->addError('业务ID不能包含连续的下划线或中划线');
  60. return false;
  61. }
  62. return true;
  63. }
  64. /**
  65. * 验证业务ID唯一性
  66. */
  67. private function validateUniqueness(): bool
  68. {
  69. // 获取应用配置
  70. $app = TransferApp::find($this->appId);
  71. if (!$app) {
  72. $this->addError('应用不存在');
  73. return false;
  74. }
  75. // 检查是否已存在相同的业务ID
  76. $existingOrder = TransferOrder::where('out_order_id', $this->businessId)
  77. ->where('out_id', $app->out_id2 ?? 0)
  78. ->first();
  79. if ($existingOrder) {
  80. $this->addError('业务ID已存在');
  81. return false;
  82. }
  83. return true;
  84. }
  85. /**
  86. * 生成建议的业务ID
  87. */
  88. public function generateSuggestion(): string
  89. {
  90. $prefix = 'TR';
  91. $timestamp = date('YmdHis');
  92. $random = strtoupper(substr(md5(uniqid()), 0, 6));
  93. return $prefix . $timestamp . $random;
  94. }
  95. /**
  96. * 验证业务ID是否可用
  97. */
  98. public static function isAvailable(string $businessId, int $appId): bool
  99. {
  100. $validator = new self($businessId, $appId);
  101. return $validator->validate();
  102. }
  103. /**
  104. * 批量验证业务ID
  105. */
  106. public static function validateBatch(array $businessIds, int $appId): array
  107. {
  108. $results = [];
  109. foreach ($businessIds as $businessId) {
  110. $validator = new self($businessId, $appId);
  111. $results[$businessId] = [
  112. 'valid' => $validator->validate(),
  113. 'error' => $validator->getError()
  114. ];
  115. }
  116. return $results;
  117. }
  118. }