AuthorityService.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. namespace App\JsonRpc;
  3. use App\Model\Role;
  4. use App\Model\Menu;
  5. use App\Model\RoleUser;
  6. use App\Model\Website;
  7. use App\Model\WebsiteRoleUser;
  8. use App\Tools\Result;
  9. use Hyperf\RpcServer\Annotation\RpcService;
  10. use App\Tools\PublicData;
  11. use Hyperf\DbConnection\Db;
  12. use Hyperf\Database\Exception\DbException;
  13. #[RpcService(name: "AuthorityService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  14. class AuthorityService implements AuthorityServiceInterface
  15. {
  16. /**
  17. * @param array $data
  18. * @return array
  19. */
  20. public function getMenuList(array $data): array
  21. {
  22. $where = [];
  23. if(isset($data['id']) && $data['id']){
  24. array_push($where,['pid','=',$data['id']]);
  25. }
  26. $result = Menu::where($where)->get();
  27. if (empty($result)) {
  28. return Result::error("没有菜单",0,[]);
  29. }
  30. return Result::success($result);
  31. }
  32. /**
  33. * @param array $data
  34. * @return array
  35. */
  36. public function getMenuInfo(array $data): array
  37. {
  38. $result = Menu::where(['id'=>$data['id']])->first();
  39. if (empty($result)) {
  40. return Result::error("没有菜单",0,[]);
  41. }
  42. return Result::success($result);
  43. }
  44. /**
  45. * @param array $data
  46. * @return array
  47. */
  48. public function updateMenu(array $data): array
  49. {
  50. $data['pid'] = isset($data['pid_arr']) ? end($data['pid_arr']) : '';
  51. $pid_arr = isset($data['pid_arr']) ? array_map('intval', $data['pid_arr']) : null;
  52. $data['pid_arr'] = json_encode($pid_arr) ?? '';
  53. $where = [
  54. 'id'=>$data['id']
  55. ];
  56. unset($data['id']);
  57. // 使用模型查询,避免 Collection 的 where 方法报错
  58. $menu = Menu::where($where)->first();
  59. if(empty($menu)){
  60. return Result::error("菜单不存在",0,[]);
  61. }
  62. $menu_query = Menu::query();
  63. if($pid_arr[0] != 0){
  64. $pid_menu = $menu_query->whereIn('id',$pid_arr)->get()->toArray();
  65. if(empty($pid_menu)){
  66. return Result::error("父菜单不存在",0,[]);
  67. }
  68. foreach ($pid_menu as $val) {
  69. if (!empty($val['url'])) {
  70. return Result::error("父级菜单已存在路由,无法添加子级菜单!");
  71. }
  72. }
  73. }
  74. Db::beginTransaction();
  75. try{
  76. $child_key = 0;
  77. if($data['pid_arr'] != $menu['pid_arr']){
  78. $all_child_pid = Menu::where('pid','!=',0)->where('id','!=',$where['id'])->select('pid_arr','id','pid')->orderByRaw("JSON_LENGTH(pid_arr)")->get()->toArray();
  79. if($data['pid'] == 0){
  80. $pid_arr = [];
  81. }
  82. $child_id = array_keys($all_child_pid);
  83. $ids = array_column($all_child_pid,'id');
  84. $child_pid = [];
  85. $one_pidarr = [];
  86. $pid_arrnum = [];
  87. $child_data = [];
  88. $caseSql = '';
  89. foreach($all_child_pid as $key => $value){
  90. $child_arr = json_decode($value['pid_arr'] ?? '[]',true);
  91. if(in_array($where['id'],$child_arr)){
  92. $count_childarr = count($child_arr);
  93. $child_ids[$child_key] = $value['id'];
  94. if($value['pid'] == $where['id']){
  95. $child_data[$value['id']] = array_merge($pid_arr,[intval($where['id'])]);
  96. $one_pidarr[$count_childarr] = array_merge($pid_arr,[intval($where['id'])]);
  97. $pid_arrnum[$child_key] = $count_childarr;
  98. }else{
  99. // 确保 $child_data[$value['pid']] 已定义
  100. if(!isset($child_data[$value['pid']])){
  101. // 若未定义,先初始化为空数组
  102. $child_data[$value['pid']] = [];
  103. }
  104. if(in_array($count_childarr,$pid_arrnum)){
  105. $child_data[$value['id']] = array_merge($one_pidarr[$count_childarr-1],[intval($value['pid'])]);
  106. }else{
  107. $one_pidarr[$count_childarr] = array_merge($child_data[$value['pid']],[intval($value['pid'])]);
  108. $child_data[$value['id']] = $one_pidarr[$count_childarr];
  109. $pid_arrnum[$child_key] = $count_childarr;
  110. }
  111. }
  112. $pidArrJson = json_encode($child_data[$value['id']]);
  113. $caseSql .= "WHEN id = {$value['id']} THEN '{$pidArrJson}' ";
  114. $child_key++;
  115. }
  116. }
  117. $caseSql = "CASE {$caseSql} END";
  118. // Db::rollBack();
  119. // return Result::success($child_data);
  120. $affectedRows = Menu::whereIn('id', $child_ids)
  121. ->update([
  122. 'pid_arr' => \Hyperf\DbConnection\Db::raw($caseSql)
  123. ]);
  124. if($affectedRows != count($child_ids)){
  125. Db::rollBack();
  126. return Result::error('更新子菜单失败');
  127. }
  128. $result = Menu::where($where)->update($data);
  129. if(empty($result)){
  130. Db::rollBack();
  131. return Result::error('更新菜单失败');
  132. }
  133. Db::commit();
  134. return Result::success($result);
  135. }else{
  136. // 当 pid_arr 没有变化时,也要执行更新并返回结果
  137. $result = Menu::where($where)->update($data);
  138. if(empty($result)){
  139. Db::rollBack();
  140. return Result::error('更新菜单失败');
  141. }
  142. Db::commit();
  143. return Result::success($result);
  144. }
  145. }catch (\Exception $e){
  146. Db::rollBack();
  147. return Result::error($e->getMessage());
  148. }
  149. }
  150. /**
  151. * @param array $data
  152. * @return array
  153. */
  154. public function delMenu(array $data): array
  155. {
  156. $result = Menu::where(['id'=>$data['id']])->delete();
  157. if($result){
  158. return Result::success($data);
  159. }else{
  160. return Result::error($data);
  161. }
  162. }
  163. /**
  164. * @param array $data
  165. * @return array
  166. */
  167. public function addMenu(array $data): array
  168. {
  169. $data['pid'] = isset($data['pid_arr']) ? end($data['pid_arr']) : '';
  170. $pid_arr = isset($data['pid_arr']) ? array_map('intval', $data['pid_arr']) : null;
  171. $data['pid_arr'] = json_encode($pid_arr) ?? '';
  172. if($pid_arr[0] != 0){
  173. $pid_menu = Menu::whereIn('id',$pid_arr)->get()->toArray();
  174. if(empty($pid_menu)){
  175. return Result::error("父菜单不存在",0,[]);
  176. }
  177. foreach ($pid_menu as $val) {
  178. if (!empty($val['url'])) {
  179. return Result::error("父级菜单已存在路由,无法添加子级菜单!");
  180. }
  181. }
  182. }
  183. $result = Menu::insertGetId($data);
  184. if($result){
  185. return Result::success($result);
  186. }else{
  187. return Result::error('添加失败');
  188. }
  189. }
  190. /**
  191. * @param array $data
  192. * @return array
  193. */
  194. public function getRecursionMenu(array $data): array
  195. {
  196. //根据角色查询权限信息
  197. $roleWhere = [
  198. 'role_user.user_id'=>$data['user_id']
  199. ];
  200. $roleInfo = RoleUser::where($roleWhere)
  201. ->leftJoin('role', 'role.id', '=', 'role_user.role_id')
  202. ->first();
  203. if(empty($roleInfo)){
  204. return Result::error("没有权限",0);
  205. }
  206. $roleArr = json_decode($roleInfo['rule']);
  207. $result = Menu::whereIn('id',$roleArr)->orderBy("sort","asc")->get();
  208. if (empty($result)) {
  209. return Result::error("没有菜单",0,[]);
  210. }
  211. return Result::success($result);
  212. }
  213. /**
  214. * 获取所有的权限
  215. * @param array $data
  216. * @return array
  217. */
  218. public function getAllMenuList(array $data): array
  219. {
  220. // $all_menu = Menu::get();
  221. // return Result::success($data);
  222. $top_menu = Menu::where('pid',0)->pluck('id')->toArray();
  223. $pid_arr = Menu::where('pid', '!=', 0)
  224. ->where('url', '<>', '')
  225. ->select('pid_arr','id')
  226. ->get()
  227. ->toArray();
  228. // return Result::success($pid_arr);
  229. $child_ids = array_column($pid_arr,'id');
  230. $pids = array_column($pid_arr,'pid_arr');
  231. $all_pid = [];
  232. foreach ($pids as $key => $val) {
  233. $value = json_decode($val, true) ?: [];
  234. if (is_array($value)) {
  235. $all_pid = array_merge($all_pid,$value);
  236. }
  237. }
  238. $all_pid = array_values(array_unique($all_pid));
  239. $all_ids = array_merge($child_ids,$all_pid);
  240. // return Result::success($child_ids);
  241. $all_menu = Menu::whereIn('id',$all_ids)->get()->toArray();
  242. // $all_menuids = array_column($all_menu,'id');
  243. if($all_menu){
  244. // return Result::success([
  245. // 'child_ids'=>$child_ids,
  246. // 'all_pid'=>$all_pid,
  247. // 'all_menuids'=>$all_menuids,
  248. // ]);
  249. return Result::success($all_menu);
  250. }else{
  251. return Result::error("没有权限",0,[]);
  252. }
  253. }
  254. }