ArticleCategoryValidate.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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;
  20. use app\common\validate\BaseValidate;
  21. use app\common\model\ArticleCategory;
  22. use app\common\model\Article;
  23. class ArticleCategoryValidate extends BaseValidate
  24. {
  25. protected $rule = [
  26. 'id' => 'require|checkArticleCategory|checkDelete',
  27. 'name' => 'require|checkUsed',
  28. 'is_show' => 'require|in:0,1',
  29. 'sort' => 'require|integer|egt:0',
  30. ];
  31. protected $message = [
  32. 'id.require' => '分类id不能为空',
  33. 'name.require' => '请输入分类名称',
  34. 'is_show.require' => '请选择是否显示',
  35. 'is_show.in' => '是否显示状态有误',
  36. 'sort.require' => '请输入排序值',
  37. 'sort.integer' => '排序值须为整型',
  38. 'sort.egt' => '排序值须大于或等于0',
  39. ];
  40. public function sceneAdd()
  41. {
  42. return $this->remove('id', 'require|checkArticleCategory');
  43. }
  44. public function sceneDetail()
  45. {
  46. return $this->only(['id'])
  47. ->remove('id', 'checkDelete');
  48. }
  49. public function sceneEdit()
  50. {
  51. return $this->remove('id', 'checkDelete');
  52. }
  53. public function sceneDelete()
  54. {
  55. return $this->only(['id']);
  56. }
  57. /**
  58. * @notes 文章显示或隐藏场景
  59. * @return ArticleValidate
  60. * @author Tab
  61. * @date 2021/7/22 17:07
  62. */
  63. public function sceneShow()
  64. {
  65. return $this->only(['id']);
  66. }
  67. /**
  68. * @notes 校验分类名称是否可用
  69. * @param $value
  70. * @return bool|string
  71. * @throws \think\db\exception\DataNotFoundException
  72. * @throws \think\db\exception\DbException
  73. * @throws \think\db\exception\ModelNotFoundException
  74. * @author Tab
  75. * @date 2021/7/13 14:28
  76. */
  77. public function checkUsed($value, $rule, $data)
  78. {
  79. $where = [
  80. ['name', '=', $value]
  81. ];
  82. if(isset($data['id'])) {
  83. $where[] = ['id', '<>', $data['id']];
  84. }
  85. $articleCategory = ArticleCategory::where($where)->select()->toArray();
  86. if ($articleCategory) {
  87. return '分类名称已存在';
  88. }
  89. return true;
  90. }
  91. /**
  92. * @notes 校验文章分类是否存在
  93. * @param $value
  94. * @return bool|string
  95. * @author Tab
  96. * @date 2021/7/13 14:28
  97. */
  98. public function checkArticleCategory($value)
  99. {
  100. $articleCategory = ArticleCategory::findOrEmpty($value);
  101. if ($articleCategory->isEmpty()) {
  102. return '文章分类不存在';
  103. }
  104. return true;
  105. }
  106. /**
  107. * @notes 校验文章分类是否可删除
  108. * @param $value
  109. * @return bool|string
  110. * @throws \think\db\exception\DataNotFoundException
  111. * @throws \think\db\exception\DbException
  112. * @throws \think\db\exception\ModelNotFoundException
  113. * @author Tab
  114. * @date 2021/7/13 14:28
  115. */
  116. public function checkDelete($value)
  117. {
  118. $articles = Article::where('cid', $value)->select()->toArray();
  119. if ($articles) {
  120. return '该分类下有文章,不允许删除';
  121. }
  122. return true;
  123. }
  124. }