企微短剧业务系统

MassMsgCreateService.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace App\Service;
  3. use App\Log;
  4. use App\Models\AuthorizeCorp;
  5. class MassMsgCreateService
  6. {
  7. /**
  8. * 创建企业群发
  9. * @param $corpid string 授权方企业微信id
  10. * @param $externalUserIds array 客户的外部联系人id列表,仅在chat_type为single时有效,不可与sender同时为空,最多可传入1万个客户
  11. * @param $sender string 发送企业群发消息的成员userid,当类型为发送给客户群时必填
  12. * @param $content string 消息文本内容
  13. * @param $attachments array 附件,最多支持添加9个附件
  14. * @param $chatType string 群发任务的类型,默认为single,表示发送给客户,group表示发送给客户群
  15. * @param array $chatIdList 客户群id列表,仅在chat_type为group时有效,最多可一次指定2000个客户群。指定群id之后,收到任务的群主无须再选择客户群,仅对4.1.10及以上版本的企业微信终端生效
  16. * */
  17. public static function addMsgTemplate($corpid, $externalUserIds, $sender, $content, $attachments, $ruleId, $retry=0, $chatType='single', $chatIdList = [])
  18. {
  19. $accessToken = AuthorizeCorp::getAccessToken($corpid, '创建企业群发');
  20. if(empty($accessToken)) { // Todo::令牌获取失败,发送报警
  21. return false;
  22. }
  23. $postData = [
  24. 'chat_type' => $chatType,
  25. 'external_userid' => $externalUserIds,
  26. 'chat_id_list' => $chatIdList,
  27. 'sender' => $sender,
  28. 'text' => [
  29. 'content' => $content
  30. ],
  31. 'attachments' => $attachments
  32. ];
  33. $requestUri = config('qyWechat.send_message');
  34. $requestUri .= $accessToken;
  35. $response = HttpService::httpPost($requestUri, json_encode($postData));
  36. $responseData = json_decode($response, true);
  37. # 发起重试
  38. if($response===false || !isset($responseData['errcode']) || in_array($responseData['errcode'], [-1, 45009, 45033, 40007])) { // 企微系统繁忙,发起重试
  39. if($retry < 10) {
  40. sleep(1);
  41. Log::logInfo('企微系统繁忙,发起重试', [
  42. 'corpid' => $corpid,
  43. 'postData' => $postData,
  44. 'retry' => $retry,
  45. 'response' => $response
  46. ], 'AddMassMsgTemplate');
  47. $retry++;
  48. if(isset($responseData['errcode']) && $responseData['errcode'] == 40007) { // 企微返回素材media_id异常,重新处理附件信息
  49. $attachments = MaterialService::radarAttachment($attachments, $corpid, $sender, 2, $ruleId, 1);
  50. }
  51. return MassMsgCreateService::addMsgTemplate($corpid, $externalUserIds, $sender, $content, $attachments, $ruleId, $retry, $chatType, $chatIdList);
  52. }
  53. }
  54. Log::logInfo('创建群发消息返回结果', [
  55. 'postData' => $postData,
  56. 'retry' => $retry,
  57. 'corpid' => $corpid,
  58. 'response' => $responseData
  59. ], 'MassMsgSend');
  60. return $responseData;
  61. }
  62. /**
  63. * 获取企业群发成员执行结果
  64. * @param $corpid string 授权方企业微信id
  65. * @param $msgId string 群发消息的id
  66. * @param $sender string 外部联系人的userid
  67. * @param $limit integer 返回的最大记录数,整型,最大值1000,默认值500,超过最大值时取默认值
  68. * @param $cursor string 上次请求返回的next_cursor,用于分页请求(见官方文档)
  69. * @param $retry integer 重试次数,默认为0
  70. * */
  71. public static function getMassMsgSendResult($corpid, $msgId, $sender, $limit, $cursor, $retry=0)
  72. {
  73. # 获取SuiteAccessToken
  74. $accessToken = AuthorizeCorp::getAccessToken($corpid, '获取企业群发成员执行结果');
  75. if(empty($accessToken)) { // Todo::令牌获取失败,发送报警
  76. return false;
  77. }
  78. $postData = [
  79. 'msgid' => $msgId,
  80. 'userid' => $sender,
  81. 'limit' => $limit,
  82. 'cursor' => $cursor
  83. ];
  84. # 获取外部联系人详情信息API
  85. $getExternalContactUri = config('qyWechat.mass_msg_send_result');
  86. $getExternalContactUri .= $accessToken;
  87. $response = HttpService::httpPost($getExternalContactUri, json_encode($postData));
  88. $responseData = json_decode($response, true);
  89. Log::logInfo('获取企业群发成员执行结果', [
  90. 'postData' => $postData,
  91. 'retry' => $retry,
  92. 'corpid' => $corpid,
  93. 'response' => $responseData
  94. ], 'getMassMsgSendResult');
  95. if(isset($responseData['errcode']) && $responseData['errcode']) {
  96. if($retry <=5) {
  97. $retry++;
  98. MassMsgCreateService::getMassMsgSendResult($corpid, $msgId, $sender, $limit, $cursor, $retry);
  99. }
  100. Log::logError('企业群发成员执行结果获取失败', $responseData, 'GetMassMsgSendResult');
  101. return false;
  102. }
  103. return $responseData;
  104. }
  105. }