where('is_used', 0) ->where('enable', 1) ->count(); } /** * 分配许可激活账号 * */ public static function activeAccount($corpid, $userid, $adminId, $sysGroupId, $isSysAdmin, &$errMsg) { try { # 判断是否有管理权限 if(!$isSysAdmin) { if($adminId != $sysGroupId) { # 获取当前用户的角色信息 $roleIds = AdminManageRole::select('role_id')->where('sys_user_id', $adminId) ->where('is_delete', 0)->distinct()->pluck('role_id'); if(empty($roleIds)) return 5229; # 判断当前用户是否有超级权限 $isAdminAuth = Role::whereIn('id', $roleIds)->where('role_type', 10)->exists(); if(!$isAdminAuth) return 5229; } } DB::beginTransaction(); $userInfo = DjUser::where('user_id', $userid)->where('corpid', $corpid)->whereIn('status', [1, 4])->first(); if(empty($userInfo)) { DB::rollBack(); return 5236; } # 查询可用的activeCode $activeCodeInfo = AccountLicense::where('corpid', $corpid)->where('is_used', 0)->where('enable', 1)->first(); $activeCode = $activeCodeInfo->active_code ?? null; if(!$activeCode) { DB::rollBack(); return 5230; } $userInfo->is_active = 1; $userInfo->active_code = $activeCode; $userInfo->active_time = date('Y-m-d H:i:s'); $userInfo->expire_time = null; $result = $userInfo->save(); if(!$result) { DB::rollBack(); EmailQueue::rPush('修改客服激活信息失败', $corpid . '-' . $userid, ['xiaohua.hou@kuxuan-inc.com'], '猎羽'); Log::logError('客服激活时调用接口返回错误码', [ 'corpid' => $corpid, 'userid' => $userid, 'active_code' => $activeCode, ], 'ActiveAccountByLicense'); return 5231; } # 修改许可状态为已使用 $activeCodeInfo->is_used = 1; $result = $activeCodeInfo->save(); if(!$result) { DB::rollBack(); EmailQueue::rPush('修改许可可用状态失败', $activeCode, ['xiaohua.hou@kuxuan-inc.com'], '猎羽'); Log::logError('修改许可可用状态失败', [ 'corpid' => $corpid, 'userid' => $userid, 'active_code' => $activeCode, 'result' => $result ], 'ActiveAccountByLicense'); return 5231; } # 激活 $responseData = QyCommon::activeAccountByLicense($activeCode, $corpid, $userid); if(isset($responseData['errcode']) && $responseData['errcode']) { DB::rollBack(); $errMsg = $responseData['errmsg'] ?? $responseData['errcode']; if(!in_array($responseData['errcode'], [701082, 701024])) { EmailQueue::rPush('客服激活时调用接口返回错误码', $errMsg, ['xiaohua.hou@kuxuan-inc.com'], '猎羽'); } Log::logError('客服激活时调用接口返回错误码', [ 'corpid' => $corpid, 'userid' => $userid, 'active_code' => $activeCode, 'response' => $responseData ], 'ActiveAccountByLicense'); return 5231; } DB::commit(); # 主动在同步客户列表队列头部塞入当前客服 RedisModel::rPush(AuthorizeCorp::SYNC_CORP_CUSTOMER,json_encode([ 'corpid' => $corpid, 'user_list' => [$userid] ])); # 主动在同步客户群队列头部塞入当前客服 RedisModel::rPush(ChatGroup::CHAT_GROUP_BASIC_INFO_RDS, json_encode(['corpid' => $corpid, 'user_list' => [$userid], 'source' => '分配许可'])); } catch (\Exception $e) { DB::rollBack(); EmailQueue::rPush('分配许可激活账号发生异常', $e->getTraceAsString(), ['xiaohua.hou@kuxuan-inc.com'], '猎羽'); Log::logError('分配许可激活账号发生异常', [ 'corpid' => $corpid, 'userid' => $userid, 'line' => $e->getLine(), 'msg' => $e->getMessage() ], 'ActiveAccountByLicense'); return 5231; } return 0; } /** * 批量分配许可 * */ public static function batchActiveAccount($corpid, $useridList, $adminId, $sysGroupId, $isSysAdmin, &$errMsg, &$success, &$failed) { try { # 判断是否有管理权限 if(!$isSysAdmin) { if($adminId != $sysGroupId) { # 获取当前用户的角色信息 $roleIds = AdminManageRole::select('role_id')->where('sys_user_id', $adminId) ->where('is_delete', 0)->distinct()->pluck('role_id'); if(empty($roleIds)) return 5229; # 判断当前用户是否有超级权限 $isAdminAuth = Role::whereIn('id', $roleIds)->where('role_type', 10)->exists(); if(!$isAdminAuth) return 5229; } } if(!is_array($useridList)) $useridList = explode(',', $useridList); # 获取需要使用的许可 $activeCodeCount = count(array_unique($useridList)); $activeCodeData = AccountLicense::select(['active_code', 'id']) ->where('corpid', $corpid)->where('is_used', 0)->where('enable', 1) ->limit($activeCodeCount)->get(); if($activeCodeCount != $activeCodeData->count()) return 5232; # 组装批量分配许可的请求体 $activeList = []; foreach ($activeCodeData as $key=>$activeCodeInfo) { $activeList[$key] = [ 'active_code' => $activeCodeInfo->active_code, 'userid' => $useridList[$key], ]; } Log::logInfo('本次批量分配组装的请求体为:', [ 'corpid' => $corpid, 'userid_list' => $useridList, 'active_list' => $activeList ], 'BatchActiveAccountByLicense'); # 执行分配 $responseData = QyCommon::batchActiveAccountByLicense($corpid, $activeList); if(isset($responseData['errcode']) && $responseData['errcode']) { $errMsg = $responseData['errmsg'] ?? $responseData['errcode']; EmailQueue::rPush('批量分配客服许可时调用接口返回错误码', $errMsg, ['xiaohua.hou@kuxuan-inc.com'], '猎羽'); Log::logError('客服批量激活时调用接口返回错误码', [ 'corpid' => $corpid, 'active_list' => $activeList, 'response' => $responseData ], 'BatchActiveAccountByLicense'); return 5231; } $activeResult = $responseData['active_result']; if(empty($activeResult)) { EmailQueue::rPush('批量分配客服返回的激活结果为空', $corpid, ['xiaohua.hou@kuxuan-inc.com'], '猎羽'); return 5234; } $activeCodeUsed = []; $errData = [701082 => '用户已激活,请勿重复激活']; foreach ($activeResult as $item) { if($item['errcode']) { $failed++; $errMsgMid['user_id'] = $item['userid']; $errMsgMid['errcode'] = isset($errData[$item['errcode']]) ? $errData[$item['errcode']] :$item['errcode']; $errMsgMid['user_name'] = DjUser::where('corpid', $corpid) ->where('user_id', $item['userid'])->where('status', 1)->value('name'); $errMsg[] = $errMsgMid; } else { $success++; $activeCodeUsed[] = $item['active_code']; # 更新用户激活状态 DjUser::where('corpid', $corpid)->where('user_id', $item['userid'])->whereIn('status', [1, 4]) ->update(['active_code' => $item['active_code'], 'is_active' => 1, 'active_time' => date('Y-m-d H:i:s'), 'expire_time' => null]); # 主动在同步客户列表队列头部塞入当前客服 RedisModel::rPush(AuthorizeCorp::SYNC_CORP_CUSTOMER,json_encode([ 'corpid' => $corpid, 'user_list' => [$item['userid']] ])); } } # 修改许可使用状态 if($activeCodeUsed) { AccountLicense::where('corpid', $corpid)->whereIn('active_code', $activeCodeUsed)->update(['is_used' => 1]); } } catch (\Exception $e) { EmailQueue::rPush('批量分配许可过程发生异常', $e->getTraceAsString(), ['xiaohua.hou@kuxuan-inc.com'], '猎羽'); Log::logError('批量分配许可过程发生异常', [ 'corpid' => $corpid, 'userid_list' => $useridList, 'line' => $e->getLine(), 'msg' => $e->getMessage(), ], 'BatchActiveAccountByLicense'); return 5233; } return 0; } /** * 许可转移 * */ public static function transfer($corpid, $handoverUserid, $takeoverUserid, $adminId, $sysGroupId, $isSysAdmin, &$errMsg) { $transferList = array( [ 'handover_userid' => $handoverUserid, 'takeover_userid' => $takeoverUserid ] ); try { # 判断是否有管理权限 if(!$isSysAdmin) { if($adminId != $sysGroupId) { # 获取当前用户的角色信息 $roleIds = AdminManageRole::select('role_id')->where('sys_user_id', $adminId) ->where('is_delete', 0)->distinct()->pluck('role_id'); if(empty($roleIds)) return 5229; # 判断当前用户是否有超级权限 $isAdminAuth = Role::whereIn('id', $roleIds)->where('role_type', 10)->exists(); if(!$isAdminAuth) return 5229; } } # 转移 $responseData = QyCommon::batchTransferLicense($corpid, $transferList); if(isset($responseData['errcode']) && $responseData['errcode']) { $errMsg = $responseData['errmsg'] ?? $responseData['errcode']; EmailQueue::rPush('批量转移客服许可时调用接口返回错误码', $errMsg, ['xiaohua.hou@kuxuan-inc.com'], '猎羽'); Log::logError('客服批量转移时调用接口返回错误码', [ 'corpid' => $corpid, 'transferList' => $transferList, 'response' => $responseData ], 'BatchTransferLicense'); return 5235; } $transferResult = $responseData['transfer_result']; if(empty($transferResult)) { EmailQueue::rPush('批量转移客服返回的激活结果为空', $corpid, ['xiaohua.hou@kuxuan-inc.com'], '猎羽'); return 5234; } foreach ($transferResult as $item) { if($item['errcode']) { $errMsg = $item['errcode']; return 5235; } else { # 更新用户激活状态 $handoverUserInfo = DjUser::where('corpid', $corpid)->where('user_id', $handoverUserid)->where('status', 1) ->first(); $handoverUserInfo->is_active = 0; $handoverUserInfo->save(); DjUser::where('corpid', $corpid)->where('user_id', $takeoverUserid)->whereIn('status', [1, 4]) ->update([ 'active_code' => $handoverUserInfo->active_code, 'is_active' => 1, 'active_time' => $handoverUserInfo->active_time, 'expire_time' => $handoverUserInfo->expire_time]); # 主动在同步客户列表队列头部塞入当前客服 RedisModel::rPush(AuthorizeCorp::SYNC_CORP_CUSTOMER,json_encode([ 'corpid' => $corpid, 'user_list' => [$takeoverUserid] ])); } } } catch (\Exception $e) { EmailQueue::rPush('批量转移客服许可时发生异常', $errMsg, ['xiaohua.hou@kuxuan-inc.com'], '猎羽'); Log::logError('批量转移客服许可时发生异常', [ 'corpid' => $corpid, 'transferList' => $transferList, 'line' => $e->getLine(), 'msg' => $e->getMessage() ], 'BatchTransferLicense'); return 5234; } return 0; } }