Goods.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace app\admin\validate\shop;
  3. use think\Validate;
  4. class Goods extends Validate
  5. {
  6. /**
  7. * 验证规则
  8. */
  9. protected $rule = [
  10. 'goods_sn' => 'require|max:50',
  11. 'title' => 'require|max:200',
  12. 'sub_title' => 'max:200',
  13. 'category_ids' => 'require',
  14. 'image' => 'require',
  15. 'images' => 'require',
  16. 'weigh' => 'require|integer|egt:0',
  17. 'status' => 'require|in:0,1,2,3',
  18. 'delivery_type' => 'require|in:EXPRESS,PICKUP,VIRTUAL',
  19. 'express_type' => 'requireIf:delivery_type,EXPRESS|in:1,2,3',
  20. 'express_freight' => 'requireIf:express_type,2|float|egt:0',
  21. 'express_template_id' => 'requireIf:express_type,3|integer|gt:0',
  22. 'spec_type' => 'require|in:0,1',
  23. 'price' => 'requireIf:spec_type,0|float|gt:0|checkSinglePrice',
  24. 'lineation_price' => 'float|egt:0',
  25. 'cost_price' => 'float|egt:0',
  26. 'stocks' => 'requireIf:spec_type,0|integer|gt:0',
  27. 'weight' => 'float|egt:0',
  28. 'volume' => 'float|egt:0',
  29. 'scheduled_online_time' => 'requireIf:status,3',
  30. 'scheduled_offline_time' => 'requireIf:is_auto_offline,1',
  31. 'spec_data' => 'checkMultiSpec',
  32. ];
  33. /**
  34. * 提示消息
  35. */
  36. protected $message = [
  37. 'goods_sn.require' => '商品编码不能为空',
  38. 'goods_sn.max' => '商品编码不能超过50个字符',
  39. 'title.require' => '商品标题不能为空',
  40. 'title.max' => '商品标题不能超过200个字符',
  41. // 'sub_title.require' => '商品副标题不能为空',
  42. 'sub_title.max' => '商品副标题不能超过200个字符',
  43. 'category_ids.require' => '请选择商品分类',
  44. 'image.require' => '请上传商品主图',
  45. 'images.require' => '请上传商品轮播图',
  46. 'weigh.require' => '排序值不能为空',
  47. 'weigh.integer' => '排序值必须是整数',
  48. 'weigh.egt' => '排序值必须大于等于0',
  49. 'status.require' => '请选择商品状态',
  50. 'status.in' => '商品状态值不正确',
  51. 'delivery_type.require' => '请选择配送方式',
  52. 'delivery_type.in' => '配送方式值不正确',
  53. 'express_type.requireIf' => '选择快递配送时必须设置运费方式',
  54. 'express_type.in' => '运费方式值不正确',
  55. 'express_freight.requireIf' => '选择统一运费时必须设置运费金额',
  56. 'express_freight.float' => '运费金额必须是数字',
  57. 'express_freight.egt' => '运费金额必须大于等于0',
  58. 'express_template_id.requireIf' => '选择运费模板时必须选择模板',
  59. 'express_template_id.integer' => '运费模板ID必须是整数',
  60. 'express_template_id.gt' => '运费模板ID必须大于0',
  61. 'spec_type.require' => '请选择规格类型',
  62. 'spec_type.in' => '规格类型值不正确',
  63. 'price.requireIf' => '单规格商品必须设置销售价',
  64. 'price.float' => '销售价必须是数字',
  65. 'price.gt' => '销售价必须大于0',
  66. 'lineation_price.float' => '划线价必须是数字',
  67. 'lineation_price.egt' => '划线价必须大于等于0',
  68. 'cost_price.float' => '成本价必须是数字',
  69. 'cost_price.egt' => '成本价必须大于等于0',
  70. 'stocks.requireIf' => '单规格商品必须设置库存',
  71. 'stocks.integer' => '库存必须是整数',
  72. 'stocks.gt' => '库存必须大于0',
  73. 'weight.float' => '重量必须是数字',
  74. 'weight.egt' => '重量必须大于等于0',
  75. 'volume.float' => '体积必须是数字',
  76. 'volume.egt' => '体积必须大于等于0',
  77. 'scheduled_online_time.requireIf' => '定时上架必须设置上架时间',
  78. 'scheduled_offline_time.requireIf' => '自动下架必须设置下架时间',
  79. ];
  80. /**
  81. * 验证场景
  82. */
  83. protected $scene = [
  84. 'add' => [],
  85. 'edit' => [],
  86. ];
  87. /**
  88. * 自定义验证规则:多规格商品验证
  89. */
  90. protected function checkMultiSpec($value, $rule, $data)
  91. {
  92. if (isset($data['spec_type']) && $data['spec_type'] == '1') {
  93. // 多规格商品需要验证规格数据
  94. if (empty($data['spec_data']) || !is_array($data['spec_data'])) {
  95. return '多规格商品必须设置规格信息';
  96. }
  97. if (empty($data['sku_data']) || !is_array($data['sku_data'])) {
  98. return '多规格商品必须设置SKU信息';
  99. }
  100. // 验证规格名称不能为空
  101. foreach ($data['spec_data'] as $spec) {
  102. if (empty($spec['name'])) {
  103. return '规格名称不能为空';
  104. }
  105. if (empty($spec['value']) || !is_array($spec['value'])) {
  106. return '规格值不能为空';
  107. }
  108. foreach ($spec['value'] as $value) {
  109. if (empty($value['name'])) {
  110. return '规格值名称不能为空';
  111. }
  112. }
  113. }
  114. // 验证SKU数据
  115. foreach ($data['sku_data'] as $sku) {
  116. if (empty($sku['skus']) || !is_array($sku['skus'])) {
  117. return 'SKU规格属性不能为空';
  118. }
  119. if (!isset($sku['price']) || $sku['price'] <= 0) {
  120. return 'SKU销售价必须大于0';
  121. }
  122. if (!isset($sku['stocks']) || $sku['stocks'] <= 0) {
  123. return 'SKU库存必须大于0';
  124. }
  125. }
  126. }
  127. return true;
  128. }
  129. /**
  130. * 自定义验证规则:单规格商品价格验证
  131. */
  132. protected function checkSinglePrice($value, $rule, $data)
  133. {
  134. // 单规格商品价格验证已简化,只验证价格大于0
  135. return true;
  136. }
  137. }