123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- namespace app\admin\validate\shop;
- use think\Validate;
- class Goods extends Validate
- {
- /**
- * 验证规则
- */
- protected $rule = [
- 'goods_sn' => 'require|max:50',
- 'title' => 'require|max:200',
- 'sub_title' => 'max:200',
- 'category_ids' => 'require',
- 'image' => 'require',
- 'images' => 'require',
- 'weigh' => 'require|integer|egt:0',
- 'status' => 'require|in:0,1,2,3',
- 'delivery_type' => 'require|in:EXPRESS,PICKUP,VIRTUAL',
- 'express_type' => 'requireIf:delivery_type,EXPRESS|in:1,2,3',
- 'express_freight' => 'requireIf:express_type,2|float|egt:0',
- 'express_template_id' => 'requireIf:express_type,3|integer|gt:0',
- 'spec_type' => 'require|in:0,1',
- 'price' => 'requireIf:spec_type,0|float|gt:0|checkSinglePrice',
- 'lineation_price' => 'float|egt:0',
- 'cost_price' => 'float|egt:0',
- 'stocks' => 'requireIf:spec_type,0|integer|gt:0',
- 'weight' => 'float|egt:0',
- 'volume' => 'float|egt:0',
- 'scheduled_online_time' => 'requireIf:status,3',
- 'scheduled_offline_time' => 'requireIf:is_auto_offline,1',
- 'spec_data' => 'checkMultiSpec',
- ];
-
- /**
- * 提示消息
- */
- protected $message = [
- 'goods_sn.require' => '商品编码不能为空',
- 'goods_sn.max' => '商品编码不能超过50个字符',
- 'title.require' => '商品标题不能为空',
- 'title.max' => '商品标题不能超过200个字符',
- // 'sub_title.require' => '商品副标题不能为空',
- 'sub_title.max' => '商品副标题不能超过200个字符',
- 'category_ids.require' => '请选择商品分类',
- 'image.require' => '请上传商品主图',
- 'images.require' => '请上传商品轮播图',
- 'weigh.require' => '排序值不能为空',
- 'weigh.integer' => '排序值必须是整数',
- 'weigh.egt' => '排序值必须大于等于0',
- 'status.require' => '请选择商品状态',
- 'status.in' => '商品状态值不正确',
- 'delivery_type.require' => '请选择配送方式',
- 'delivery_type.in' => '配送方式值不正确',
- 'express_type.requireIf' => '选择快递配送时必须设置运费方式',
- 'express_type.in' => '运费方式值不正确',
- 'express_freight.requireIf' => '选择统一运费时必须设置运费金额',
- 'express_freight.float' => '运费金额必须是数字',
- 'express_freight.egt' => '运费金额必须大于等于0',
- 'express_template_id.requireIf' => '选择运费模板时必须选择模板',
- 'express_template_id.integer' => '运费模板ID必须是整数',
- 'express_template_id.gt' => '运费模板ID必须大于0',
- 'spec_type.require' => '请选择规格类型',
- 'spec_type.in' => '规格类型值不正确',
- 'price.requireIf' => '单规格商品必须设置销售价',
- 'price.float' => '销售价必须是数字',
- 'price.gt' => '销售价必须大于0',
- 'lineation_price.float' => '划线价必须是数字',
- 'lineation_price.egt' => '划线价必须大于等于0',
- 'cost_price.float' => '成本价必须是数字',
- 'cost_price.egt' => '成本价必须大于等于0',
- 'stocks.requireIf' => '单规格商品必须设置库存',
- 'stocks.integer' => '库存必须是整数',
- 'stocks.gt' => '库存必须大于0',
- 'weight.float' => '重量必须是数字',
- 'weight.egt' => '重量必须大于等于0',
- 'volume.float' => '体积必须是数字',
- 'volume.egt' => '体积必须大于等于0',
- 'scheduled_online_time.requireIf' => '定时上架必须设置上架时间',
- 'scheduled_offline_time.requireIf' => '自动下架必须设置下架时间',
- ];
-
- /**
- * 验证场景
- */
- protected $scene = [
- 'add' => [],
- 'edit' => [],
- ];
-
- /**
- * 自定义验证规则:多规格商品验证
- */
- protected function checkMultiSpec($value, $rule, $data)
- {
- if (isset($data['spec_type']) && $data['spec_type'] == '1') {
- // 多规格商品需要验证规格数据
- if (empty($data['spec_data']) || !is_array($data['spec_data'])) {
- return '多规格商品必须设置规格信息';
- }
-
- if (empty($data['sku_data']) || !is_array($data['sku_data'])) {
- return '多规格商品必须设置SKU信息';
- }
-
- // 验证规格名称不能为空
- foreach ($data['spec_data'] as $spec) {
- if (empty($spec['name'])) {
- return '规格名称不能为空';
- }
-
- if (empty($spec['value']) || !is_array($spec['value'])) {
- return '规格值不能为空';
- }
-
- foreach ($spec['value'] as $value) {
- if (empty($value['name'])) {
- return '规格值名称不能为空';
- }
- }
- }
-
- // 验证SKU数据
- foreach ($data['sku_data'] as $sku) {
- if (empty($sku['skus']) || !is_array($sku['skus'])) {
- return 'SKU规格属性不能为空';
- }
-
- if (!isset($sku['price']) || $sku['price'] <= 0) {
- return 'SKU销售价必须大于0';
- }
-
-
- if (!isset($sku['stocks']) || $sku['stocks'] <= 0) {
- return 'SKU库存必须大于0';
- }
- }
- }
-
- return true;
- }
-
- /**
- * 自定义验证规则:单规格商品价格验证
- */
- protected function checkSinglePrice($value, $rule, $data)
- {
- // 单规格商品价格验证已简化,只验证价格大于0
- return true;
- }
- }
|