截流自动化的商城平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CategoryValidate.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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\admin\validate\goods;
  20. use think\Validate;
  21. use app\common\model\goods\GoodsCategory as GoodsCategoryModel;
  22. use app\common\model\goods\Goods as GoodsModel;
  23. use app\admin\logic\goods\CategoryLogic;
  24. class CategoryValidate extends Validate
  25. {
  26. protected $rule = [
  27. 'id' => 'require|checkCategory',
  28. 'name' => 'require|max: 30|checkName',
  29. 'pid' => 'require|integer|addPid|editPid',
  30. ];
  31. protected $message = [
  32. 'id.require' => 'id不能为空',
  33. 'name.require' => '分类名称不能为空',
  34. 'name.max' => '分类名称不能超过30个字符',
  35. 'pid.require' => '请选择上级分类',
  36. 'pid.integer' => '上级id必须为整型',
  37. ];
  38. /**
  39. * 添加场景
  40. */
  41. public function sceneAdd()
  42. {
  43. return $this->remove('id', ['require', 'checkCategory'])
  44. ->remove('pid','editPid');
  45. }
  46. /**
  47. * 删除场景
  48. */
  49. public function sceneDel()
  50. {
  51. return $this->only(['id']);
  52. }
  53. /**
  54. * 编辑场景
  55. */
  56. public function sceneEdit()
  57. {
  58. return $this->remove('id', 'checkCategory')
  59. ->remove('pid', 'addPid');
  60. }
  61. /*
  62. * 校验分类名称(同一个上级分类下不允许出现相同分类名称)
  63. */
  64. protected function checkName($value,$rule,$data){
  65. $where[] = ['del','=',0];
  66. // 如果有id代表是编辑校验分类名称
  67. if(isset($data['id'])){
  68. $where[] = ['id','<>',$data['id']];
  69. }
  70. $where[] = ['name','=',$data['name']];
  71. $where[] = ['pid','=',$data['pid']];
  72. $name = GoodsCategoryModel::where($where)->value('name');
  73. if($name){
  74. return '分类名称已存在';
  75. }
  76. return true;
  77. }
  78. /*
  79. * 添加时,校验上级
  80. */
  81. protected function addPid($value, $rule, $data){
  82. // 顶级分类直接通过
  83. if($value == 0) return true;
  84. $goods_category = GoodsCategoryModel::where([
  85. 'id' => $value,
  86. 'del' => 0
  87. ])->find();
  88. if($goods_category) return true;
  89. return '上级分类不存在,请重新选择';
  90. }
  91. /*
  92. * 验证分类
  93. */
  94. protected function checkCategory($value, $rule, $data){
  95. $children = GoodsCategoryModel::where([
  96. 'del' => 0,
  97. 'pid' => $value
  98. ])->find();
  99. if($children) {
  100. return '该分类下还有子分类不允许删除';
  101. }
  102. // 已经有商品绑定了该分类,不能删除
  103. $goods = GoodsModel::where([
  104. 'del' => 0,
  105. 'third_cate_id' => $value
  106. ])->find();
  107. if($goods) {
  108. return '已有商品绑定此分类不允许删除';
  109. }
  110. return true;
  111. }
  112. /*
  113. * 编辑时,验证上级分类
  114. */
  115. protected function editPid($value, $rule, $data){
  116. // 目标上级分类为顶部分类时,直接通过
  117. if($value == 0 ) return true;
  118. // 当前分类
  119. $category = GoodsCategoryModel::where(['id'=>$data['id'],'del'=>0])->find();
  120. // 目标上级分类
  121. $partner = GoodsCategoryModel::where(['id'=>$value,'del'=>0])->find();
  122. // 当前分类下的子分类
  123. $level = CategoryLogic::getCategoryLevel($category);
  124. if($category['id'] == $partner['id']) return '上级分类不能是自己';
  125. // 限制分类不超过3级
  126. if($level == 3 && $partner) return '该分类下有完整的子分类,不可修改上级分类';
  127. if($partner['level'] == 2 && $level != 1) return '该分类下有子分类,请先调整该分类下的子分类';
  128. if($partner['level'] == 3) return '父级分类不能是第三级';
  129. return true;
  130. }
  131. }