where("is_delete",0) ->where("view_type",$view_type) ->where("parent_id",$parent_id); //是否只查看特定权限 if($only_permission_id_arr!==false){ $query->whereIn("id",$only_permission_id_arr); } $list = $query->select("id","title","route","icon") ->orderBy("weight") ->get() ->toArray(); if(empty($list)) return []; foreach ($list as $k => $item){ $list[$k]['level'] = $level; $list[$k]['sub'] = self::permission_tree($view_type,$item['id'],++$level,$only_permission_id_arr); } return $list; } /** * 权限列表 * @param $view_type * @param $parent_id * @return array */ public static function permission_list($view_type,$parent_id){ $list = Permission::query()->where("parent_id",$parent_id) ->where("view_type",$view_type) ->where("is_delete",0) ->select("id","title","parent_id","route","icon","created_at") ->orderBy("weight","asc") ->get()->toArray(); foreach ($list as $k => $item){ //统计子权限数量 $list[$k]['sub_num'] = Permission::query()->where("parent_id",$item['id']) ->where("view_type",$view_type) ->where("is_delete",0) ->count(); } return $list; } /** * 删除权限 * @param $view_type * @param $id */ public static function del_permission($view_type,$id) { /**删除指定权限**/ $find = Permission::query()->where("is_delete",0) ->where("view_type",$view_type) ->where("id",$id) ->first(); if(empty($find)) return self::NOT_FOUND; Permission::query()->where("id",$id)->update(['is_delete'=>1]); //todo 删除权限引用 /**删除子集权限**/ $sub = Permission::query()->where("is_delete") ->where("view_type",$view_type) ->where("parent_id",$id) ->get()->toArray(); foreach ($sub as $item){ self::del_permission($view_type,$item['id']); //递归删除 } return self::SUCCESS; } /** * 创建权限 * @param $view_type * @param $admin_id * @param $parent_id * @param $title * @param $route * @param $weight * @param $icon */ public static function create_permission($view_type,$admin_id,$parent_id,$title,$route,$weight,$icon){ //检查上级id是否存在 if( $parent_id != 0 ){ $check_parent_id = Permission::query() ->where("view_type",$view_type) ->where("id",$parent_id) ->where("is_delete",0) ->first(); if(empty($check_parent_id)) return self::PARENT_ID_NOT_EXIST; } //检查路由地址是否已存在 // $find = Permission::query()->where("root_admin_id",$root_admin_id) // ->where("route",$route) // ->where("is_delete",0) // ->first(); // if(!empty($find)) return self::ROUTE_EXIST; Permission::query()->create( [ 'view_type' => $view_type, 'title' => trim($title), 'parent_id' => $parent_id, 'route' => trim($route), 'icon' => trim($icon), 'weight' => $weight, 'create_admin_id' => $admin_id ] ); return self::SUCCESS; } /** * 编辑权限(顶级) * @param $view_type * @param $id * @param $title * @param $route * @param $icon */ public static function edit_permission_l1($view_type,$id, $title, $route, $icon){ $find = Permission::query()->where("id",$id) ->where("view_type",$view_type) ->where("parent_id",0) ->first(); if(empty($find)) return self::NOT_FOUND; Permission::query()->where("id",$id) ->update([ 'title' => trim($title), 'route' => trim($route), 'icon' => trim($icon) ]); return self::SUCCESS; } /** * 编辑权限(顶级) * @param $view_type * @param $id * @param $title * @param $route */ public static function edit_permission_l2($view_type,$id, $title, $route){ $find = Permission::query()->where("id",$id) ->where("view_type",$view_type) ->where("parent_id","!=",0) ->first(); if(empty($find)) return self::NOT_FOUND; Permission::query()->where("id",$id) ->update([ 'title' => trim($title), 'route' => trim($route), ]); return self::SUCCESS; } /*** * 对权限进行排序设置 * @param $view_type * @param $parent_id * @param $permission_id_arr */ public static function be_sort($view_type,$parent_id,$permission_id_arr){ $count = Permission::query() ->where("view_type",$view_type) ->where("parent_id",$parent_id) ->where("is_delete",0) ->count(); if($count != count($permission_id_arr)){ return self::SORT_ERROR; } foreach ($permission_id_arr as $num => $p_id){ Permission::query()->where("id",$p_id) ->where("parent_id",$parent_id) ->where("is_delete",0) ->update(['weight'=>$num]); } return self::SUCCESS; } /** * 获取用户的权限集合 * @param $view_type * @param $admin_id * @param $is_system_user */ public static function user_permission($view_type, $admin_id, $is_system_user,$sys_group_id) { $only_permission_id_arr = false; /**非超级用户根据用户角色,找到角色对应的所有权限**/ if($is_system_user != Users::SYSTEM_ADMIN){ # 判断账号是否已授权企微 $hasAccount = AdminManageCorp::query() ->where("sys_user_id", $sys_group_id) ->where("is_delete",0) ->exists(); if($hasAccount) { # 获取用户可操作的权限集 $base_permission_id_arr = RoleService::getPermissionIdsOfUser($admin_id, $sys_group_id); } else { $base_permission_id_arr = RoleService::getPermissionIdsOfUser($admin_id, $sys_group_id, [24, 20]); } if(!is_array($base_permission_id_arr)) $base_permission_id_arr = $base_permission_id_arr->toArray(); //补充权限集的上一集 $parent_permission_id_arr = self::get_parent_permission_id($view_type,$base_permission_id_arr); //补充公共菜单 $public_permission_id_arr = Permission::query()->where("is_delete",0) ->where("view_type",$view_type) ->where("public",1) ->pluck("id")->toArray(); $parent_permission_id_arr = array_merge($public_permission_id_arr,$parent_permission_id_arr); //汇总权限集 $only_permission_id_arr = array_unique(array_merge($base_permission_id_arr,$parent_permission_id_arr)); } return self::permission_tree($view_type,0,1,$only_permission_id_arr); } /** * 获取所有的上级权限列表 * @param $view_type * @param $sub_permission_id_arr * @return array */ public static function get_parent_permission_id($view_type,$sub_permission_id_arr){ if(empty($sub_permission_id_arr)){ return []; } $parent_permission_id_arr = Permission::query()->where("is_delete",0) ->where("view_type",$view_type) ->whereIn("id",$sub_permission_id_arr) ->pluck("parent_id")->toArray(); return array_merge($parent_permission_id_arr,self::get_parent_permission_id($view_type,$parent_permission_id_arr)); } }