Discount.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace app\api\validate;
  3. use think\Validate;
  4. /**
  5. * 折扣API接口验证器
  6. */
  7. class Discount extends Validate
  8. {
  9. /**
  10. * 验证规则
  11. */
  12. protected $rule = [
  13. 'page' => 'integer|between:1,1000',
  14. 'pageSize' => 'integer|between:1,50',
  15. 'goods_ids' => 'array|checkGoodsIdsArray',
  16. 'goods_id' => 'require|integer|gt:0',
  17. 'sku_id' => 'integer|egt:0',
  18. ];
  19. /**
  20. * 提示消息
  21. */
  22. protected $message = [
  23. 'page.integer' => '页码必须为整数',
  24. 'page.between' => '页码必须在1-1000之间',
  25. 'pageSize.integer' => '每页数量必须为整数',
  26. 'pageSize.between' => '每页数量必须在1-50之间',
  27. 'goods_ids.require' => '商品ID不能为空',
  28. 'goods_ids.array' => '商品ID必须为数组格式',
  29. 'goods_id.require' => '商品ID不能为空',
  30. 'goods_id.integer' => '商品ID必须为整数',
  31. 'goods_id.gt' => '商品ID必须大于0',
  32. 'sku_id.integer' => 'SKU ID必须为整数',
  33. 'sku_id.egt' => 'SKU ID不能小于0',
  34. ];
  35. /**
  36. * 验证场景
  37. */
  38. protected $scene = [
  39. 'lists' => ['page', 'pageSize', 'goods_ids'],
  40. 'goods' => ['goods_id', 'sku_id'],
  41. 'batch' => ['goods_ids' => 'require|array|checkGoodsIdsArray'],
  42. ];
  43. /**
  44. * 自定义验证规则 - 验证商品ID数组格式
  45. */
  46. protected function checkGoodsIdsArray($value, $rule, $data = [])
  47. {
  48. // 允许为空数组
  49. if (empty($value)) {
  50. return true;
  51. }
  52. // 必须是数组
  53. if (!is_array($value)) {
  54. return '商品ID必须为数组格式';
  55. }
  56. // 检查数组中的每个值
  57. foreach ($value as $id) {
  58. if (!is_numeric($id) || intval($id) <= 0) {
  59. return '商品ID必须为正整数';
  60. }
  61. }
  62. // 检查数量限制
  63. if (count($value) > 100) {
  64. return '一次最多查询100个商品';
  65. }
  66. return true;
  67. }
  68. }