DeptValidate.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\adminapi\validate\dept;
  15. use app\common\model\auth\Admin;
  16. use app\common\model\auth\AdminDept;
  17. use app\common\model\dept\Dept;
  18. use app\common\validate\BaseValidate;
  19. /**
  20. * 部门验证器
  21. * Class DeptValidate
  22. * @package app\adminapi\validate\dept
  23. */
  24. class DeptValidate extends BaseValidate
  25. {
  26. protected $rule = [
  27. 'id' => 'require|checkDept',
  28. 'pid' => 'require|integer',
  29. 'name' => 'require|unique:'.Dept::class.'|length:1,30',
  30. 'status' => 'require|in:0,1',
  31. 'sort' => 'egt:0',
  32. ];
  33. protected $message = [
  34. 'id.require' => '参数缺失',
  35. 'name.require' => '请填写部门名称',
  36. 'name.length' => '部门名称长度须在1-30位字符',
  37. 'name.unique' => '部门名称已存在',
  38. 'sort.egt' => '排序值不正确',
  39. 'pid.require' => '请选择上级部门',
  40. 'pid.integer' => '上级部门参数错误',
  41. 'status.require' => '请选择部门状态',
  42. ];
  43. /**
  44. * @notes 添加场景
  45. * @return DeptValidate
  46. * @author 段誉
  47. * @date 2022/5/25 18:16
  48. */
  49. public function sceneAdd()
  50. {
  51. return $this->remove('id', true)->append('pid', 'checkDept');
  52. }
  53. /**
  54. * @notes 详情场景
  55. * @return DeptValidate
  56. * @author 段誉
  57. * @date 2022/5/25 18:16
  58. */
  59. public function sceneDetail()
  60. {
  61. return $this->only(['id']);
  62. }
  63. /**
  64. * @notes 编辑场景
  65. * @return DeptValidate
  66. * @author 段誉
  67. * @date 2022/5/26 18:42
  68. */
  69. public function sceneEdit()
  70. {
  71. return $this->append('pid', 'checkPid');
  72. }
  73. /**
  74. * @notes 删除场景
  75. * @return DeptValidate
  76. * @author 段誉
  77. * @date 2022/5/25 18:16
  78. */
  79. public function sceneDelete()
  80. {
  81. return $this->only(['id'])->append('id', 'checkAbleDetele');
  82. }
  83. /**
  84. * @notes 校验部门
  85. * @param $value
  86. * @return bool|string
  87. * @author 段誉
  88. * @date 2022/5/25 18:17
  89. */
  90. public function checkDept($value)
  91. {
  92. $dept = Dept::findOrEmpty($value);
  93. if ($dept->isEmpty()) {
  94. return '部门不存在';
  95. }
  96. return true;
  97. }
  98. /**
  99. * @notes 校验能否删除
  100. * @param $value
  101. * @return bool|string
  102. * @author 段誉
  103. * @date 2022/5/26 14:22
  104. */
  105. public function checkAbleDetele($value)
  106. {
  107. $hasLower = Dept::where(['pid' => $value])->findOrEmpty();
  108. if (!$hasLower->isEmpty()) {
  109. return '已关联下级部门,暂不可删除';
  110. }
  111. $check = AdminDept::where(['dept_id' => $value])->findOrEmpty();
  112. if (!$check->isEmpty()) {
  113. return '已关联管理员,暂不可删除';
  114. }
  115. $dept = Dept::findOrEmpty($value);
  116. if ($dept['pid'] == 0) {
  117. return '顶级部门不可删除';
  118. }
  119. return true;
  120. }
  121. /**
  122. * @notes 校验部门
  123. * @param $value
  124. * @param $rule
  125. * @param array $data
  126. * @return bool|string
  127. * @author 段誉
  128. * @date 2022/5/26 18:41
  129. */
  130. public function checkPid($value, $rule, $data = [])
  131. {
  132. // 当前编辑的部门id信息是否存在
  133. $dept = Dept::findOrEmpty($data['id']);
  134. if ($dept->isEmpty()) {
  135. return '当前部门信息缺失';
  136. }
  137. // 顶级部门不校验上级部门id
  138. if ($dept['pid'] == 0) {
  139. return true;
  140. }
  141. if ($data['id'] == $value) {
  142. return '上级部门不可是当前部门';
  143. }
  144. $leaderDept = Dept::findOrEmpty($value);
  145. if ($leaderDept->isEmpty()) {
  146. return '部门不存在';
  147. }
  148. return true;
  149. }
  150. }