StoreProductValidate.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\validate\merchant;
  12. use think\Exception;
  13. use think\File;
  14. use think\Validate;
  15. class StoreProductValidate extends Validate
  16. {
  17. protected $failException = true;
  18. protected $rule = [
  19. "image|主图" => 'require|max:128',
  20. "store_name|商品名称" => 'require|max:128',
  21. "cate_id|平台分类" => 'require',
  22. "mer_cate_id|商户分类" => 'array',
  23. "unit_name|单位名" => 'require|max:4',
  24. "spec_type" => "in:0,1",
  25. "is_show|是否上架" => "in:0,1",
  26. "extension_type|分销类型" => "in:0,1",
  27. "attr|商品规格" => "requireIf:spec_type,1|Array|checkUnique",
  28. "attrValue|商品属性" => "require|array|productAttrValue",
  29. 'type|商品类型' => 'require|in:0,1',
  30. 'delivery_way|发货方式' => 'requireIf:is_ficti,0|require',
  31. 'once_min_count|最小限购' => 'min:0',
  32. 'pay_limit|是否限购' => 'require|in:0,1,2|payLimit',
  33. ];
  34. protected function payLimit($value,$rule,$data)
  35. {
  36. if ($value && ($data['once_max_count'] < $data['once_min_count']))
  37. return '限购数量不能小于最少购买件数';
  38. return true;
  39. }
  40. protected function productAttrValue($value,$rule,$data)
  41. {
  42. $arr = [];
  43. try{
  44. foreach ($value as $v){
  45. $sku = '';
  46. if(isset($v['detail']) && is_array($v['detail'])){
  47. sort($v['detail'],SORT_STRING);
  48. $sku = implode(',',$v['detail']);
  49. }
  50. if(in_array($sku,$arr)) return '商品SKU重复';
  51. $arr[] = $sku;
  52. if(isset($data['extension_type']) && $data['extension_type'] && systemConfig('extension_status')){
  53. if(!isset($v['extension_one']) || !isset($v['extension_two'])) return '佣金金额必须填写';
  54. if(($v['extension_one'] < 0) || ($v['extension_two'] < 0))
  55. return '佣金金额不可存在负数';
  56. if($v['price'] < bcadd($v['extension_one'],$v['extension_two'],2))
  57. return '自定义佣金总金额不能大于商品售价';
  58. }
  59. }
  60. } catch (\Exception $exception) {
  61. return '商品属性格式错误';
  62. }
  63. return true;
  64. }
  65. protected function checkUnique($value)
  66. {
  67. $arr = [];
  68. foreach ($value as $item){
  69. if(in_array($item['value'],$arr)) return '规格重复';
  70. $arr[] = $item['value'];
  71. if (count($item['detail']) != count(array_unique($item['detail']))) return '属性重复';
  72. }
  73. return true;
  74. }
  75. }