123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?php
- namespace App\Service;
- use App\Log;
- use App\Models\AuthorizeCorp;
- class MassMsgCreateService
- {
- /**
- * 创建企业群发
- * @param $corpid string 授权方企业微信id
- * @param $externalUserIds array 客户的外部联系人id列表,仅在chat_type为single时有效,不可与sender同时为空,最多可传入1万个客户
- * @param $sender string 发送企业群发消息的成员userid,当类型为发送给客户群时必填
- * @param $content string 消息文本内容
- * @param $attachments array 附件,最多支持添加9个附件
- * @param $chatType string 群发任务的类型,默认为single,表示发送给客户,group表示发送给客户群
- * @param array $chatIdList 客户群id列表,仅在chat_type为group时有效,最多可一次指定2000个客户群。指定群id之后,收到任务的群主无须再选择客户群,仅对4.1.10及以上版本的企业微信终端生效
- * */
- public static function addMsgTemplate($corpid, $externalUserIds, $sender, $content, $attachments, $ruleId, $retry=0, $chatType='single', $chatIdList = [])
- {
- $accessToken = AuthorizeCorp::getAccessToken($corpid, '创建企业群发');
- if(empty($accessToken)) { // Todo::令牌获取失败,发送报警
- return false;
- }
- $postData = [
- 'chat_type' => $chatType,
- 'external_userid' => $externalUserIds,
- 'chat_id_list' => $chatIdList,
- 'sender' => $sender,
- 'text' => [
- 'content' => $content
- ],
- 'attachments' => $attachments
- ];
- $requestUri = config('qyWechat.send_message');
- $requestUri .= $accessToken;
- $response = HttpService::httpPost($requestUri, json_encode($postData));
- $responseData = json_decode($response, true);
- # 发起重试
- if($response===false || !isset($responseData['errcode']) || in_array($responseData['errcode'], [-1, 45009, 45033, 40007])) { // 企微系统繁忙,发起重试
- if($retry < 10) {
- sleep(1);
- Log::logInfo('企微系统繁忙,发起重试', [
- 'corpid' => $corpid,
- 'postData' => $postData,
- 'retry' => $retry,
- 'response' => $response
- ], 'AddMassMsgTemplate');
- $retry++;
- if(isset($responseData['errcode']) && $responseData['errcode'] == 40007) { // 企微返回素材media_id异常,重新处理附件信息
- $attachments = MaterialService::radarAttachment($attachments, $corpid, $sender, 2, $ruleId, 1);
- }
- return MassMsgCreateService::addMsgTemplate($corpid, $externalUserIds, $sender, $content, $attachments, $ruleId, $retry, $chatType, $chatIdList);
- }
- }
- Log::logInfo('创建群发消息返回结果', [
- 'postData' => $postData,
- 'retry' => $retry,
- 'corpid' => $corpid,
- 'response' => $responseData
- ], 'MassMsgSend');
- return $responseData;
- }
- /**
- * 获取企业群发成员执行结果
- * @param $corpid string 授权方企业微信id
- * @param $msgId string 群发消息的id
- * @param $sender string 外部联系人的userid
- * @param $limit integer 返回的最大记录数,整型,最大值1000,默认值500,超过最大值时取默认值
- * @param $cursor string 上次请求返回的next_cursor,用于分页请求(见官方文档)
- * @param $retry integer 重试次数,默认为0
- * */
- public static function getMassMsgSendResult($corpid, $msgId, $sender, $limit, $cursor, $retry=0)
- {
- # 获取SuiteAccessToken
- $accessToken = AuthorizeCorp::getAccessToken($corpid, '获取企业群发成员执行结果');
- if(empty($accessToken)) { // Todo::令牌获取失败,发送报警
- return false;
- }
- $postData = [
- 'msgid' => $msgId,
- 'userid' => $sender,
- 'limit' => $limit,
- 'cursor' => $cursor
- ];
- # 获取外部联系人详情信息API
- $getExternalContactUri = config('qyWechat.mass_msg_send_result');
- $getExternalContactUri .= $accessToken;
- $response = HttpService::httpPost($getExternalContactUri, json_encode($postData));
- $responseData = json_decode($response, true);
- Log::logInfo('获取企业群发成员执行结果', [
- 'postData' => $postData,
- 'retry' => $retry,
- 'corpid' => $corpid,
- 'response' => $responseData
- ], 'getMassMsgSendResult');
- if(isset($responseData['errcode']) && $responseData['errcode']) {
- if($retry <=5) {
- $retry++;
- MassMsgCreateService::getMassMsgSendResult($corpid, $msgId, $sender, $limit, $cursor, $retry);
- }
- Log::logError('企业群发成员执行结果获取失败', $responseData, 'GetMassMsgSendResult');
- return false;
- }
- return $responseData;
- }
- }
|