No Description

ChatGroupService.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace App\Service;
  3. use App\Log;
  4. use App\Models\ChatGroup;
  5. use App\RedisModel;
  6. class ChatGroupService
  7. {
  8. // 客户群事件回调处理
  9. public static function templateCallBack($msgStr)
  10. {
  11. try{
  12. $msgObj = simplexml_load_string($msgStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  13. $changeType = trim($msgObj->ChangeType);
  14. $params['corpid'] = trim($msgObj->ToUserName);
  15. $params['chat_id'] = trim($msgObj->ChatId);
  16. $params['change_type'] = $changeType;
  17. if(empty($params['corpid']) || empty($params['chat_id']) || empty($params['change_type'])) {
  18. Log::logError('客户群回调数据不合法', [
  19. 'params' => $msgStr,
  20. 'err_msg' => '回调参数不合法',
  21. ], 'ChatGroupService');
  22. return false;
  23. }
  24. $params['update_detail'] = isset($msgObj->UpdateDetail) ? trim($msgObj->UpdateDetail) : null;
  25. $params['create_time'] = isset($msgObj->CreateTime) ? trim($msgObj->CreateTime) : null;
  26. $params['join_scene'] = isset($msgObj->JoinScene) ? trim($msgObj->JoinScene) : null;
  27. $params['quit_scene'] = isset($msgObj->QuitScene) ? trim($msgObj->QuitScene) : null;
  28. $params['mem_change_cnt'] = isset($msgObj->MemChangeCnt) ? trim($msgObj->MemChangeCnt) : null;
  29. $params['request_time'] = time()+5;
  30. $params['status'] = null;
  31. switch($changeType) {
  32. case 'create' : // 客户群创建
  33. case 'update': // 客户群变更
  34. $res = RedisModel::lPush(ChatGroup::CHAT_GROUP_DETAIL_RDS, json_encode($params));
  35. break;
  36. case 'dismiss': // 客户群解散
  37. $res = ChatGroup::query()
  38. ->where('corpid', $params['corpid'])
  39. ->where('chat_id', $params['chat_id'])
  40. ->update(['status' => 2, 'updated_at' => date('Y-m-d H:i:s')]);
  41. break;
  42. default:
  43. Log::logError('客户群事件类型不合法', [
  44. 'params' => $msgStr,
  45. 'err_msg' => '事件类型不合法',
  46. ], 'ChatGroupService');
  47. return false;
  48. }
  49. if(!$res) {
  50. Log::logError('客户群回调事件处理失败', [
  51. 'params' => $msgStr,
  52. ], 'ChatGroupService');
  53. return false;
  54. }
  55. return true;
  56. } catch (\Exception $exception){
  57. Log::logError('客户群回调事件处理过程发生异常', [
  58. 'params' => $msgStr,
  59. 'file' => $exception->getFile(),
  60. 'line' => $exception->getLine(),
  61. 'message' => $exception->getMessage(),
  62. 'trace' => $exception->getTraceAsString(),
  63. ], 'ChatGroupService');
  64. return false;
  65. }
  66. }
  67. /**
  68. * 获取客户群详情
  69. * */
  70. public static function getGroupChatDetail($corpid, $chatId, $needName=1, $retry=0)
  71. {
  72. $accessToken = AccessTokenService::getAccessToken($corpid, '客户群基本信息获取');
  73. if(empty($accessToken)) {
  74. Log::logError('客户群详情获取', [
  75. 'params' => ['corpid' => $corpid],
  76. 'errmsg' => '令牌获取失败'
  77. ], 'GetGroupChatDetail');
  78. return false;
  79. }
  80. $url = config('qyWechat.group_chat_detail') . $accessToken;
  81. $postData = [
  82. 'chat_id' => $chatId,
  83. 'need_name' => $needName
  84. ];
  85. $response = HttpService::httpPost($url, json_encode($postData));
  86. if($response === false && $retry <=5) { // 发起重试
  87. $retry++;
  88. return self::getGroupChatDetail($corpid, $chatId, $needName, $retry);
  89. }
  90. $responseData = json_decode($response, true);
  91. Log::logInfo('getGroupChatDetail', (array)$responseData, 'qyWechat');
  92. return $responseData;
  93. }
  94. /**
  95. * 客户群基本信息获取
  96. * */
  97. public static function getGroupChatList($corpid, $ownerFilter, $limit, $cursor, $statusFilter=0, $retry=0)
  98. {
  99. $accessToken = AccessTokenService::getAccessToken($corpid, '客户群基本信息获取');
  100. if(empty($accessToken)) {
  101. Log::logError('客户群基本信息获取', [
  102. 'params' => ['corpid' => $corpid],
  103. 'errmsg' => '令牌获取失败'
  104. ], 'qyWechat');
  105. return false;
  106. }
  107. $url = config('qyWechat.group_chat_list') . $accessToken;
  108. $postData = [
  109. 'status_filter' => $statusFilter,
  110. 'cursor' => $cursor,
  111. 'limit' => $limit
  112. ];
  113. $response = HttpService::httpPost($url, json_encode($postData));
  114. if($response === false && $retry <=5) { // 发起重试
  115. $retry++;
  116. return self::getGroupChatList($corpid, $ownerFilter, $limit, $cursor, $statusFilter, $retry);
  117. }
  118. $responseData = json_decode($response, true);
  119. Log::logInfo('getGroupChatList', (array)$responseData, 'GetGroupChatList');
  120. return $responseData;
  121. }
  122. }