ChangeType); $corpid = trim($msgObj->AuthCorpId); if(empty($corpid)) { $corpid = trim($msgObj->ToUserName); } $openUserId = trim($msgObj->OpenUserID); $userId = trim($msgObj->UserID); switch($changeType) { case 'create_user': // 通讯录-新增成员事件 # 获取成员详情 $userDetail = ContactService::getContactUserDetail($corpid, $userId); if($userDetail === false) { Log::logError('获取通讯录成员数据回收失败', ['corpid' => $corpid, 'userId'=>$userId], 'ChangeContactEvent-CU'); return false; } $insertData = [ 'enable' => 1, 'is_open_contract' => 1, ]; $insertData['user_id'] = $userId; if(isset($userDetail['name'])) $insertData['name'] = $userDetail['name']; if(isset($userDetail['department'])) $insertData['department'] = implode(',', $userDetail['department']); if(isset($userDetail['order'])) $insertData['order'] = implode(',', $userDetail['order']); if(isset($userDetail['gender'])) $insertData['gender'] = $userDetail['gender']; if(isset($userDetail['avatar'])) $insertData['avatar'] = $userDetail['avatar']; if(isset($userDetail['status'])) $insertData['status'] = $userDetail['status']; if(isset($userDetail['main_department'])) $insertData['main_department'] = $userDetail['main_department']; if(isset($userDetail['is_leader_in_dept'])) $insertData['is_leader_in_dept'] = implode(',', $userDetail['is_leader_in_dept']); if(isset($userDetail['thumb_avatar'])) $insertData['thumb_avatar'] = $userDetail['thumb_avatar']; if(isset($userDetail['open_user_id'])) $insertData['open_user_id'] = $userDetail['open_user_id']; if(isset($userDetail['direct_leader'])) $insertData['direct_leader'] = implode(',', $userDetail['direct_leader']); if(isset($userDetail['position'])) $insertData['position'] = $userDetail['position']; if(isset($userDetail['mobile'])) $insertData['mobile'] = $userDetail['mobile']; if(isset($userDetail['email'])) $insertData['email'] = $userDetail['email']; if(isset($userDetail['isleader'])) $insertData['is_leader'] = $userDetail['isleader']; if(isset($userDetail['enable'])) $insertData['wx_enable'] = $userDetail['enable']; if(isset($userDetail['hide_mobile'])) $insertData['hide_mobile'] = $userDetail['hide_mobile']; if(isset($userDetail['telephone'])) $insertData['telephone'] = $userDetail['telephone']; if(isset($userDetail['qr_code'])) $insertData['qr_code'] = $userDetail['qr_code']; if(isset($userDetail['alias'])) $insertData['alias'] = $userDetail['alias']; if(isset($userDetail['extattr'])) $insertData['extattr'] = json_encode($userDetail['extattr']); $result = DjUser::updateOrCreate(['corpid' => $corpid, 'user_id' => $userId], $insertData); if(!$result) { // 插入数据库失败后将数据插入到Redis $saveResult = RedisModel::lPush(DjUser::USER_SAVE_FAIL_RDS, json_encode($insertData)); if(!$saveResult) { Log::logError('添加通讯录成员数据回收失败', $insertData, 'ChangeContactEvent-CU'); return false; } } break; case 'update_user': // 通讯录-更新成员事件 $insertData['corpid'] = $corpid; $insertData['enable'] = 1; $insertData['open_user_id'] = $openUserId; $insertData['status'] = trim($msgObj->Status); $insertData['user_id'] = trim($msgObj->NewUserID); $insertData['gender'] = trim($msgObj->Gender); $insertData['name'] = trim($msgObj->Name); $insertData['alias'] = trim($msgObj->Alias); $insertData['avatar'] = trim($msgObj->Avatar); $insertData = array_filter($insertData); $result = DjUser::updateOrCreate([ 'corpid' => $corpid, 'user_id' => $userId ], $insertData); if(!$result) { // 更新至数据库失败后将数据插入到Redis $saveResult = RedisModel::lPush(DjUser::USER_UPDATE_FAIL_RDS, json_encode($insertData)); if(!$saveResult) { Log::logError('更新通讯录成员数据回收失败', $insertData, 'ChangeContactEvent-UU'); return false; } } break; case 'delete_user': // 通讯录-删除成员事件 try { $result = ContactService::deleteUser($corpid, $userId); if($result===false) { // 删除成员数据处理失败回收至Redis $recycleData = ['corpid' => $corpid, 'user_id' => $userId]; $saveResult = RedisModel::lPush(DjUser::USER_DELETE_FAIL_RDS, json_encode($recycleData)); if(!$saveResult) { Log::logError('更新通讯录成员数据回收失败', $recycleData, 'ChangeContactEvent-DU'); return false; } } } catch (\Exception $e) { Log::logError('删除成员事件处理发生异常', [ 'line' => $e->getLine(), 'msg' => $e->getMessage() ], 'DeleteUserCallBack'); } break; } return true; } /** * 成员删除相关处理 * */ public static function deleteUser($corpid, $userId) { try { DB::beginTransaction(); # 修改成员对应外部联系人关系表 CustomerDetails::suffix($corpid)->where('corpid', $corpid)->where('user_id', $userId) ->update(['loss_status'=>2, 'enable'=>0]); # 修改成员表 $userInfo = DjUser::where('user_id', $userId)->where('corpid', $corpid)->first(); if(empty($userInfo)) { EmailQueue::rPush('成员离职事件信息中的成员不存在', json_encode(['corpid' => $corpid, 'user_id' => $userId]), ['xiaohua.hou@kuxuan-inc.com'], '猎羽'); return false; } if($userInfo->is_active == 1) { // 离职成员已分配的许可未处理,进行自动转移 # 查询一个接替成员的信息 $takeoverUser = DjUser::select('user_id')->where('is_active', 0)->where('enable', 1)->where('status', 1)->first(); if(empty($takeoverUser)) { // 无可用接替成员 EmailQueue::rPush('无可用接替成员', json_encode(['corpid' => $corpid, 'user_id' => $userId]), ['xiaohua.hou@kuxuan-inc.com'], '猎羽'); } else { $transferResult = QyCommon::batchTransferLicense($corpid, array([ 'handover_userid' => $userId, 'takeover_userid' => $takeoverUser->user_id ])); EmailQueue::rPush('离职自动转移许可完成', json_encode(['corpid' => $corpid, 'user_id' => $userId, 'takeover_user_id' => $takeoverUser->user_id, 'response' => $transferResult]), ['xiaohua.hou@kuxuan-inc.com'], '猎羽'); } } $userInfo->status = 5; $userInfo->new_user_id = null; $userInfo->enable = 0; $userInfo->is_active = 0; $result = $userInfo->save(); if(!$result) { DB::rollBack(); return false; } DB::commit(); } catch (\Exception $e) { DB::rollBack(); Log::logError('删除成员事件处理发生异常', [ 'line' => $e->getLine(), 'msg' => $e->getMessage(), 'corpid' => $corpid, 'userId' => $userId ], 'DeleteUserDB'); EmailQueue::rPush('删除成员事件处理发生异常', $e->getTraceAsString(), ['xiaohua.hou@kuxuan-inc.com'], '猎羽'); return false; } return true; } /** * 获取企业员工信息 * */ public static function getContactUserDetail($corpid, $userid, $retry=0) { # 获取SuiteAccessToken $accessToken = AuthorizeCorp::getAccessToken($corpid, '获取企业员工信息'); if(empty($accessToken)) { // 令牌获取失败,发送报警 return false; } # 获取外部联系人详情信息API $getExternalContactUri = config('qyWechat.get_user_detail'); $getExternalContactUri .= $accessToken . '&userid=' . $userid; $response = HttpService::httpGet($getExternalContactUri); $responseData = json_decode($response, true); if(isset($responseData['errcode']) && $responseData['errcode']) { if($retry <=5) { $retry++; ContactService::getContactUserDetail($corpid, $userid, $retry); } Log::logError('外部联系人详情信息获取失败', $responseData, 'getContactUserDetail'); return false; } return $responseData; } }