123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568 |
- <?php
- /**
- * Created by:PhpStorm
- * Author:chenzhiyuan
- * Date: 2022/4/19
- * Time: 11:34 上午
- */
- namespace App\Service\Admin;
- use App\Log;
- use App\Models\AuthorizeCorp;
- use App\Models\DramaUserRela;
- use App\Models\OfficialAccount;
- use App\Models\System\AdminManageCorp;
- use App\Models\System\AdminManageRole;
- use App\Models\System\Role;
- use App\Models\System\Users;
- use App\User;
- use Illuminate\Support\Facades\Hash;
- class AdminService
- {
- CONST USER_NAME_EXIST = 4404, //用户名已经存在
- PARAM_ERR = 4403, //请求参数错误
- SUCCESS_CODE = 0, //请求成功
- ERR_CODE = 500; // 更新数据失败
- CONST VIEW_TYPE = [
- 'helper', //助手看板
- 'data', //数据看板
- ];
- /***
- * 查询管理员的列表接口
- * @param $view_type
- * @param $is_system_admin
- * @param $group_admin_id
- * @param $page
- * @param $page_limit
- * @return array
- */
- public static function admin_list($view_type,$is_system_admin,$admin_id,$sys_group_id,$group_admin_id,$page,$page_limit,&$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 [];
- }
- $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];
- }
- }
- }
|