ArticleCategoryValidate.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. public function sceneShow()
  58. {
  59. return $this->only(['id'])
  60. ->remove('id', 'checkDelete|checkArticleCategory');
  61. }
  62. /**
  63. * @notes 校验分类名称是否可用
  64. * @param $value
  65. * @return bool|string
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\DbException
  68. * @throws \think\db\exception\ModelNotFoundException
  69. * @author Tab
  70. * @date 2021/7/13 14:28
  71. */
  72. public function checkUsed($value, $rule, $data)
  73. {
  74. $where = [
  75. ['name', '=', $value]
  76. ];
  77. if(isset($data['id'])) {
  78. $where[] = ['id', '<>', $data['id']];
  79. }
  80. $articleCategory = ArticleCategory::where($where)->select()->toArray();
  81. if ($articleCategory) {
  82. return '分类名称已存在';
  83. }
  84. return true;
  85. }
  86. /**
  87. * @notes 校验文章分类是否存在
  88. * @param $value
  89. * @return bool|string
  90. * @author Tab
  91. * @date 2021/7/13 14:28
  92. */
  93. public function checkArticleCategory($value)
  94. {
  95. $articleCategory = ArticleCategory::findOrEmpty($value);
  96. if ($articleCategory->isEmpty()) {
  97. return '文章分类不存在';
  98. }
  99. return true;
  100. }
  101. /**
  102. * @notes 校验文章分类是否可删除
  103. * @param $value
  104. * @return bool|string
  105. * @throws \think\db\exception\DataNotFoundException
  106. * @throws \think\db\exception\DbException
  107. * @throws \think\db\exception\ModelNotFoundException
  108. * @author Tab
  109. * @date 2021/7/13 14:28
  110. */
  111. public function checkDelete($value)
  112. {
  113. $articles = Article::where('cid', $value)->select()->toArray();
  114. if ($articles) {
  115. return '该分类下有文章,不允许删除';
  116. }
  117. return true;
  118. }
  119. }