GoodsBrandValidate.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\adminapi\validate\goods;
  20. use app\common\model\GoodsBrand;
  21. use app\common\validate\BaseValidate;
  22. class GoodsBrandValidate extends BaseValidate
  23. {
  24. protected $rule = [
  25. 'id' => 'require|checkId|checkDel',
  26. 'name' => 'require|checkName|max:8',
  27. 'is_show' => 'in:0,1',
  28. 'sort' => 'number',
  29. ];
  30. protected $message = [
  31. 'id.require' => '品牌id不能为空',
  32. 'name.require' => '品牌名称不能为空',
  33. ];
  34. public function sceneAdd()
  35. {
  36. return $this->only(['name','is_show','sort']);
  37. }
  38. public function sceneDel()
  39. {
  40. return $this->only(['id']);
  41. }
  42. public function sceneEdit()
  43. {
  44. return $this->only(['id','name','is_show','sort'])
  45. ->remove('id','checkDel');
  46. }
  47. public function sceneStatus()
  48. {
  49. return $this->only(['id','is_show'])
  50. ->remove('id','checkDel');
  51. }
  52. public function sceneDetail()
  53. {
  54. return $this->only(['id'])
  55. ->remove('id','checkDel');
  56. }
  57. /**
  58. * @notes 验证品牌是否已存在
  59. * @param $value
  60. * @param $rule
  61. * @param $data
  62. * @return bool|string
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\DbException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. * @author ljj
  67. * @date 2021/7/14 3:51
  68. */
  69. public function checkName($value,$rule,$data)
  70. {
  71. $where[] = ['name', '=', $value];
  72. // 编辑的情况,要排除自身ID
  73. if (isset($data['id'])) {
  74. $where[] = ['id', '<>', $data['id']];
  75. }
  76. $result = GoodsBrand::where($where)->select()->toArray();
  77. if ($result) {
  78. return '该品牌已存在';
  79. }
  80. return true;
  81. }
  82. /**
  83. * @notes 验证品牌是否存在
  84. * @param $value
  85. * @param $rule
  86. * @param $data
  87. * @return bool|string
  88. * @author ljj
  89. * @date 2021/7/14 7:21
  90. */
  91. public function checkId($value,$rule,$data)
  92. {
  93. $result = GoodsBrand::findOrEmpty($value);
  94. if ($result->isEmpty()) {
  95. return '品牌不存在';
  96. }
  97. return true;
  98. }
  99. /**
  100. * @notes 验证品牌能否删除
  101. * @param $value
  102. * @param $rule
  103. * @param $data
  104. * @return bool|string
  105. * @throws \think\db\exception\DataNotFoundException
  106. * @throws \think\db\exception\DbException
  107. * @throws \think\db\exception\ModelNotFoundException
  108. * @throws \think\exception\DbException
  109. * @author ljj
  110. * @date 2021/7/15 10:36
  111. */
  112. public function checkDel($value,$rule,$data)
  113. {
  114. $result = GoodsBrand::hasWhere('goods',['brand_id'=>$value])->select()->toArray();
  115. if (!empty($result)) {
  116. return '该品牌正在使用中,无法删除';
  117. }
  118. return true;
  119. }
  120. }