TransferInValidation.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace App\Module\Transfer\Validations;
  3. use App\Module\Transfer\Validators\TransferAppValidator;
  4. use App\Module\Transfer\Validators\BusinessIdValidator;
  5. use App\Module\Transfer\Validators\AmountValidator;
  6. use UCore\Validation\ValidationCore;
  7. /**
  8. * 转入验证类
  9. */
  10. class TransferInValidation extends ValidationCore
  11. {
  12. /**
  13. * 验证规则
  14. */
  15. protected function rules(): array
  16. {
  17. return [
  18. 'transfer_app_id' => 'required|integer|min:1',
  19. 'business_id' => 'required|string|max:100',
  20. 'out_user_id' => 'nullable|string|max:50',
  21. 'user_id' => 'required|integer|min:1',
  22. 'amount' => 'required|string',
  23. 'remark' => 'nullable|string|max:255',
  24. 'callback_data' => 'nullable|array',
  25. ];
  26. }
  27. /**
  28. * 验证消息
  29. */
  30. protected function messages(): array
  31. {
  32. return [
  33. 'transfer_app_id.required' => '应用ID不能为空',
  34. 'transfer_app_id.integer' => '应用ID必须为整数',
  35. 'transfer_app_id.min' => '应用ID必须大于0',
  36. 'business_id.required' => '业务订单ID不能为空',
  37. 'business_id.string' => '业务订单ID必须为字符串',
  38. 'business_id.max' => '业务订单ID长度不能超过100个字符',
  39. 'out_user_id.string' => '外部用户ID必须为字符串',
  40. 'out_user_id.max' => '外部用户ID长度不能超过50个字符',
  41. 'user_id.required' => '用户ID不能为空',
  42. 'user_id.integer' => '用户ID必须为整数',
  43. 'user_id.min' => '用户ID必须大于0',
  44. 'amount.required' => '金额不能为空',
  45. 'amount.string' => '金额必须为字符串格式',
  46. 'remark.string' => '备注必须为字符串',
  47. 'remark.max' => '备注长度不能超过255个字符',
  48. 'callback_data.array' => '回调数据必须为数组格式',
  49. ];
  50. }
  51. /**
  52. * 自定义验证
  53. */
  54. protected function customValidation(): void
  55. {
  56. // 验证应用配置
  57. $appValidator = new TransferAppValidator($this->data['transfer_app_id'] ?? 0);
  58. if (!$appValidator->validate()) {
  59. $this->addError('transfer_app_id', $appValidator->getError());
  60. }
  61. // 验证业务ID唯一性
  62. if (isset($this->data['business_id']) && isset($this->data['transfer_app_id'])) {
  63. $businessIdValidator = new BusinessIdValidator(
  64. $this->data['business_id'],
  65. $this->data['transfer_app_id']
  66. );
  67. if (!$businessIdValidator->validate()) {
  68. $this->addError('business_id', $businessIdValidator->getError());
  69. }
  70. }
  71. // 验证金额格式
  72. if (isset($this->data['amount'])) {
  73. $amountValidator = new AmountValidator($this->data['amount']);
  74. if (!$amountValidator->validate()) {
  75. $this->addError('amount', $amountValidator->getError());
  76. }
  77. }
  78. // 验证用户是否存在
  79. if (isset($this->data['user_id'])) {
  80. $userId = $this->data['user_id'];
  81. // 这里可以调用用户模块的验证服务
  82. // if (!UserService::exists($userId)) {
  83. // $this->addError('user_id', '用户不存在');
  84. // }
  85. }
  86. // 验证回调数据格式
  87. if (isset($this->data['callback_data']) && is_array($this->data['callback_data'])) {
  88. $callbackData = $this->data['callback_data'];
  89. // 检查回调数据大小(限制为1KB)
  90. $jsonSize = strlen(json_encode($callbackData, JSON_UNESCAPED_UNICODE));
  91. if ($jsonSize > 1024) {
  92. $this->addError('callback_data', '回调数据过大,不能超过1KB');
  93. }
  94. // 检查是否包含敏感字段
  95. $forbiddenKeys = ['password', 'token', 'secret', 'key'];
  96. foreach ($forbiddenKeys as $key) {
  97. if (array_key_exists($key, $callbackData)) {
  98. $this->addError('callback_data', "回调数据不能包含敏感字段: {$key}");
  99. break;
  100. }
  101. }
  102. }
  103. }
  104. /**
  105. * 验证转入权限
  106. */
  107. protected function validateTransferInPermission(): void
  108. {
  109. if (!isset($this->data['transfer_app_id'])) {
  110. return;
  111. }
  112. $appId = $this->data['transfer_app_id'];
  113. // 获取应用配置
  114. $app = \App\Module\Transfer\Models\TransferApp::find($appId);
  115. if (!$app) {
  116. return;
  117. }
  118. // 检查应用是否支持转入
  119. if (!$app->supportsTransferIn()) {
  120. $this->addError('transfer_app_id', '该应用不支持转入操作');
  121. }
  122. // 检查应用是否启用
  123. if (!$app->is_enabled) {
  124. $this->addError('transfer_app_id', '应用已禁用');
  125. }
  126. }
  127. /**
  128. * 执行验证后的处理
  129. */
  130. protected function afterValidation(): void
  131. {
  132. // 验证转入权限
  133. $this->validateTransferInPermission();
  134. // 格式化金额(确保精度)
  135. if (isset($this->data['amount'])) {
  136. $this->data['amount'] = number_format((float) $this->data['amount'], 10, '.', '');
  137. }
  138. // 清理回调数据
  139. if (isset($this->data['callback_data']) && is_array($this->data['callback_data'])) {
  140. // 移除空值
  141. $this->data['callback_data'] = array_filter($this->data['callback_data'], function ($value) {
  142. return $value !== null && $value !== '';
  143. });
  144. }
  145. }
  146. }