123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- <?php
- /**
- * Created by:PhpStorm
- * Author:chenzhiyuan
- * Date: 2022/4/19
- * Time: 1:49 下午
- */
- namespace App\Service\Admin;
- use App\Log;
- use App\Models\System\AdminManageCorp;
- use App\Models\System\AdminManageRole;
- use App\Models\System\Permission;
- use App\Models\System\RolePermission;
- use App\Models\System\Users;
- class PermissionService
- {
- CONST ROUTE_EXIST = 4400,
- PARENT_ID_NOT_EXIST = 4401,
- SORT_ERROR = 4402,
- NOT_FOUND = 4403,
- SUCCESS = 0;
- /**
- * 获取权限树
- * @param $view_type
- * @param int $parent_id
- * @param int $level
- * @param false $only_permission_id_arr [false:查看全部权限。权限集合数组:只查看特定的权限]
- * @return array
- */
- public static function permission_tree($view_type,$parent_id = 0,$level = 1,$only_permission_id_arr = false){
-
- $query = Permission::query()->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));
- }
- }
|