GZHSendKFMessage.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace App\Jobs\WechatPlatform;
  3. use App\Service\Util\Support\Http\HttpRequestService;
  4. use App\Service\Util\Support\Trace\TraceContext;
  5. use App\Service\WechatPlatform\GZHSendKFMessageService;
  6. use App\Service\WechatPlatform\WechatPlatform;
  7. use EasyWeChat\OfficialAccount\Application;
  8. use Illuminate\Bus\Queueable;
  9. use Illuminate\Contracts\Queue\ShouldBeUnique;
  10. use Illuminate\Contracts\Queue\ShouldQueue;
  11. use Illuminate\Foundation\Bus\Dispatchable;
  12. use Illuminate\Queue\InteractsWithQueue;
  13. use Illuminate\Queue\SerializesModels;
  14. use Illuminate\Support\Facades\DB;
  15. class GZHSendKFMessage implements ShouldQueue
  16. {
  17. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  18. /**
  19. * @var
  20. * <pre>
  21. * [
  22. * 'gzhId' => $gzhId, // wechat_authorization_infos.id
  23. * 'messageId' => $item->id, // wechat_kf_messages.id
  24. * 'traceInfo' => $traceContext->getTraceInfo() // traceInfo
  25. * ]
  26. * </pre>
  27. */
  28. private $info;
  29. /**
  30. * @var TraceContext
  31. */
  32. private $traceContext;
  33. /**
  34. * Create a new job instance.
  35. */
  36. public function __construct($info)
  37. {
  38. $this->info = $info;
  39. }
  40. /**
  41. * Execute the job.
  42. */
  43. public function handle(): void
  44. {
  45. $this->traceContext = TraceContext::newFromParent($this->info['traceInfo']);
  46. myLog('KFMessageSend')->info('公众号开始发送客服消息', [
  47. 'info' => $this->info,
  48. 'traceInfo' => $this->traceContext->getTraceInfo(),
  49. ]);
  50. $gzh = $this->getGZH();
  51. if(!$gzh) return;
  52. $message = $this->getMessage();
  53. if(!$message) return;
  54. $messageContent = collect(\json_decode($message->message_content, true));
  55. $messageStr = $messageContent->pluck('text')->join("\n");
  56. $officialAccount = $this->getOfficialAccount($gzh);
  57. if(false === $officialAccount) return;
  58. if($this->info['isTest'] ?? false) {
  59. $openid = $this->info['openid'] ?? '';
  60. if(!$openid) {
  61. myLog('KFMessageSend')->error('测试回传没有openid', [
  62. 'info' => $this->info
  63. ]);
  64. }
  65. GZHSendKFMessageService::sendText($officialAccount, $openid, $messageStr, $this->traceContext);
  66. } else {
  67. $next_openid = '';
  68. $loop = 1;
  69. while (true) {
  70. if($loop++ > 10000) {
  71. break;
  72. }
  73. if(1 == $message->u_type) {
  74. $info = $this->getUserOpenids($officialAccount, $next_openid);
  75. foreach (($info['data']['openid'] ?? []) as $opid){
  76. // GZHSendKFMessageService::sendText($officialAccount, $opid, $messageStr, $this->traceContext);
  77. dump($opid);
  78. }
  79. $next_openid = $info['next_openid'] ?? '';
  80. if(!$next_openid) {
  81. break;
  82. }
  83. } elseif (2 == $message->u_type) {
  84. $info = $this->getUsersFromUG($gzh->id, $message->ug_id, $next_openid);
  85. foreach (($info['data']['openid'] ?? []) as $opid) {
  86. // GZHSendKFMessageService::sendText($officialAccount, $opid, $messageStr, $this->traceContext);
  87. dump($opid);
  88. }
  89. $next_openid = $info['data']['next_uid'] ?? '';
  90. if(!$next_openid) {
  91. break;
  92. }
  93. }
  94. }
  95. }
  96. }
  97. private function getMessage() {
  98. $message = DB::table('wechat_kf_messages')
  99. ->where('id', $this->info['messageId'])
  100. ->first();
  101. if(!$message) {
  102. myLog('KFMessageSend')->error('消息不存在', [
  103. 'info' => $this->info,
  104. 'traceInfo' => $this->traceContext->getTraceInfo(),
  105. ]);
  106. return false;
  107. }
  108. if(1 != $message->message_type) {
  109. myLog('KFMessageSend')->error('不支持的消息类型', [
  110. 'info' => $this->info,
  111. 'traceInfo' => $this->traceContext->getTraceInfo(),
  112. ]);
  113. return false;
  114. }
  115. return $message;
  116. }
  117. /**
  118. * 拉取公众号粉丝
  119. * @param $officialAccount Application
  120. */
  121. private function getUserOpenids($officialAccount, $next_openid) {
  122. $result = $officialAccount->user->list($next_openid);
  123. if(0 != ($result['errcode'] ?? 0)) {
  124. return false;
  125. }
  126. return $result;
  127. }
  128. /**
  129. * 通过用户分群获取
  130. * @param $gzhId
  131. * @param $ugId
  132. * @param $nextUid
  133. * @return false|mixed
  134. */
  135. private function getUsersFromUG($gzhId, $ugId, $nextUid) {
  136. $url = config('wechat.ug.url.listUser');
  137. $signKey = config('wechat.ug.signKey');
  138. $now = time();
  139. $res = HttpRequestService::simpleGet($url, [
  140. 'timestamp' => $now,
  141. 'sign' => md5($signKey.$now),
  142. 'gzhId' => $gzhId,
  143. 'ugId' => $ugId,
  144. 'nextUid' => $nextUid,
  145. 'limit' => 1000
  146. ]);
  147. if($res && (10000 == ($res['code'] ?? 10000))) {
  148. myLog('KFMessageSend')->debug('通过用户分群获取用户列表', [
  149. 'gzhId' => $gzhId,
  150. 'ugId' => $ugId,
  151. 'res' => $res
  152. ]);
  153. return $res;
  154. }
  155. return false;
  156. }
  157. /**
  158. * 获取公众号调用对象
  159. * @param $gzh
  160. * @return \EasyWeChat\OfficialAccount\Application
  161. * @throws \EasyWeChat\Kernel\Exceptions\BadResponseException
  162. * @throws \EasyWeChat\Kernel\Exceptions\HttpException
  163. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  164. * @throws \Psr\SimpleCache\InvalidArgumentException
  165. * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
  166. * @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface
  167. * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
  168. * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
  169. * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  170. */
  171. private function getOfficialAccount($gzh) {
  172. try{
  173. return WechatPlatform::buildApplication($gzh)
  174. ->officialAccount($gzh->authorizer_appid, $gzh->authorizer_refresh_token);
  175. } catch (\Throwable $exception) {
  176. myLog('KFMessageSend')->error('获取公众号调用对象失败', [
  177. 'exceptionMessage' => $exception->getMessage(),
  178. 'traceInfo' => $this->traceContext->getTraceInfo()
  179. ]);
  180. return false;
  181. }
  182. }
  183. private function getGZH() {
  184. $gzh = DB::table('wechat_authorization_infos as a')
  185. ->join('wechat_open_platform_infos as b', 'a.component_appid', 'b.app_id')
  186. ->where([
  187. ['a.id', '=', $this->info['gzhId']],
  188. ['a.is_enabled', '=', 1],
  189. ['b.is_enabled', '=', 1]
  190. ])->select('a.id', 'a.authorizer_appid', 'a.authorizer_refresh_token',
  191. 'b.app_id', 'b.secret', 'b.token', 'b.aes_key')
  192. ->first();
  193. if(!$gzh) {
  194. myLog('KFMessageSend')->error('公众号不可用', [
  195. 'traceInfo' => $this->traceContext->getTraceInfo(),
  196. ]);
  197. }
  198. return $gzh;
  199. }
  200. }