IntegralGoodsValidate.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace app\adminapi\validate\integral;
  3. use app\common\enum\IntegralGoodsEnum;
  4. use app\common\model\IntegralGoods;
  5. use app\common\validate\BaseValidate;
  6. /**
  7. * 积分商品验证
  8. * Class IntegralGoodsValidate
  9. * @package app\admin\validate\kefu
  10. */
  11. class IntegralGoodsValidate extends BaseValidate
  12. {
  13. protected $rule = [
  14. 'id'=>'require|checkGoods',
  15. 'type' => 'require|in:1,2',
  16. 'name' => 'require',
  17. 'image' => 'require',
  18. 'market_price' => 'checkMarketPrice',
  19. 'stock' => 'require|integer|gt:0',
  20. 'exchange_way' => 'requireIf:type,1',
  21. 'need_integral' => 'require|integer|checkNeedIntegral',
  22. 'need_money' => 'requireIf:exchange_way,2|checkNeedMoney',
  23. 'delivery_way' => 'requireIf:type,1',
  24. 'express_type' => 'requireIf:delivery_way,1',
  25. 'express_money' => 'requireIf:express_type,2|checkExpressMoney',
  26. 'balance' => 'requireIf:type,2|checkBalance',
  27. 'status' => 'require|in:0,1',
  28. ];
  29. protected $message = [
  30. 'id.require' => '参数缺失',
  31. 'type.require' => '请选择兑换类型',
  32. 'type.in' => '兑换类型错误',
  33. 'name.require' => '请填写商品名称',
  34. 'image.require' => '请上传商品封面',
  35. 'stock.require' => '请填写发放库存',
  36. 'stock.integer' => '请填写整数发放库存',
  37. 'stock.gt' => '发放库存需大于0',
  38. 'exchange_way.requireIf' => '请选择兑换方式',
  39. 'need_integral.require' => '请填写兑换积分',
  40. 'need_integral.integer' => '请填写整数兑换积分',
  41. 'need_money.requireIf' => '请填写兑换金额',
  42. 'delivery_way.requireIf' => '请选择物流配送方式',
  43. 'express_type.requireIf' => '请选择物流方式',
  44. 'express_money.requireIf' => '请填写运费',
  45. 'balance.requireIf' => '请填写红包面值',
  46. 'status.require' => '请选择商品状态',
  47. 'status.in' => '商品状态参数错误',
  48. ];
  49. /**
  50. * @notes 添加场景
  51. * @author 段誉
  52. * @date 2022/3/30 14:17
  53. */
  54. public function sceneAdd()
  55. {
  56. $this->remove('id', true);
  57. }
  58. /**
  59. * @notes 编辑场景
  60. * @author 段誉
  61. * @date 2022/3/30 14:17
  62. */
  63. public function sceneEdit()
  64. {
  65. }
  66. /**
  67. * @notes 删除场景
  68. * @author 段誉
  69. * @date 2022/3/30 16:02
  70. */
  71. public function sceneDel()
  72. {
  73. $this->only(['id']);
  74. }
  75. /**
  76. * @notes 详情场景
  77. * @author 段誉
  78. * @date 2022/3/30 16:02
  79. */
  80. public function sceneDetail()
  81. {
  82. $this->only(['id']);
  83. }
  84. /**
  85. * @notes 切换状态场景
  86. * @author 段誉
  87. * @date 2022/3/30 16:03
  88. */
  89. public function sceneStatus()
  90. {
  91. $this->only(['id', 'status']);
  92. }
  93. /**
  94. * @notes 验证商品
  95. * @param $value
  96. * @param $rule
  97. * @param $data
  98. * @return bool|string
  99. * @author 段誉
  100. * @date 2022/3/30 16:03
  101. */
  102. protected function checkGoods($value, $rule, $data)
  103. {
  104. $goods = IntegralGoods::where(['id' => $value])->findOrEmpty();
  105. if ($goods->isEmpty()) {
  106. return '积分商品不存在';
  107. }
  108. if ($goods['del'] == 1) {
  109. return '积分商品已被删除';
  110. }
  111. return true;
  112. }
  113. /**
  114. * @notes 验证运费
  115. * @param $value
  116. * @param $rule
  117. * @param $data
  118. * @return bool|string
  119. * @author 段誉
  120. * @date 2022/3/30 16:03
  121. */
  122. protected function checkExpressMoney($value, $rule, $data)
  123. {
  124. // 快递配送 统一运费 运费须大于0
  125. if ($data['delivery_way'] == IntegralGoodsEnum::DELIVERY_EXPRESS
  126. && $data['express_type'] == IntegralGoodsEnum::EXPRESS_TYPE_UNIFIED
  127. && $value <= 0
  128. ) {
  129. return '请输入大于0的运费';
  130. }
  131. return true;
  132. }
  133. /**
  134. * @notes 验证兑换积分
  135. * @param $value
  136. * @param $rule
  137. * @param $data
  138. * @return bool|string
  139. * @author 段誉
  140. * @date 2022/3/30 16:03
  141. */
  142. protected function checkNeedIntegral($value, $rule, $data)
  143. {
  144. if ($value <= 0) {
  145. return '请输入大于0的兑换积分';
  146. }
  147. return true;
  148. }
  149. /**
  150. * @notes 验证兑换方式为 积分+金额 时,金额不小于0
  151. * @param $value
  152. * @param $rule
  153. * @param $data
  154. * @return bool|string
  155. * @author 段誉
  156. * @date 2022/3/30 16:04
  157. */
  158. protected function checkNeedMoney($value, $rule, $data)
  159. {
  160. if ($data['exchange_way'] == IntegralGoodsEnum::EXCHANGE_WAY_HYBRID && $value <= 0) {
  161. return '请输入大于0的兑换金额';
  162. }
  163. return true;
  164. }
  165. /**
  166. * @notes 验证兑换类型为 红包时,红包面额不小于0
  167. * @param $value
  168. * @param $rule
  169. * @param $data
  170. * @return bool|string
  171. * @author 段誉
  172. * @date 2022/3/30 16:04
  173. */
  174. protected function checkBalance($value, $rule, $data)
  175. {
  176. if ($data['type'] == IntegralGoodsEnum::TYPE_BALANCE && $value <= 0) {
  177. return '请输入大于0的红包面值';
  178. }
  179. return true;
  180. }
  181. protected function checkMarketPrice($value)
  182. {
  183. if (!empty($value) && $value < 0) {
  184. return '请输入正确市场价';
  185. }
  186. return true;
  187. }
  188. }