RuleService.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace Ycbl\AdminAuth\Service;
  3. use Exception;
  4. use Hyperf\Validation\Contract\ValidatorFactoryInterface;
  5. use Ycbl\AdminAuth\Dao\AuthRule;
  6. use Hyperf\Di\Annotation\Inject;
  7. use Ycbl\AdminAuth\Exception\AdminAuthException;
  8. class RuleService
  9. {
  10. /**
  11. * @Inject
  12. * @var AuthRule
  13. */
  14. protected $authRuleDao;
  15. /**
  16. * @Inject
  17. * @var ValidatorFactoryInterface
  18. */
  19. protected $validationFactory;
  20. const TREE = 1;
  21. const LIST = 2;
  22. /**
  23. * @Inject
  24. * @var AuthService
  25. */
  26. protected $auth;
  27. public function __construct()
  28. {
  29. if (!$this->auth->isSuperAdmin()) {
  30. throw new AdminAuthException('仅超级管理组可以访问');
  31. }
  32. }
  33. /**
  34. * 获取所有权限规则
  35. * @param int $type TREE OR LIST
  36. * @return array
  37. */
  38. public function getAllRule(int $type = self::TREE): array
  39. {
  40. $list = $this->authRuleDao->getRuleList()->toArray();
  41. $tree = make(TreeService::class)->init($list);
  42. $arrTree = $tree->getTreeArray(0);
  43. if ($type == self::TREE) {
  44. return $arrTree;
  45. } else {
  46. return $tree->getTreeList($arrTree, 'title');
  47. }
  48. }
  49. /**
  50. * 创建规则
  51. * @param string $title 权限标题
  52. * @param string $path 前端路由
  53. * @param string $auth api路由
  54. * @param string $icon 图标
  55. * @param int $pid 父级ID
  56. * @param int $is_menu 是否菜单
  57. * @param int $weigh 权重
  58. * @param string $remark 备注
  59. * @return bool
  60. * @throws Exception
  61. */
  62. public function createRule(string $title, string $path, string $auth, $icon = '', $pid = 0, $is_menu = 0, $weigh = 0, $remark = '')
  63. {
  64. if (!$is_menu && !$pid) {
  65. throw new Exception('非菜单规则节点必须有父级');
  66. }
  67. $data = [
  68. 'pid' => $pid,
  69. 'path' => $path,
  70. 'auth' => $auth,
  71. 'title' => $title,
  72. 'icon' => $icon,
  73. 'remark' => $remark,
  74. 'ismenu' => $is_menu,
  75. 'weigh' => $weigh,
  76. ];
  77. $validator = $this->validationFactory->make($data, [
  78. 'pid' => 'required|integer',
  79. 'path' => 'required|unique:auth_rule',
  80. 'auth' => 'required|unique:auth_rule',
  81. 'title' => 'required',
  82. 'ismenu' => 'required|boolean',
  83. 'weigh' => 'required|integer',
  84. ]);
  85. if ($validator->fails()) {
  86. throw new Exception($validator->errors()->first());
  87. }
  88. return $this->authRuleDao->insertRule($data);
  89. }
  90. /**
  91. * 编辑规则
  92. * @param $id
  93. * @param string $title
  94. * @param string $path
  95. * @param string $auth
  96. * @param $icon
  97. * @param $pid
  98. * @param $is_menu
  99. * @param $weigh
  100. * @param $remark
  101. * @return int
  102. * @throws Exception
  103. */
  104. public function editRule($id, string $title, string $path, string $auth, $icon, $pid, $is_menu, $weigh, $remark)
  105. {
  106. $rule = $this->authRuleDao->getOneRuleById($id);
  107. if (!$rule) {
  108. throw new Exception('记录未找到');
  109. }
  110. if (!$is_menu && !$pid) {
  111. throw new Exception('非菜单规则节点必须有父级');
  112. }
  113. if ($pid != $rule->pid) {
  114. //获取当前节点的所有子节点ID
  115. $all_rule = $this->authRuleDao->getRuleList()->toArray();
  116. $children_ids = make(TreeService::class)->init($all_rule)->getChildrenIds($rule->id);
  117. if (in_array($pid, $children_ids)) {
  118. throw new Exception("变更的父组别不能是它的子组别");
  119. }
  120. }
  121. $data = [
  122. 'pid' => $pid,
  123. 'path' => $path,
  124. 'auth' => $auth,
  125. 'title' => $title,
  126. 'icon' => $icon,
  127. 'remark' => $remark,
  128. 'ismenu' => $is_menu,
  129. 'weigh' => $weigh,
  130. ];
  131. $validator = $this->validationFactory->make($data, [
  132. 'pid' => 'required|integer',
  133. 'path' => 'required|unique:auth_rule,path,' . $id . ',id',
  134. 'auth' => 'required|unique:auth_rule,auth,' . $id . ',id',
  135. 'title' => 'required',
  136. 'ismenu' => 'required|boolean',
  137. 'weigh' => 'required|integer',
  138. ]);
  139. if ($validator->fails()) {
  140. throw new Exception($validator->errors()->first());
  141. }
  142. return $this->authRuleDao->updateRuleById($id, $data);
  143. }
  144. /**
  145. * 删除规则
  146. * @param $ids
  147. * @return int|mixed
  148. */
  149. public function deleteRule($ids)
  150. {
  151. if (!is_array($ids)) {
  152. $ids = explode(',', $ids);
  153. }
  154. $del_ids = [];
  155. foreach ($ids as $k => $v) {
  156. $all_rule = $this->authRuleDao->getRuleList()->toArray();
  157. $children_ids = make(TreeService::class)->init($all_rule)->getChildrenIds($v,true);
  158. $del_ids = array_merge($del_ids, $children_ids);
  159. }
  160. return $this->authRuleDao->deleteRule($del_ids);
  161. }
  162. }