123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- <?php
- namespace app\admin\validate\shop;
- use think\Validate;
- class Goods extends Validate
- {
- /**
- * 验证规则
- */
- protected $rule = [
- // 基础信息
- 'type' => 'require|in:1,2,3,4',
- 'goods_sn' => 'require|max:50|unique:shop_goods,goods_sn',
- 'title' => 'require|max:200',
- 'subtitle' => 'max:200',
- 'category_id' => 'require|integer|gt:0',
- 'category_ids' => 'require',
- 'brand_id' => 'integer|egt:0',
- 'image' => 'require',
- 'images' => 'require',
- 'weigh' => 'integer|egt:0',
-
- // 价格库存
- 'spec_type' => 'require|in:0,1',
- 'price' => 'requireIf:spec_type,0|float|gt:0',
- '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',
- 'sku_sn' => 'max:100',
- 'single_image' => '',
-
- // 配送设置
- 'delivery_type' => 'require|in:EXPRESS,PICKUP,VIRTUAL',
- 'express_type' => 'requireIf:delivery_type,EXPRESS|in:1,2,3',
- 'express_price' => 'requireIf:express_type,2|float|egt:0',
- 'express_template_id' => 'requireIf:express_type,3|integer|gt:0',
-
- // 销售设置
- 'online_type' => 'require|in:1,2,3',
- 'stock_show_type' => 'require|in:1,2',
- 'sales_show_type' => 'require|in:1,2',
- 'scheduled_online_time' => 'requireIf:online_type,3',
- 'scheduled_offline_time' => 'requireIf:is_auto_offline,1',
- 'is_auto_offline' => 'in:0,1',
- 'keywords' => 'max:255',
- 'description' => 'max:500',
-
- // 多规格相关
- 'spec_data' => 'checkMultiSpec',
- 'sku_data' => 'checkSkuData',
- ];
-
- /**
- * 提示消息
- */
- protected $message = [
- // 基础信息
- 'type.require' => '请选择商品类型',
- 'type.in' => '商品类型值不正确',
- 'goods_sn.require' => '商品编码不能为空',
- 'goods_sn.max' => '商品编码不能超过50个字符',
- 'goods_sn.unique' => '商品编码已存在',
- 'title.require' => '商品标题不能为空',
- 'title.max' => '商品标题不能超过200个字符',
- 'subtitle.max' => '商品副标题不能超过200个字符',
- 'category_id.require' => '请选择商品分类',
- 'category_id.integer' => '商品分类ID必须是整数',
- 'category_id.gt' => '请选择有效的商品分类',
- 'category_ids.require' => '请选择商品分类',
- 'brand_id.integer' => '品牌ID必须是整数',
- 'brand_id.egt' => '品牌ID必须大于等于0',
- 'image.require' => '请上传商品主图',
- 'images.require' => '请上传商品轮播图',
- 'weigh.integer' => '排序值必须是整数',
- 'weigh.egt' => '排序值必须大于等于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',
- 'sku_sn.max' => 'SKU编码不能超过100个字符',
-
- // 配送设置
- 'delivery_type.require' => '请选择配送方式',
- 'delivery_type.in' => '配送方式值不正确',
- 'express_type.requireIf' => '选择快递配送时必须设置运费方式',
- 'express_type.in' => '运费方式值不正确',
- 'express_price.requireIf' => '选择统一运费时必须设置运费金额',
- 'express_price.float' => '运费金额必须是数字',
- 'express_price.egt' => '运费金额必须大于等于0',
- 'express_template_id.requireIf' => '选择运费模板时必须选择模板',
- 'express_template_id.integer' => '运费模板ID必须是整数',
- 'express_template_id.gt' => '运费模板ID必须大于0',
-
- // 销售设置
- 'online_type.require' => '请选择上架方式',
- 'online_type.in' => '上架方式值不正确',
- 'stock_show_type.require' => '请选择库存显示方式',
- 'stock_show_type.in' => '库存显示方式值不正确',
- 'sales_show_type.require' => '请选择销量显示方式',
- 'sales_show_type.in' => '销量显示方式值不正确',
- 'scheduled_online_time.requireIf' => '定时上架必须设置上架时间',
- 'scheduled_offline_time.requireIf' => '自动下架必须设置下架时间',
- 'is_auto_offline.in' => '自动下架设置值不正确',
- 'keywords.max' => '关键词不能超过255个字符',
- 'description.max' => '描述不能超过500个字符',
- ];
-
- /**
- * 验证场景
- */
- protected $scene = [
- 'add' => [
- 'type', 'goods_sn', 'title', 'subtitle', 'category_id', 'category_ids', 'brand_id',
- 'image', 'images', 'weigh', 'spec_type', 'price', 'lineation_price', 'cost_price',
- 'stocks', 'weight', 'volume', 'sku_sn', 'single_image', 'delivery_type',
- 'express_type', 'express_price', 'express_template_id', 'online_type',
- 'stock_show_type', 'sales_show_type', 'scheduled_online_time', 'scheduled_offline_time',
- 'is_auto_offline', 'keywords', 'description', 'spec_data', 'sku_data'
- ],
- 'edit' => [
- 'type', 'goods_sn', 'title', 'subtitle', 'category_id', 'category_ids', 'brand_id',
- 'image', 'images', 'weigh', 'spec_type', 'price', 'lineation_price', 'cost_price',
- 'stocks', 'weight', 'volume', 'sku_sn', 'single_image', 'delivery_type',
- 'express_type', 'express_price', 'express_template_id', 'online_type',
- 'stock_show_type', 'sales_show_type', 'scheduled_online_time', 'scheduled_offline_time',
- 'is_auto_offline', 'keywords', 'description', 'spec_data', 'sku_data'
- ],
- ];
-
- /**
- * 字段描述
- */
- protected $field = [
- 'type' => '商品类型',
- 'goods_sn' => '商品编码',
- 'title' => '商品标题',
- 'subtitle' => '商品副标题',
- 'category_id' => '商品分类',
- 'category_ids' => '商品分类',
- 'brand_id' => '品牌',
- 'image' => '商品主图',
- 'images' => '商品轮播图',
- 'spec_type' => '规格类型',
- 'price' => '销售价',
- 'lineation_price' => '划线价',
- 'cost_price' => '成本价',
- 'stocks' => '库存',
- 'weight' => '重量',
- 'volume' => '体积',
- 'delivery_type' => '配送方式',
- 'express_type' => '运费方式',
- 'express_price' => '运费金额',
- 'express_template_id' => '运费模板',
- 'online_type' => '上架方式',
- 'stock_show_type' => '库存显示方式',
- 'sales_show_type' => '销量显示方式',
- 'scheduled_online_time' => '定时上架时间',
- 'scheduled_offline_time' => '定时下架时间',
- 'keywords' => '关键词',
- 'description' => '描述',
- ];
-
- /**
- * 自定义验证规则:多规格商品验证
- */
- 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 '多规格商品必须设置规格信息';
- }
-
- // 验证规格名称不能为空
- foreach ($data['spec_data'] as $key => $spec) {
- if (empty($spec['name'])) {
- return "规格#{$key}名称不能为空";
- }
-
- if (empty($spec['value']) || !is_array($spec['value'])) {
- return "规格「{$spec['name']}」的规格值不能为空";
- }
-
- foreach ($spec['value'] as $idx => $specValue) {
- if (empty($specValue['name'])) {
- return "规格「{$spec['name']}」的第" . ($idx + 1) . "个规格值名称不能为空";
- }
-
- // 检查定制规格是否上传了图片
- if (isset($spec['type']) && $spec['type'] === 'custom') {
- if (empty($specValue['image'])) {
- return "定制规格「{$spec['name']}」的规格值「{$specValue['name']}」必须上传图片";
- }
- }
- }
- }
- }
-
- return true;
- }
-
- /**
- * 自定义验证规则:SKU数据验证
- */
- protected function checkSkuData($value, $rule, $data)
- {
- if (isset($data['spec_type']) && $data['spec_type'] == '1') {
- // 多规格商品需要验证SKU数据
- if (empty($data['sku_data']) || !is_array($data['sku_data'])) {
- return '多规格商品必须设置SKU信息';
- }
-
- $hasValidSku = false;
-
- // 验证SKU数据
- foreach ($data['sku_data'] as $index => $sku) {
- // 检查显示状态的SKU
- if (isset($sku['status']) && ($sku['status'] == 1 || $sku['status'] === '1' || $sku['status'] === true)) {
- $hasValidSku = true;
-
- if (empty($sku['skus']) || !is_array($sku['skus'])) {
- return "第" . ($index + 1) . "个SKU的规格属性不能为空";
- }
-
- if (!isset($sku['price']) || floatval($sku['price']) <= 0) {
- return "第" . ($index + 1) . "个SKU的销售价必须大于0";
- }
-
- if (!isset($sku['stocks']) || intval($sku['stocks']) <= 0) {
- return "第" . ($index + 1) . "个SKU的库存必须大于0";
- }
- }
- }
-
- if (!$hasValidSku) {
- return '多规格商品至少需要一个有效的SKU';
- }
- }
-
- return true;
- }
- }
|