StoreProductValidate.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\File;
  13. use think\Validate;
  14. class StoreProductValidate extends Validate
  15. {
  16. protected $failException = true;
  17. protected $rule = [
  18. "image|主图" => 'require|max:128',
  19. "slider_image|轮播图" => 'require',
  20. "store_name|商品名称" => 'require|max:128',
  21. "brand_id|品牌ID" => 'require',
  22. "cate_id|平台分类" => 'require',
  23. "mer_cate_id|商户分类" => 'array',
  24. "unit_name|单位名" => 'require|max:4',
  25. "temp_id|运费模板" => 'require',
  26. "spec_type" => "in:0,1",
  27. "is_show|是否商家" => "in:0,1",
  28. "extension_type" => "in:0,1",
  29. "attr|商品规格" => "requireIf:spec_type,1|Array|checkUnique",
  30. "attrValue|商品属性" => "Array|productAttrValue"
  31. ];
  32. protected function productAttrValue($value,$rule,$data)
  33. {
  34. $arr = [];
  35. $extension_one_rate = systemConfig('extension_one_rate');
  36. $extension_two_rate = systemConfig('extension_two_rate');
  37. foreach ($value as $v){
  38. $sku = '';
  39. if(isset($v['detail']) && is_array($v['detail'])){
  40. sort($v['detail'],SORT_STRING);
  41. $sku = implode(',',$v['detail']);
  42. }
  43. if(in_array($sku,$arr)) return '商品SKU重复';
  44. $arr[] = $sku;
  45. if(isset($data['extension_type']) && $data['extension_type'] && systemConfig('extension_status')){
  46. if(!isset($v['extension_one']) || !isset($v['extension_two'])) return '佣金比例必须填写';
  47. if(($v['extension_one'] < 0) || ($v['extension_two'] < 0))
  48. return '不可存在负数';
  49. if($v['price'] < bcadd($v['extension_one'],$v['extension_two'],2))
  50. return '自定义佣金金额不能大于商品价格';
  51. if(isset($v['extension_one'])){
  52. if((bccomp($v['extension_one'],bcmul($v['price'],$extension_one_rate,2),2)) == -1)
  53. return '设置一级佣金不能低于系统比例';
  54. }
  55. if(isset($v['extension_two'])){
  56. if((bccomp($v['extension_two'],bcmul($v['price'],$extension_two_rate,2),2)) == -1)
  57. return '设置二级佣金不能低于系统比例';
  58. }
  59. }
  60. }
  61. return true;
  62. }
  63. public function checkUnique($value)
  64. {
  65. $arr = [];
  66. foreach ($value as $item){
  67. if(in_array($item['value'],$arr)) return '规格重复';
  68. $arr[] = $item['value'];
  69. if (count($item['detail']) != count(array_unique($item['detail']))) return '属性重复';
  70. }
  71. return true;
  72. }
  73. }