MatchexchangeAddValidation.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace App\Module\AppGame\Validations;
  3. use App\Module\Mex\Validators\MexOrderNumberValidator;
  4. use App\Module\Mex\Validators\MexOrderValidator;
  5. use App\Module\Mex\Validators\MexItemValidator;
  6. use App\Module\Mex\Logic\FundLogic;
  7. use Uraus\Kku\Common\MEX_DIRECTION;
  8. /**
  9. * 农贸市场添加挂单验证类
  10. *
  11. * 用于验证添加挂单请求的参数,包括物品、价格、数量等验证
  12. */
  13. class MatchexchangeAddValidation extends ValidationBase
  14. {
  15. /**
  16. * 验证规则
  17. *
  18. * @param array $rules 自定义规则
  19. * @return array
  20. */
  21. public function rules($rules = []): array
  22. {
  23. return [
  24. // 基础参数验证
  25. [
  26. 'user_id,itemId,direction', 'required'
  27. ],
  28. [
  29. 'price', 'required',
  30. 'msg' => '必须输入价格'
  31. ],
  32. [
  33. 'num', 'required',
  34. 'msg' => '数量必须输入'
  35. ],
  36. [
  37. 'user_id,itemId', 'integer',
  38. 'msg' => '{attr}必须是大于0的整数'
  39. ],
  40. [
  41. 'num', 'integer', 'min' => 1, 'max' => 10000, 'msg' => '一次最多10000'
  42. ],
  43. ['price', 'float', 'min' => '0.00001',
  44. 'msg' => '价格必须是大于0的数字'
  45. ],
  46. // 验证交易方向(支持字符串和整数两种格式)
  47. [
  48. 'direction', 'in', 'range' => [MEX_DIRECTION::BUY, MEX_DIRECTION::SELL],
  49. 'msg' => '交易方向无效'
  50. ],
  51. // 验证物品是否可交易
  52. [
  53. 'itemId', new MexItemValidator($this),
  54. 'msg' => '该物品不支持在农贸市场交易'
  55. ],
  56. // 注意:根据文档要求,挂单阶段无价格验证,价格验证延后到撮合阶段
  57. // 移除价格验证器,只保留基本的数值验证
  58. // 验证订单创建条件(资金和物品数量验证)
  59. [
  60. 'direction', new MexOrderValidator($this, ['user_id', 'itemId', 'num', 'price', 'currency_type']),
  61. 'msg' => '订单创建条件不满足'
  62. ],
  63. [
  64. 'direction', new MexOrderNumberValidator($this, ['user_id', 'itemId', 'num', 'price', 'currency_type']),
  65. 'msg' => '订单数量过多'
  66. ]
  67. ];
  68. }
  69. /**
  70. * 设置默认值
  71. *
  72. * @return array
  73. */
  74. public function default(): array
  75. {
  76. return [];
  77. }
  78. /**
  79. * 数据预处理
  80. * 将 protobuf 枚举字符串转换为对应的整数值
  81. * 添加默认币种信息
  82. *
  83. * @return bool
  84. */
  85. public function beforeValidate(): bool
  86. {
  87. // 处理 direction 字段的转换
  88. $direction = $this->getRaw('direction');
  89. if (is_string($direction)) {
  90. // 将字符串枚举名转换为对应的整数值
  91. $directionValue = match (strtoupper($direction)) {
  92. 'SELL' => MEX_DIRECTION::SELL,
  93. 'BUY' => MEX_DIRECTION::BUY,
  94. 'DIRECTION_NONE' => MEX_DIRECTION::DIRECTION_NONE,
  95. default => $direction // 保持原值,让验证器处理
  96. };
  97. $this->setRaw('direction', $directionValue);
  98. }
  99. // 添加默认币种信息(钻石)用于验证
  100. // 由于protobuf请求中没有币种字段,我们使用默认币种
  101. $defaultCurrency = FundLogic::getDefaultCurrency();
  102. $this->setRaw('currency_type', $defaultCurrency->value);
  103. return true;
  104. }
  105. /**
  106. * 获取交易方向描述
  107. *
  108. * @return string
  109. */
  110. public function getDirectionText(): string
  111. {
  112. $direction = $this->getSafe('direction');
  113. return match ($direction) {
  114. MEX_DIRECTION::BUY => '买入',
  115. MEX_DIRECTION::SELL => '卖出',
  116. default => '未知'
  117. };
  118. }
  119. /**
  120. * 是否为买入订单
  121. *
  122. * @return bool
  123. */
  124. public function isBuyOrder(): bool
  125. {
  126. return $this->getSafe('direction') === MEX_DIRECTION::BUY;
  127. }
  128. /**
  129. * 是否为卖出订单
  130. *
  131. * @return bool
  132. */
  133. public function isSellOrder(): bool
  134. {
  135. return $this->getSafe('direction') === MEX_DIRECTION::SELL;
  136. }
  137. }