where('sys_user_id', $admin_id)->where('is_delete', 0)->pluck('role_id'); if(empty($userRoleIds)) return []; $userRoleType = Role::whereIn('id', $userRoleIds)->where('is_delete', 0)->min('role_type'); if(!$userRoleType) return []; } $query = Users::query()->where("enable",1); /**管理员查看权限**/ if($is_system_admin == Users::SYSTEM_ADMIN){ //系统管理员只看到组管理员 $query->whereRaw("group_admin_id = id"); }else{ $query->where("group_admin_id",$group_admin_id); //非系统管理员只看到当前组的所有成员 } $total = $query->count(); $list = $query ->select("id","name","is_system_admin as is_super_admin","is_system_admin","create_time", 'is_promoter') ->offset(($page-1)*$page_limit) ->limit($page_limit) ->orderByRaw("is_system_admin desc,create_time asc") ->get()->toArray(); foreach ($list as $k=> $item){ $admin_role_name_arr = []; //拥有角色名称集合 $admin_manage_copy_name_arr = []; //管理主体ID if($item['is_system_admin'] != Users::SYSTEM_ADMIN){ //拥有的角色ID $manage_role_id_arr = AdminManageRole::query()->where("is_delete",0) ->where("view_type",$view_type) ->where("sys_user_id",$item['id']) ->pluck("role_id")->toArray(); //拥有角色 $adminRoleData = Role::select(['name', 'role_type'])->where("is_delete",0) ->whereIn("id",$manage_role_id_arr)->get(); $admin_role_name_arr = $adminRoleData->pluck("name")->toArray(); $adminRoleType = $adminRoleData->min('role_type'); //所管理主体 $admin_manage_copy_id_arr = AdminManageCorp::query()->where("is_delete",0) ->where("view_type",$view_type) ->where("sys_user_id",$item['id']) ->pluck("corpid")->toArray(); $admin_manage_copy_name_arr = AuthorizeCorp::query()->whereIn("id",$admin_manage_copy_id_arr) ->where("enable",1) ->pluck("corp_name")->toArray(); }else{ /*** * 超级管理员 */ } if(!$is_system_admin && ($admin_id != $sys_group_id)) { $list[$k]['can_del'] = $adminRoleType < $userRoleType ? 0: 1; } else { $list[$k]['can_del'] = $is_system_admin ? 1 : ($item['id'] == $sys_group_id ? 0 : 1); } $list[$k]['role'] = $admin_role_name_arr; $list[$k]['corp'] = $admin_manage_copy_name_arr; } return [$total,$list]; } /** * 管理员详情 * @param $view_type * @param $is_system_admin * @param $group_admin_id * @param $admin_id */ public static function admin_info($view_type, $is_system_admin, $group_admin_id, $admin_id){ $find = Users::where("id",$admin_id) ->where(function ($query) use ($is_system_admin,$group_admin_id){ if($is_system_admin != Users::SYSTEM_ADMIN) $query->where("group_admin_id",$group_admin_id); }) ->selectRaw('id, name, is_system_admin as is_super_admin, is_system_admin, create_time, is_promoter, is_all_adq, adq_account, is_all_mp, mp_account, can_export') ->where("enable",1)->first(); if(empty($find)) return self::PARAM_ERR; //补充角色 $find->role_id = AdminManageRole::where("is_delete",0)->where("view_type",$view_type) ->where("sys_user_id",$admin_id) ->pluck("role_id"); //补充主体 $find->corp_id = AdminManageCorp::query()->where("is_delete",0) ->where("view_type",$view_type) ->where("sys_user_id",$admin_id) ->pluck("corpid"); return $find; } /** * 拥有主体列表 * @param $group_admin_id * @param $admin_id * @param $is_system_admin * @return array */ public static function corp_list($group_admin_id,$admin_id,$is_system_admin){ $query = AuthorizeCorp::query(); //非超级管理员 if($is_system_admin != Users::SYSTEM_ADMIN){ $corp_id_arr = AdminManageCorp::query()->where("is_delete",0) ->where("sys_user_id",$admin_id) ->pluck("corpid")->toArray(); $query->whereIn("id",$corp_id_arr); } return $query->where("enable",1) ->select("id","corp_name") ->orderBy("id","asc") ->get()->toArray(); } /** * 可选角色 * @param $view_type */ public static function all_role_list($admin_id,$sys_group_id,$is_system_admin,&$errno) { $userRoleType = 0; if(!$is_system_admin && ($admin_id != $sys_group_id)) { # 获取当前登录用户的角色权限 $userRoleIds = AdminManageRole::select(['role_id'])->where('sys_user_id', $admin_id)->where('is_delete', 0)->pluck('role_id'); if(empty($userRoleIds)) return []; $userRoleType = Role::whereIn('id', $userRoleIds)->where('is_delete', 0)->min('role_type'); if(!$userRoleType) return []; } return Role::where("is_delete",0) ->where(function($query) use($is_system_admin, $sys_group_id, $admin_id) { if(!$is_system_admin) { $query->where('sys_group_id', $sys_group_id); } else { $query->where('sys_group_id', $admin_id); } }) ->where('role_type', '>=', $userRoleType) ->select("id","name", 'role_type') ->orderBy("role_type") ->get(); } /** * 创建管理员 * @param $view_type * @param $group_admin_id * @param $name * @param $password * @param $role_id_arr * @param $corpid_arr * @param $is_new_group [是否新建分组:true/false] * @return int */ public static function admin_create( $view_type, $group_admin_id, $name, $password, $role_id_arr , $corpid_arr ,$is_new_group, $is_promoter, $is_all_adq, $adq_account, $is_all_mp, $mp_account, $can_export ){ //检查用户名是否存在 $find = Users::where("enable",1)->where("name",$name)->first(); if(!empty($find)) return self::USER_NAME_EXIST; //创建用户 $sys_user_id = Users::create([ 'name' => $name, 'is_promoter' => $is_promoter, 'password' => Hash::make($password), 'group_admin_id' => $group_admin_id, 'is_all_adq' => $is_all_adq, 'adq_account' => $adq_account, 'is_all_mp' => $is_all_mp, 'mp_account' => $mp_account, 'can_export' => $can_export ])->id; //新建账号为新的分组 if($is_new_group){ Users::query()->where("id",$sys_user_id)->update(['group_admin_id' => $sys_user_id]); } //用户绑定所属主体 self::sys_user_bind_corp_id($view_type,$sys_user_id,$corpid_arr); //用户绑定角色 self::sys_user_bind_role($view_type,$sys_user_id,$role_id_arr); return self::SUCCESS_CODE; } /** * 系统用户绑定主体 * @param $view_type * @param $admin_id * @param $corp_id_arr */ private static function sys_user_bind_corp_id($view_type,$admin_id,$corp_id_arr){ //绑定主体前释放主体 AdminManageCorp::query()->where("sys_user_id",$admin_id) ->where("view_type",$view_type) ->update(['is_delete'=>1]); $is_system_admin = Users::query()->where("id",$admin_id)->value("is_system_admin"); //非超级管理员就增加主体绑定 if($is_system_admin!=Users::SYSTEM_ADMIN){ foreach ($corp_id_arr as $corpid){ AdminManageCorp::query()->updateOrInsert([ 'sys_user_id' => $admin_id, 'corpid' => $corpid, ],[ 'view_type' => $view_type, 'is_delete' => 0, ]); } } } /** * 系统用户绑定角色 * @param $view_type * @param $admin_id * @param $role_id_arr */ private static function sys_user_bind_role($view_type, $admin_id, $role_id_arr){ //绑定主体前释放主体 AdminManageRole::query()->where("sys_user_id",$admin_id) ->where("view_type",$view_type) ->update(['is_delete'=>1]); $is_system_admin = Users::query()->where("id",$admin_id)->value("is_system_admin"); //非超级管理员就增加主体绑定 if($is_system_admin!=Users::SYSTEM_ADMIN){ foreach ($role_id_arr as $corpid){ AdminManageRole::query()->updateOrInsert([ 'sys_user_id' => $admin_id, 'role_id' => $corpid, ],[ 'view_type' => $view_type, 'is_delete' => 0, ]); } } } /** * 编辑管理员 * @param $view_type * @param $group_admin_id * @param $admin_id * @param $password * @param $role_id_arr * @param $corpid_arr * @return int */ public static function admin_edit( $view_type, $group_admin_id, $admin_id, $password, $role_id_arr , $corpid_arr, $is_promoter, $is_all_adq, $adq_account, $is_all_mp, $mp_account, $can_export ){ /**检查用户是否正确存在**/ $find = Users::where("id",$admin_id)->where("enable",1)->first(); if(empty($find)) return self::PARAM_ERR; /**保存修改**/ $save_user_data = [ 'is_promoter' =>$is_promoter, 'is_all_adq' => $is_all_adq, 'adq_account' => $adq_account, 'is_all_mp' => $is_all_mp, 'mp_account' => $mp_account, 'can_export' => $can_export, ]; //是否修改密码 if(!empty($password)){ $save_user_data['password'] = Hash::make($password); } Users::where("id",$admin_id)->where("enable",1)->update($save_user_data); //调整绑定主体 self::sys_user_bind_corp_id($view_type,$admin_id,$corpid_arr); //调整绑定角色 self::sys_user_bind_role($view_type,$admin_id,$role_id_arr); return self::SUCCESS_CODE; } /** * 管理员删除 * @param $is_system_admin * @param $group_admin_id [所属管理员分组] * @param $admin_id [要删除的管理员ID] * @return int */ public static function admin_del($is_system_admin,$group_admin_id, $sys_user_id, $sys_group_id, $admin_id) { if(!$is_system_admin && ($admin_id != $sys_group_id)) { return 4506; } $find = Users::query() ->where("id",$sys_user_id) ->where(function ($query) use ($is_system_admin,$group_admin_id){ if($is_system_admin != Users::SYSTEM_ADMIN) $query->where("group_admin_id",$group_admin_id); }) ->where("enable",1) ->first(); if(empty($find)) return self::PARAM_ERR; \DB::begintransaction(); $res1 = $find->update(['enable'=>0]); //将授予角色、主体关系删除 $manageRoleCount = AdminManageRole::query()->where("sys_user_id",$sys_user_id)->count(); if($manageRoleCount > 0) { $res2 = AdminManageRole::query()->where("sys_user_id",$sys_user_id)->update(['is_delete'=>1]); } else { $res2 = true; } $manageCorpCount = AdminManageCorp::query()->where("sys_user_id",$sys_user_id)->count(); if($manageCorpCount > 0) { $res3 = AdminManageCorp::query()->where("sys_user_id",$sys_user_id)->update(['is_delete'=>1]); } else { $res3 = true; } # 如果为投手,则将投手绑定关系状态修改 $relaCount = DramaUserRela::query()->where('user_id', $sys_user_id)->where('enable', 1)->count(); if($relaCount > 0) { $res4 = DramaUserRela::query()->where('user_id', $sys_user_id)->where('enable', 1) ->update(['is_show' => 0, 'disable_date' => date('Y-m-d')]); } else { $res4 = true; } if($res1 && $res2 && $res3 && $res4) { \DB::commit(); return self::SUCCESS_CODE; } else { \DB::rollBack(); return self::ERR_CODE; } } /** * 获取拥有"投手"角色的账号列表,不包括超级管理员 * @param false $corp_id_str [非必传参数。不传时查看全部主体。传入corpid时,代表查询具体主体下的投手账号列表] */ public static function get_toushou_user_list($corp_id_str = false){ //投手角色ID $role_id = Role::toushou_role_id(); //所有投手角色用户ID $all_toushou_admin_id_arr = AdminManageRole::query()->where("is_delete",0) ->where("view_type","helper") ->where("role_id",$role_id) ->pluck("sys_user_id")->toArray(); /**查询账号**/ $query = Users::query()->where("enable",1) ->where("is_system_admin",0) ->whereIn("id",$all_toushou_admin_id_arr); //是否查询指定主体 if($corp_id_str!==false){ $corp_id = AuthorizeCorp::query()->where("enable",1) ->where("corpid",$corp_id_str) ->value("id"); $manage_corp_admin_id_arr = AdminManageCorp::query()->where("is_delete",0) ->where("corpid",$corp_id) ->pluck("sys_user_id")->toArray(); $query->whereIn("id",$manage_corp_admin_id_arr); //查询拥有主体管理权限的用户 } //返回列表 return $query->select("id","name") ->get()->toArray(); } /** * 获取管理的账号组列表 * @param $group_admin_id * @param $is_system_admin */ public static function manage_account_group_list($group_admin_id,$is_system_admin){ /**整理账号组列表**/ $query = Users::query()->where("is_system_admin",0) ->where("enable",1) ->whereRaw("id=group_admin_id"); //只查询组管理员 if($is_system_admin != Users::SYSTEM_ADMIN){ //非系统管理员只查询当前组下的管理员列表 $query->where("group_admin_id",$group_admin_id); } $admin_group_list = $query->select("id as group_id","name as group_name")->get()->keyBy("group_id")->toArray(); /**账号组列表涉及到的主体列表**/ $manage_corp_id_arr = []; foreach ($admin_group_list as $group_id =>$item){ $manage_corp_id_arr[$group_id] = AdminManageCorp::query() ->where("is_delete",0) ->where("sys_user_id",$group_id) ->pluck("corpid")->toArray(); } foreach ($admin_group_list as $group_id=>$item){ $manage_corp_list = []; if(!empty($manage_corp_id_arr[$group_id])){ $manage_corp_list = AuthorizeCorp::query() ->whereIn("id",$manage_corp_id_arr[$group_id]) ->where("enable",1) ->select("corp_name","corpid","id") ->get()->keyBy("id")->toArray(); foreach ($manage_corp_list as $corpid=>$corpinfo){ $manage_corp_list[$corpid]['corp_name_alias'] = $item['group_name'] . ' — '.$corpinfo['corp_name']; unset($manage_corp_list[$corpid]['id']); } } $admin_group_list[$group_id]['manage_corp_list'] = array_values($manage_corp_list); } return array_values($admin_group_list); } public static function isSuperUser($isSystemAdmin, $sysGroupId, $adminId) { if(1 == $isSystemAdmin) return true; if($adminId == $sysGroupId) return true; # 查询公司账号下所有超级权限的角色ID $superRoleIdList = Role::getSuperRoleIdList($sysGroupId); if(empty($superRoleIdList)) return false; # 查询具有超级权限的账号ID $superUserIdList = AdminManageRole::getSuperUserIdList($superRoleIdList); if(empty($superUserIdList)) return false; # 再次通过账号表查询一次 $finalSuperUserIdList = Users::getCorpUserIdList($sysGroupId, $superUserIdList); if(empty($finalSuperUserIdList)) return false; if(in_array($adminId, $finalSuperUserIdList)) return true; return false; } public static function getCorpBindUserList($corpId, $viewType, $groupAdminId, $isSystemAdmin) { $sysUserIdList = AdminManageCorp::getBindUserIdListByCorpId($viewType, $corpId); $query = Users::query()->whereIn('id', $sysUserIdList)->where('enable', 1); /**管理员查看权限**/ if($isSystemAdmin == Users::SYSTEM_ADMIN){ //系统管理员只看到组管理员 $query->whereRaw("group_admin_id = id"); }else{ $query->where("group_admin_id",$groupAdminId); //非系统管理员只看到当前组的所有成员 } return $query->selectRaw('id as user_id, name as user_name')->get(); } public static function corpBindMultipleUser($corpId, $userIdList, $viewType) { try{ $systemAdminList = Users::getSystemAdminIdList(); //绑定主体前释放主体 AdminManageCorp::query()->where("corpid",$corpId) ->where("view_type",$viewType) ->update(['is_delete'=>1]); //非超级管理员就增加主体绑定 foreach ($userIdList as $userId){ if(in_array($userId, $systemAdminList)) { continue; } AdminManageCorp::query()->updateOrInsert([ 'sys_user_id' => $userId, 'corpid' => $corpId, ],[ 'view_type' => $viewType, 'is_delete' => 0, ]); } return ['成功', 0]; } catch (\Exception $exception) { Log::logError('企微批量绑定成员异常', [ 'file' => $exception->getFile(), 'line' => $exception->getLine(), 'msg' => $exception->getMessage(), 'trace' => $exception->getTraceAsString(), ], 'interface'); return ['系统异常', 500]; } } }