GoodsValidate.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LikeShop有特色的全开源社交分销电商系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 商业用途务必购买系统授权,以免引起不必要的法律纠纷
  7. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  8. // | 微信公众号:好象科技
  9. // | 访问官网:http://www.likemarket.net
  10. // | 访问社区:http://bbs.likemarket.net
  11. // | 访问手册:http://doc.likemarket.net
  12. // | 好象科技开发团队 版权所有 拥有最终解释权
  13. // +----------------------------------------------------------------------
  14. // | Author: LikeShopTeam
  15. // +----------------------------------------------------------------------
  16. namespace app\adminapi\validate\goods;
  17. use app\common\{logic\GoodsActivityLogic,
  18. model\Goods,
  19. model\Freight,
  20. enum\GoodsEnum,
  21. model\GoodsBrand,
  22. model\GoodsCategory,
  23. model\GoodsSupplier,
  24. validate\BaseValidate};
  25. /**
  26. * 商品验证器
  27. * Class GoodsValidate
  28. * @package app\adminapi\validate\goods
  29. */
  30. class GoodsValidate extends BaseValidate
  31. {
  32. protected $regex = ['money'=>'/^[0-9]+(.[0-9]{1,2})?$/'];
  33. protected $rule = [
  34. 'ids' => 'require|array',
  35. 'id' => 'require|checkGoods',
  36. 'name' => 'require|max:100',
  37. 'code' => 'require|max:32|unique:'.Goods::class.',code',
  38. 'type' => 'require|in:'.GoodsEnum::GOODS_REALITY.','.GoodsEnum::GOODS_VIRTUAL,
  39. 'category_id' => 'require|array|checkCategory',
  40. 'goods_image' => 'require|array|max:10',
  41. 'video_source' => 'in:1,2',
  42. 'brand_id' => 'checkBrand',
  43. 'supplier_id' => 'checkSupplier',
  44. 'express_type' => 'require|in:1,2,3',
  45. 'express_money' => 'requireIf:express_type,2|regex:money',
  46. 'express_template_id' => 'requireIf:express_type,3|checkTemplateId',
  47. 'spec_type' => 'require|in:'.GoodsEnum::SEPC_TYPE_SIGNLE.','.GoodsEnum::SEPC_TYPE_MORE,
  48. 'is_express' => 'require|in:0,1',
  49. 'is_selffetch' => 'require|in:0,1|checkDelivery',
  50. 'is_virtualdelivery' => 'requireIf:type,2|in:0,1',
  51. 'after_pay' => 'requireIf:type,2|in:0,'.GoodsEnum::AFTER_PAY_AUTO.','.GoodsEnum::AFTER_PAY_HANDOPERSTION,
  52. 'after_delivery' => 'requireIf:type,2|in:0,'.GoodsEnum::AFTER_DELIVERY_AUTO.','.GoodsEnum::AFTER_DELIVERY_HANDOPERSTION,
  53. 'stock_warning' => 'number',
  54. 'virtual_sales_num' => 'number',
  55. 'virtual_click_num' => 'number',
  56. 'is_address' => 'requireIf:type,2|in:0,1',
  57. 'limit_type' => 'require|in:1,2,3',
  58. 'limit_value' => 'requireIf:limit_type,2|requireIf:limit_type,3|number',
  59. 'content' => [ 'max' => 65535 ],
  60. ];
  61. protected $message = [
  62. 'ids.require' => '请选择商品',
  63. 'ids.array' => '参数格式错误',
  64. 'id.require' => '请选择商品',
  65. 'name.require' => '请选择商品名称',
  66. 'name.max' => '商品名称不可以超过100个字符',
  67. 'name.unique' => '商品名称已存在',
  68. 'code.require' => '请输入商品编码',
  69. 'code.max' => '商品编码不可以超过32个字符',
  70. 'code.unique' => '商品编码已存在',
  71. 'type.require' => '请选择商品类型',
  72. 'type.in' => '商品类型错误',
  73. 'category_id.require' => '请选择商品分类',
  74. 'category_id.array' => '商品分类值错误',
  75. 'goods_image.require' => '请上传商品轮播图',
  76. 'goods_image.array' => '商品轮播图信息错误',
  77. 'goods_image.max' => '商品轮播图不能超过5张',
  78. 'express_type.require' => '请选择配送设置',
  79. 'express_money.requireIf' => '请输入运费',
  80. 'express_money.regex' => '运费必须大于零,且保留两位小数',
  81. 'express_template_id.requireIf' => '请选择运费模板',
  82. 'spec_type.require' => '请选择规格',
  83. 'spec_type.in' => '商品规格类型错误',
  84. 'is_express.require' => '请选择物流',
  85. 'is_express.in' => '物流支持类型错误',
  86. 'is_selffetch.require' => '请选择物流',
  87. 'is_selffetch.in' => '物流支持类型错误',
  88. 'is_virtualdelivery.requireIf' => '请选择物流',
  89. 'is_virtualdelivery.in' => '物流支持类型错误',
  90. 'after_pay.requireIf' => '请选择买家付款后发货方式',
  91. 'after_pay.in' => '买家付款后发货方式类型错误',
  92. 'after_delivery.requireIf' => '请选择发货后是否自动完成订单',
  93. 'after_delivery.in' => '发货后是否自动完成订单类型错误',
  94. 'stock_warning.number' => '库存预警只能输入正整数字',
  95. 'virtual_sales_num.number' => '虚拟销量只能输入正整数字',
  96. 'virtual_click_num.number' => '虚拟浏览量只能输入正整数字',
  97. 'is_address.requireIf' => '请选择是否开启收货地址',
  98. 'is_address.in' => '收货地址值错误',
  99. 'limit_type.require' => '请选择购买限制',
  100. 'limit_type.in' => '购买限制值错误',
  101. 'limit_value.requireIf' => '请输入购买限制值',
  102. 'limit_value.number' => '购买限制值错误',
  103. 'content.max' => '商品内容最多支持65535字符',
  104. ];
  105. //商品添加验证
  106. public function sceneAdd()
  107. {
  108. return $this->remove(['id'=>'require','ids'=>'require|array']);
  109. }
  110. //商品编辑验证
  111. public function sceneEdit(){
  112. return $this->remove(['ids'=>'require|array'])->append(['id'=>'checkAcitvity']);
  113. }
  114. //商品详情验证
  115. public function sceneDetail(){
  116. return $this->only(['id']);
  117. }
  118. //商品状态验证
  119. public function sceneStatus(){
  120. return $this->only(['ids'])->append(['ids'=>'checkAcitvity']);
  121. }
  122. //商品排序验证
  123. public function sceneSort(){
  124. return $this->only(['id'=>'require']);
  125. }
  126. //重命名
  127. public function sceneRename(){
  128. return $this->only(['id'=>'require','name']);
  129. }
  130. //删除商品
  131. public function sceneDel(){
  132. return $this->only(['ids'])->append(['ids'=>'checkAcitvity']);
  133. }
  134. public function sceneChangeCategory(){
  135. return $this->only(['ids','category_id']);
  136. }
  137. public function checkGoods($value, $rule, $data){
  138. $goods = Goods::find($value);
  139. if(empty($goods)){
  140. return '商品已不存在';
  141. }
  142. return true;
  143. }
  144. // todo 编辑、删除时需要考虑商品的营销活动等问题
  145. public function checkAcitvity($value,$rule,$data){
  146. //商品下架需要验证是否在活动中
  147. if('status' == $this->currentScene && 1 == $data['status'] ){
  148. return true;
  149. };
  150. $activityList = GoodsActivityLogic::activityInfo($value);
  151. if($activityList){
  152. return '商品正在参加活动中,不允许修改';
  153. }
  154. return true;
  155. }
  156. //验证品牌
  157. public function checkBrand($value,$rule,$data)
  158. {
  159. if (empty($value)) {
  160. return true;
  161. }
  162. if (!(GoodsBrand::find($value))) {
  163. return '商品品牌不存在';
  164. }
  165. return true;
  166. }
  167. //验证供应商
  168. public function checkSupplier($value,$rule,$data)
  169. {
  170. if (empty($value)) {
  171. return true;
  172. }
  173. if (!(GoodsSupplier::find($value))) {
  174. return '供应商不存在';
  175. }
  176. return true;
  177. }
  178. //验证分类
  179. public function checkCategory($value,$rule,$data){
  180. $category_ids = GoodsCategory::where(['id'=>$value])->column('id');
  181. //提交的分类和找到的分类数量不一致
  182. if(count($value) != count($category_ids)){
  183. return '分类信息错误,请刷新分类';
  184. }
  185. return true;
  186. }
  187. /**
  188. * @notes 检查运费模版是否存在
  189. * @param $value
  190. * @param $rule
  191. * @param $data
  192. * @return bool|string
  193. * @author ljj
  194. * @date 2021/8/2 5:54 下午
  195. */
  196. public function checkTemplateId($value,$rule,$data)
  197. {
  198. if ($data['express_type'] == 3) {
  199. $result = Freight::where('id',$value)->findOrEmpty();
  200. if ($result->isEmpty()) {
  201. return '运费模版不存在';
  202. }
  203. }
  204. return true;
  205. }
  206. public function checkDelivery($value,$rule,$data)
  207. {
  208. if(GoodsEnum::GOODS_REALITY == $data['type'] && empty($value) && empty($data['is_express'])){
  209. return '至少选择一个物流支持';
  210. }
  211. return true;
  212. }
  213. }