| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- <?php
- namespace App\JsonRpc;
- use App\Model\Role;
- use App\Model\Menu;
- use App\Model\RoleUser;
- use App\Model\Website;
- use App\Model\WebsiteRoleUser;
- use App\Tools\Result;
- use Hyperf\RpcServer\Annotation\RpcService;
- use App\Tools\PublicData;
- use Hyperf\DbConnection\Db;
- use Hyperf\Database\Exception\DbException;
- #[RpcService(name: "AuthorityService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
- class AuthorityService implements AuthorityServiceInterface
- {
- /**
- * @param array $data
- * @return array
- */
- public function getMenuList(array $data): array
- {
- $where = [];
- if(isset($data['id']) && $data['id']){
- array_push($where,['pid','=',$data['id']]);
- }
- $result = Menu::where($where)->get();
- if (empty($result)) {
- return Result::error("没有菜单",0,[]);
- }
- return Result::success($result);
- }
- /**
- * @param array $data
- * @return array
- */
- public function getMenuInfo(array $data): array
- {
- $result = Menu::where(['id'=>$data['id']])->first();
- if (empty($result)) {
- return Result::error("没有菜单",0,[]);
- }
- return Result::success($result);
- }
- /**
- * @param array $data
- * @return array
- */
- public function updateMenu(array $data): array
- {
- $data['pid'] = isset($data['pid_arr']) ? end($data['pid_arr']) : '';
- $pid_arr = isset($data['pid_arr']) ? array_map('intval', $data['pid_arr']) : null;
- $data['pid_arr'] = json_encode($pid_arr) ?? '';
- $where = [
- 'id'=>$data['id']
- ];
- unset($data['id']);
- // 使用模型查询,避免 Collection 的 where 方法报错
- $menu = Menu::where($where)->first();
- if(empty($menu)){
- return Result::error("菜单不存在",0,[]);
- }
- $menu_query = Menu::query();
- if($pid_arr[0] != 0){
- $pid_menu = $menu_query->whereIn('id',$pid_arr)->get()->toArray();
- if(empty($pid_menu)){
- return Result::error("父菜单不存在",0,[]);
- }
- foreach ($pid_menu as $val) {
- if (!empty($val['url'])) {
- return Result::error("父级菜单已存在路由,无法添加子级菜单!");
- }
- }
- }
- Db::beginTransaction();
- try{
- $child_key = 0;
- $all_child_pid = Menu::where('pid','!=',0)->where('id','!=',$where['id'])->select('pid_arr','id','pid')->orderByRaw("JSON_LENGTH(pid_arr)")->get()->toArray();
- if($data['pid'] == 0){
- $pid_arr = [];
- }
- $child_id = array_keys($all_child_pid);
- $ids = array_column($all_child_pid,'id');
- $pids = array_column($all_child_pid,'pid');
- if($data['pid_arr'] != $menu['pid_arr'] && in_array($where['id'],$pids)){
- $child_pid = [];
- $one_pidarr = [];
- $pid_arrnum = [];
- $child_data = [];
- $caseSql = '';
- foreach($all_child_pid as $key => $value){
- $child_arr = json_decode($value['pid_arr'] ?? '[]',true);
- if(in_array($where['id'],$child_arr)){
- $count_childarr = count($child_arr);
- $child_ids[$child_key] = $value['id'];
- if($value['pid'] == $where['id']){
- $child_data[$value['id']] = array_merge($pid_arr,[intval($where['id'])]);
- $one_pidarr[$count_childarr] = array_merge($pid_arr,[intval($where['id'])]);
- $pid_arrnum[$child_key] = $count_childarr;
- }else{
- // 确保 $child_data[$value['pid']] 已定义
- if(!isset($child_data[$value['pid']])){
- // 若未定义,先初始化为空数组
- $child_data[$value['pid']] = [];
- }
- if(in_array($count_childarr,$pid_arrnum)){
- $child_data[$value['id']] = array_merge($one_pidarr[$count_childarr-1],[intval($value['pid'])]);
- }else{
- $one_pidarr[$count_childarr] = array_merge($child_data[$value['pid']],[intval($value['pid'])]);
- $child_data[$value['id']] = $one_pidarr[$count_childarr];
- $pid_arrnum[$child_key] = $count_childarr;
- }
- }
- $pidArrJson = json_encode($child_data[$value['id']]);
- $caseSql .= "WHEN id = {$value['id']} THEN '{$pidArrJson}' ";
- $child_key++;
- }
- }
- $caseSql = "CASE {$caseSql} END";
- // Db::rollBack();
- // return Result::success($child_data);
- $affectedRows = Menu::whereIn('id', $child_ids)
- ->update([
- 'pid_arr' => \Hyperf\DbConnection\Db::raw($caseSql)
- ]);
- if($affectedRows != count($child_ids)){
- Db::rollBack();
- return Result::error('更新子菜单失败');
- }
- }
- $result = Menu::where($where)->update($data);
- if(empty($result)){
- Db::rollBack();
- return Result::error('更新菜单失败');
- }
- Db::commit();
- return Result::success($result);
- }catch (\Exception $e){
- Db::rollBack();
- return Result::error($e->getMessage());
- }
-
- }
- /**
- * @param array $data
- * @return array
- */
- public function delMenu(array $data): array
- {
- $result = Menu::where(['id'=>$data['id']])->delete();
- if($result){
- return Result::success($data);
- }else{
- return Result::error($data);
- }
- }
- /**
- * @param array $data
- * @return array
- */
- public function addMenu(array $data): array
- {
- $data['pid'] = isset($data['pid_arr']) ? end($data['pid_arr']) : '';
- $pid_arr = isset($data['pid_arr']) ? array_map('intval', $data['pid_arr']) : null;
- $data['pid_arr'] = json_encode($pid_arr) ?? '';
- if($pid_arr[0] != 0){
- $pid_menu = Menu::whereIn('id',$pid_arr)->get()->toArray();
- if(empty($pid_menu)){
- return Result::error("父菜单不存在",0,[]);
- }
- foreach ($pid_menu as $val) {
- if (!empty($val['url'])) {
- return Result::error("父级菜单已存在路由,无法添加子级菜单!");
- }
- }
- }
- $result = Menu::insertGetId($data);
- if($result){
- return Result::success($result);
- }else{
- return Result::error('添加失败');
- }
- }
- /**
- * @param array $data
- * @return array
- */
- public function getRecursionMenu(array $data): array
- {
- //根据角色查询权限信息
- $roleWhere = [
- 'role_user.user_id'=>$data['user_id']
- ];
- $roleInfo = RoleUser::where($roleWhere)
- ->leftJoin('role', 'role.id', '=', 'role_user.role_id')
- ->first();
- if(empty($roleInfo)){
- return Result::error("没有权限",0);
- }
- $roleArr = json_decode($roleInfo['rule']);
- $result = Menu::whereIn('id',$roleArr)->orderBy("sort","asc")->get();
- if (empty($result)) {
- return Result::error("没有菜单",0,[]);
- }
- return Result::success($result);
- }
- /**
- * 获取所有的权限
- * @param array $data
- * @return array
- */
- public function getAllMenuList(array $data): array
- {
- // $all_menu = Menu::get();
- // return Result::success($data);
- $top_menu = Menu::where('pid',0)->pluck('id')->toArray();
- $pid_arr = Menu::where('pid', '!=', 0)
- ->where('url', '<>', '')
- ->select('pid_arr','id')
- ->get()
- ->toArray();
- // return Result::success($pid_arr);
- $child_ids = array_column($pid_arr,'id');
- $pids = array_column($pid_arr,'pid_arr');
- $all_pid = [];
- foreach ($pids as $key => $val) {
- $value = json_decode($val, true) ?: [];
- if (is_array($value)) {
- $all_pid = array_merge($all_pid,$value);
- }
- }
- $all_pid = array_values(array_unique($all_pid));
- $all_ids = array_merge($child_ids,$all_pid);
- // return Result::success($child_ids);
- $all_menu = Menu::whereIn('id',$all_ids)->get()->toArray();
- // $all_menuids = array_column($all_menu,'id');
- if($all_menu){
- // return Result::success([
- // 'child_ids'=>$child_ids,
- // 'all_pid'=>$all_pid,
- // 'all_menuids'=>$all_menuids,
- // ]);
- return Result::success($all_menu);
- }else{
- return Result::error("没有权限",0,[]);
- }
- }
- }
|