123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- namespace App\Service;
- use App\Log;
- use App\Models\ChatGroup;
- use App\RedisModel;
- class ChatGroupService
- {
- // 客户群事件回调处理
- public static function templateCallBack($msgStr)
- {
- try{
- $msgObj = simplexml_load_string($msgStr, 'SimpleXMLElement', LIBXML_NOCDATA);
- $changeType = trim($msgObj->ChangeType);
- $params['corpid'] = trim($msgObj->ToUserName);
- $params['chat_id'] = trim($msgObj->ChatId);
- $params['change_type'] = $changeType;
- if(empty($params['corpid']) || empty($params['chat_id']) || empty($params['change_type'])) {
- Log::logError('客户群回调数据不合法', [
- 'params' => $msgStr,
- 'err_msg' => '回调参数不合法',
- ], 'ChatGroupService');
- return false;
- }
- $params['update_detail'] = isset($msgObj->UpdateDetail) ? trim($msgObj->UpdateDetail) : null;
- $params['create_time'] = isset($msgObj->CreateTime) ? trim($msgObj->CreateTime) : null;
- $params['join_scene'] = isset($msgObj->JoinScene) ? trim($msgObj->JoinScene) : null;
- $params['quit_scene'] = isset($msgObj->QuitScene) ? trim($msgObj->QuitScene) : null;
- $params['mem_change_cnt'] = isset($msgObj->MemChangeCnt) ? trim($msgObj->MemChangeCnt) : null;
- $params['request_time'] = time()+5;
- $params['status'] = null;
- switch($changeType) {
- case 'create' : // 客户群创建
- case 'update': // 客户群变更
- $res = RedisModel::lPush(ChatGroup::CHAT_GROUP_DETAIL_RDS, json_encode($params));
- break;
- case 'dismiss': // 客户群解散
- $res = ChatGroup::query()
- ->where('corpid', $params['corpid'])
- ->where('chat_id', $params['chat_id'])
- ->update(['status' => 2, 'updated_at' => date('Y-m-d H:i:s')]);
- break;
- default:
- Log::logError('客户群事件类型不合法', [
- 'params' => $msgStr,
- 'err_msg' => '事件类型不合法',
- ], 'ChatGroupService');
- return false;
- }
- if(!$res) {
- Log::logError('客户群回调事件处理失败', [
- 'params' => $msgStr,
- ], 'ChatGroupService');
- return false;
- }
- return true;
- } catch (\Exception $exception){
- Log::logError('客户群回调事件处理过程发生异常', [
- 'params' => $msgStr,
- 'file' => $exception->getFile(),
- 'line' => $exception->getLine(),
- 'message' => $exception->getMessage(),
- 'trace' => $exception->getTraceAsString(),
- ], 'ChatGroupService');
- return false;
- }
- }
- /**
- * 获取客户群详情
- * */
- public static function getGroupChatDetail($corpid, $chatId, $needName=1, $retry=0)
- {
- $accessToken = AccessTokenService::getAccessToken($corpid, '客户群基本信息获取');
- if(empty($accessToken)) {
- Log::logError('客户群详情获取', [
- 'params' => ['corpid' => $corpid],
- 'errmsg' => '令牌获取失败'
- ], 'GetGroupChatDetail');
- return false;
- }
- $url = config('qyWechat.group_chat_detail') . $accessToken;
- $postData = [
- 'chat_id' => $chatId,
- 'need_name' => $needName
- ];
- $response = HttpService::httpPost($url, json_encode($postData));
- if($response === false && $retry <=5) { // 发起重试
- $retry++;
- return self::getGroupChatDetail($corpid, $chatId, $needName, $retry);
- }
- $responseData = json_decode($response, true);
- Log::logInfo('getGroupChatDetail', (array)$responseData, 'qyWechat');
- return $responseData;
- }
- /**
- * 客户群基本信息获取
- * */
- public static function getGroupChatList($corpid, $ownerFilter, $limit, $cursor, $statusFilter=0, $retry=0)
- {
- $accessToken = AccessTokenService::getAccessToken($corpid, '客户群基本信息获取');
- if(empty($accessToken)) {
- Log::logError('客户群基本信息获取', [
- 'params' => ['corpid' => $corpid],
- 'errmsg' => '令牌获取失败'
- ], 'qyWechat');
- return false;
- }
- $url = config('qyWechat.group_chat_list') . $accessToken;
- $postData = [
- 'status_filter' => $statusFilter,
- 'cursor' => $cursor,
- 'limit' => $limit
- ];
- $response = HttpService::httpPost($url, json_encode($postData));
- if($response === false && $retry <=5) { // 发起重试
- $retry++;
- return self::getGroupChatList($corpid, $ownerFilter, $limit, $cursor, $statusFilter, $retry);
- }
- $responseData = json_decode($response, true);
- Log::logInfo('getGroupChatList', (array)$responseData, 'GetGroupChatList');
- return $responseData;
- }
- }
|