GoodsGatherValidate.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop开源商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | gitee下载:https://gitee.com/likeshop_gitee
  7. // | github下载:https://github.com/likeshop-github
  8. // | 访问官网:https://www.likeshop.cn
  9. // | 访问社区:https://home.likeshop.cn
  10. // | 访问手册:http://doc.likeshop.cn
  11. // | 微信公众号:likeshop技术社区
  12. // | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
  13. // | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
  14. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  15. // | likeshop团队版权所有并拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshop.cn.team
  18. // +----------------------------------------------------------------------
  19. namespace app\adminapi\validate\goods;
  20. use app\common\enum\GoodsEnum;
  21. use app\common\model\Freight;
  22. use app\common\model\Goods;
  23. use app\common\model\GoodsBrand;
  24. use app\common\model\GoodsCategory;
  25. use app\common\model\GoodsGatherGoods;
  26. use app\common\model\GoodsSupplier;
  27. use app\common\service\ConfigService;
  28. use app\common\validate\BaseValidate;
  29. use think\facade\Validate;
  30. class GoodsGatherValidate extends BaseValidate
  31. {
  32. protected $rule = [
  33. 'goods_type' => 'require|in:1,2',
  34. 'goods_category' => 'require|checkCategory',
  35. 'gather_url' => 'require|array',
  36. 'log_id' => 'require',
  37. 'id' => 'require',
  38. 'delivery_content' => 'requireIf:goods_type,2',
  39. 'gather_id' => 'require',
  40. 'name' => 'require|max:100|unique:'.Goods::class.',name',
  41. 'code' => 'require|max:32|unique:'.Goods::class.',code',
  42. 'type' => 'require|in:'.GoodsEnum::GOODS_REALITY.','.GoodsEnum::GOODS_VIRTUAL,
  43. 'category_id' => 'require|array|checkCategory',
  44. 'goods_image' => 'require|array|max:10',
  45. 'video_source' => 'in:1,2',
  46. 'brand_id' => 'checkBrand',
  47. 'supplier_id' => 'checkSupplier',
  48. 'express_type' => 'require|in:1,2,3',
  49. 'express_money' => 'requireIf:express_type,2|checkExpressMoney',
  50. 'express_template_id' => 'requireIf:express_type,3|checkTemplateId',
  51. 'is_express' => 'require|in:0,1',
  52. 'is_selffetch' => 'require|in:0,1|checkDelivery',
  53. 'is_virtualdelivery' => 'requireIf:type,2|in:0,1',
  54. 'after_pay' => 'requireIf:type,2|in:0,'.GoodsEnum::AFTER_PAY_AUTO.','.GoodsEnum::AFTER_PAY_HANDOPERSTION,
  55. 'after_delivery' => 'requireIf:type,2|in:0,'.GoodsEnum::AFTER_DELIVERY_AUTO.','.GoodsEnum::AFTER_DELIVERY_HANDOPERSTION,
  56. 'stock_warning' => 'number',
  57. 'virtual_sales_num' => 'number',
  58. 'virtual_click_num' => 'number',
  59. 'spec_type' => 'require|in:'.GoodsEnum::SEPC_TYPE_SIGNLE.','.GoodsEnum::SEPC_TYPE_MORE,
  60. 'spec_value' => 'requireIf:spec_type,'.GoodsEnum::SEPC_TYPE_MORE.'|array',
  61. 'spec_value_list' => 'require|array',
  62. 'ids' => 'require|array',
  63. 'status' => 'require|in:0,1',
  64. ];
  65. protected $message = [
  66. 'goods_type.require' => '请选择商品类型',
  67. 'goods_type.in' => '商品类型值错误',
  68. 'goods_category.require' => '请选择商品分类',
  69. 'gather_url.require' => '商品链接不能为空',
  70. 'gather_url.array' => '商品链接值错误',
  71. 'log_id.require' => '参数错误',
  72. 'id.require' => '参数错误',
  73. 'delivery_content.requireIf' => '请输入发货内容',
  74. 'name.require' => '请选择商品名称',
  75. 'name.max' => '商品名称不可以超过100个字符',
  76. 'name.unique' => '商品名称已存在',
  77. 'code.require' => '请输入商品编码',
  78. 'code.max' => '商品编码不可以超过32个字符',
  79. 'code.unique' => '商品编码已存在',
  80. 'type.require' => '请选择商品类型',
  81. 'type.in' => '商品类型错误',
  82. 'goods_image.require' => '请上传商品轮播图',
  83. 'goods_image.array' => '商品轮播图信息错误',
  84. 'goods_image.max' => '商品轮播图不能超过5张',
  85. 'express_type.require' => '请选择配送设置',
  86. 'express_money.requireIf' => '请输入运费',
  87. 'express_money.regex' => '运费必须大于零,且保留两位小数',
  88. 'express_template_id.requireIf' => '请选择运费模板',
  89. 'is_express.require' => '请选择物流',
  90. 'is_express.in' => '物流支持类型错误',
  91. 'is_selffetch.require' => '请选择物流',
  92. 'is_selffetch.in' => '物流支持类型错误',
  93. 'is_virtualdelivery.requireIf' => '请选择物流',
  94. 'is_virtualdelivery.in' => '物流支持类型错误',
  95. 'after_pay.requireIf' => '请选择买家付款后发货方式',
  96. 'after_pay.in' => '买家付款后发货方式类型错误',
  97. 'after_delivery.requireIf' => '请选择发货后是否自动完成订单',
  98. 'after_delivery.in' => '发货后是否自动完成订单类型错误',
  99. 'stock_warning.number' => '库存预警只能输入正整数字',
  100. 'virtual_sales_num.number' => '虚拟销量只能输入正整数字',
  101. 'virtual_click_num.number' => '虚拟浏览量只能输入正整数字',
  102. 'spec_type.require' => '请选择规格',
  103. 'spec_type.in' => '商品规格类型错误',
  104. 'spec_value.requireIf' => '请输入规格项',
  105. 'spec_value.array' => '规格项值错误',
  106. 'spec_value_list.require' => '请输入规格项信息',
  107. 'spec_value_list.array' => '规格项信息值错误',
  108. 'ids.require' => '参数错误',
  109. 'ids.array' => '参数格式错误',
  110. 'status.require' => '请选择状态值',
  111. 'status.in' => '状态值错误',
  112. ];
  113. public function sceneGather()
  114. {
  115. return $this->only(['goods_type','goods_category','gather_url','delivery_content'])
  116. ->append('gather_url','checkGather');
  117. }
  118. public function sceneDel()
  119. {
  120. return $this->only(['log_id']);
  121. }
  122. public function sceneGatherGoodsDetail()
  123. {
  124. return $this->only(['id']);
  125. }
  126. public function sceneGatherGoodsEdit()
  127. {
  128. return $this->only(['gather_id','name','code','type','category_id','goods_image','video_source','brand_id','supplier_id','express_type','express_money','express_template_id','is_express','is_selffetch','is_virtualdelivery','after_pay','after_delivery','stock_warning','virtual_sales_num','virtual_click_num','spec_type','spec_value','spec_value_list']);
  129. }
  130. public function sceneCreateGoods()
  131. {
  132. return $this->only(['ids','status'])
  133. ->append('ids','checkCreateGoods');
  134. }
  135. /**
  136. * @notes 校验商品分类
  137. * @param $value
  138. * @param $rule
  139. * @param $data
  140. * @return bool|string
  141. * @author ljj
  142. * @date 2023/3/13 3:33 下午
  143. */
  144. public function checkCategory($value,$rule,$data)
  145. {
  146. $result = GoodsCategory::where(['id'=>$value])->findOrEmpty();
  147. if($result->isEmpty()){
  148. return '商品分类不存在';
  149. }
  150. return true;
  151. }
  152. /**
  153. * @notes 校验采集
  154. * @param $value
  155. * @param $rule
  156. * @param $data
  157. * @return bool|string
  158. * @author ljj
  159. * @date 2023/3/13 3:52 下午
  160. */
  161. public function checkGather($value,$rule,$data)
  162. {
  163. $key_99api = ConfigService::get('goods_gather', 'key_99api', '');
  164. if(empty($key_99api)){
  165. return '99Apikey还未配置,请前往配置';
  166. }
  167. return true;
  168. }
  169. /**
  170. * @notes 校验品牌
  171. * @param $value
  172. * @param $rule
  173. * @param $data
  174. * @return bool|string
  175. * @throws \think\db\exception\DataNotFoundException
  176. * @throws \think\db\exception\DbException
  177. * @throws \think\db\exception\ModelNotFoundException
  178. * @author ljj
  179. * @date 2023/3/16 11:12 上午
  180. */
  181. public function checkBrand($value,$rule,$data)
  182. {
  183. if (empty($value)) {
  184. return true;
  185. }
  186. if (!(GoodsBrand::find($value))) {
  187. return '商品品牌不存在';
  188. }
  189. return true;
  190. }
  191. /**
  192. * @notes 校验供应商
  193. * @param $value
  194. * @param $rule
  195. * @param $data
  196. * @return bool|string
  197. * @throws \think\db\exception\DataNotFoundException
  198. * @throws \think\db\exception\DbException
  199. * @throws \think\db\exception\ModelNotFoundException
  200. * @author ljj
  201. * @date 2023/3/16 11:12 上午
  202. */
  203. public function checkSupplier($value,$rule,$data)
  204. {
  205. if (empty($value)) {
  206. return true;
  207. }
  208. if (!(GoodsSupplier::find($value))) {
  209. return '供应商不存在';
  210. }
  211. return true;
  212. }
  213. function checkExpressMoney($value,$rule,$data)
  214. {
  215. if ($data['express_type'] == 2) {
  216. if (! is_numeric($value)) {
  217. return '请输入正确的运费';
  218. }
  219. if ($value < 0) {
  220. return '运费不能小于0';
  221. }
  222. if (round($value, 2) != $value) {
  223. return '运费至多两位小数';
  224. }
  225. }
  226. return true;
  227. }
  228. /**
  229. * @notes 检查运费模版是否存在
  230. * @param $value
  231. * @param $rule
  232. * @param $data
  233. * @return bool|string
  234. * @author ljj
  235. * @date 2021/8/2 5:54 下午
  236. */
  237. public function checkTemplateId($value,$rule,$data)
  238. {
  239. if ($data['express_type'] == 3) {
  240. $result = Freight::where('id',$value)->findOrEmpty();
  241. if ($result->isEmpty()) {
  242. return '运费模版不存在';
  243. }
  244. }
  245. return true;
  246. }
  247. /**
  248. * @notes 校验物流
  249. * @param $value
  250. * @param $rule
  251. * @param $data
  252. * @return bool|string
  253. * @author ljj
  254. * @date 2023/3/16 11:13 上午
  255. */
  256. public function checkDelivery($value,$rule,$data)
  257. {
  258. if(GoodsEnum::GOODS_REALITY == $data['type'] && empty($value) && empty($data['is_express'])){
  259. return '至少选择一个物流支持';
  260. }
  261. return true;
  262. }
  263. /**
  264. * @notes 校验创建商品
  265. * @param $value
  266. * @param $rule
  267. * @param $data
  268. * @return bool|string
  269. * @throws \think\db\exception\DataNotFoundException
  270. * @throws \think\db\exception\DbException
  271. * @throws \think\db\exception\ModelNotFoundException
  272. * @author ljj
  273. * @date 2023/5/9 11:04 上午
  274. */
  275. public function checkCreateGoods($value,$rule,$data)
  276. {
  277. $lists = GoodsGatherGoods::where(['gather_id'=>$value])->field('id,name')->select()->toArray();
  278. $data = [];
  279. foreach ($lists as $list) {
  280. if (!Validate::max($list['name'],100)) {
  281. $data[] = '商品:\''.$list['name'].'\' 的商品名称不可以超过100个字符';
  282. }
  283. $goods = Goods::where(['name'=>$list['name']])->findOrEmpty();
  284. if (!$goods->isEmpty()) {
  285. $data[] = '商品:\''.$list['name'].'\' 的商品名称已存在';
  286. }
  287. }
  288. if (!empty($data)) {
  289. return json_encode($data,JSON_UNESCAPED_UNICODE);
  290. }
  291. return true;
  292. }
  293. }