StoreSeckillProductValidate.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace app\validate\merchant;
  3. use think\File;
  4. use think\Validate;
  5. class StoreSeckillProductValidate extends Validate
  6. {
  7. protected $failException = true;
  8. protected $rule = [
  9. "image|主图" => 'require|max:128',
  10. "slider_image|轮播图" => 'require',
  11. "store_name|商品名称" => 'require|max:128',
  12. "brand_id|品牌ID" => 'require',
  13. "cate_id|平台分类" => 'require',
  14. "mer_cate_id|商户分类" => 'array',
  15. "unit_name|单位名" => 'require|max:4',
  16. "temp_id|运费模板" => 'require',
  17. "spec_type" => "in:0,1",
  18. "is_show|是否上架" => "in:0,1",
  19. "start_day|开始日期" => "require",
  20. "end_day|结束日期" => "require",
  21. "start_time|开始时间" => "require",
  22. "end_time|结束时间" => "require",
  23. "old_product_id|原商品ID" => 'require',
  24. "once_pay_count|单场限购" => 'require',
  25. "all_pay_count|限购总数" => 'require',
  26. "attr|商品规格" => "requireIf:spec_type,1|Array|checkUnique",
  27. "attrValue|商品属性" => "Array|productAttrValue"
  28. ];
  29. protected function productAttrValue($value,$rule,$data)
  30. {
  31. $arr = [];
  32. foreach ($value as $v){
  33. $sku = '';
  34. if(isset($v['detail']) && is_array($v['detail'])){
  35. sort($v['detail'],SORT_STRING);
  36. $sku = implode(',',$v['detail']);
  37. }
  38. if($v['stock'] < 1) return '限量不能小于1';
  39. if(in_array($sku,$arr)) return '商品SKU重复';
  40. $arr[] = $sku;
  41. }
  42. return true;
  43. }
  44. public function checkUnique($value)
  45. {
  46. $arr = [];
  47. foreach ($value as $item){
  48. if(in_array($item['value'],$arr)) return '规格重复';
  49. $arr[] = $item['value'];
  50. if (count($item['detail']) != count(array_unique($item['detail']))) return '属性重复';
  51. }
  52. return true;
  53. }
  54. }