GoodsCategoryValidate.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\shop\validate\goods;
  20. use think\Validate;
  21. use app\common\model\shop\ShopGoodsCategory as ShopGoodsCategoryModel;
  22. use app\common\model\goods\Goods as GoodsModel;
  23. class GoodsCategoryValidate extends Validate
  24. {
  25. protected $rule = [
  26. 'id' => 'require|checkCategory',
  27. 'name' => 'require|max: 30|checkName',
  28. 'sort' => 'integer|egt:0',
  29. ];
  30. protected $message = [
  31. 'id.require' => 'id不能为空',
  32. 'name.require' => '分类名称不能为空',
  33. 'name.max' => '分类名称不能超过30个字符',
  34. 'pid.require' => '请选择上级分类',
  35. 'pid.integer' => '上级id必须为整型',
  36. 'sort.integer' => '排序值须为整数',
  37. 'sort.egt' => '排序值须大于或等于0',
  38. ];
  39. /**
  40. * 添加场景
  41. */
  42. public function sceneAdd()
  43. {
  44. return $this->remove('id', ['require', 'checkCategory']);
  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. }
  60. /*
  61. * 校验分类名称
  62. */
  63. protected function checkName($value,$rule,$data){
  64. $where[] = ['del','=',0];
  65. $where[] = ['shop_id', '=', $data['shop_id']];
  66. // 如果有id代表是编辑校验分类名称
  67. if(isset($data['id'])){
  68. $where[] = ['id','<>',$data['id']];
  69. }
  70. $where[] = ['name','=',$data['name']];
  71. $name = ShopGoodsCategoryModel::where($where)->value('name');
  72. if($name){
  73. return '分类名称已存在';
  74. }
  75. return true;
  76. }
  77. /*
  78. * 验证分类
  79. */
  80. protected function checkCategory($value, $rule, $data){
  81. // 已经有商品绑定了该分类,不能删除
  82. $goods = GoodsModel::where([
  83. 'del' => 0,
  84. 'shop_cate_id' => $value
  85. ])->find();
  86. if($goods) {
  87. return '已有商品绑定此分类不允许删除';
  88. }
  89. return true;
  90. }
  91. }