123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- <?php
- namespace App\Service;
- use App\Log;
- use App\Models\AccountLicense;
- use App\Models\AuthorizeCorp;
- use App\Models\DjUser;
- use App\Models\System\AdminManageRole;
- use App\Models\System\Role;
- use App\RedisModel;
- use App\Support\EmailQueue;
- use App\Support\qyApi\QyCommon;
- use Illuminate\Support\Facades\DB;
- class LicenseService
- {
- /**
- * 获取企微可使用的许可数
- * */
- public static function getLicenseCount($corpid)
- {
- return AccountLicense::where('corpid', $corpid)
- ->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]
- ]));
- } 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;
- }
- }
|