123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- namespace app\api\validate;
- use think\Validate;
- /**
- * 折扣API接口验证器
- */
- class Discount extends Validate
- {
- /**
- * 验证规则
- */
- protected $rule = [
- 'page' => 'integer|between:1,1000',
- 'pageSize' => 'integer|between:1,50',
- 'goods_ids' => 'array|checkGoodsIdsArray',
- 'goods_id' => 'require|integer|gt:0',
- 'sku_id' => 'integer|egt:0',
- ];
- /**
- * 提示消息
- */
- protected $message = [
- 'page.integer' => '页码必须为整数',
- 'page.between' => '页码必须在1-1000之间',
- 'pageSize.integer' => '每页数量必须为整数',
- 'pageSize.between' => '每页数量必须在1-50之间',
- 'goods_ids.require' => '商品ID不能为空',
- 'goods_ids.array' => '商品ID必须为数组格式',
- 'goods_id.require' => '商品ID不能为空',
- 'goods_id.integer' => '商品ID必须为整数',
- 'goods_id.gt' => '商品ID必须大于0',
- 'sku_id.integer' => 'SKU ID必须为整数',
- 'sku_id.egt' => 'SKU ID不能小于0',
- ];
- /**
- * 验证场景
- */
- protected $scene = [
- 'lists' => ['page', 'pageSize', 'goods_ids'],
- 'goods' => ['goods_id', 'sku_id'],
- 'batch' => ['goods_ids' => 'require|array|checkGoodsIdsArray'],
- ];
- /**
- * 自定义验证规则 - 验证商品ID数组格式
- */
- protected function checkGoodsIdsArray($value, $rule, $data = [])
- {
- // 允许为空数组
- if (empty($value)) {
- return true;
- }
- // 必须是数组
- if (!is_array($value)) {
- return '商品ID必须为数组格式';
- }
- // 检查数组中的每个值
- foreach ($value as $id) {
- if (!is_numeric($id) || intval($id) <= 0) {
- return '商品ID必须为正整数';
- }
- }
- // 检查数量限制
- if (count($value) > 100) {
- return '一次最多查询100个商品';
- }
- return true;
- }
- }
|