GZHSendKFMessage.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace App\Jobs\WechatPlatform;
  3. use App\Service\Util\Support\Trace\TraceContext;
  4. use App\Service\WechatPlatform\GZHSendKFMessageService;
  5. use App\Service\WechatPlatform\WechatPlatform;
  6. use EasyWeChat\OfficialAccount\Application;
  7. use Illuminate\Bus\Queueable;
  8. use Illuminate\Contracts\Queue\ShouldBeUnique;
  9. use Illuminate\Contracts\Queue\ShouldQueue;
  10. use Illuminate\Foundation\Bus\Dispatchable;
  11. use Illuminate\Queue\InteractsWithQueue;
  12. use Illuminate\Queue\SerializesModels;
  13. use Predis\Command\Traits\DB;
  14. class GZHSendKFMessage implements ShouldQueue
  15. {
  16. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  17. /**
  18. * @var
  19. * <pre>
  20. * [
  21. * 'gzhId' => $gzhId, // wechat_authorization_infos.id
  22. * 'messageId' => $item->id, // wechat_kf_messages.id
  23. * 'traceInfo' => $traceContext->getTraceInfo() // traceInfo
  24. * ]
  25. * </pre>
  26. */
  27. private $info;
  28. private $isTest;
  29. /**
  30. * @var TraceContext
  31. */
  32. private $traceContext;
  33. /**
  34. * Create a new job instance.
  35. */
  36. public function __construct($info)
  37. {
  38. $this->isTest = $info['isTest'] ?? false;
  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->isTest) {
  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['openid'] as $opid){
  76. GZHSendKFMessageService::sendText($officialAccount, $opid, $messageStr, $this->traceContext);
  77. }
  78. $next_openid = $info['next_openid'];
  79. if(!$next_openid) {
  80. break;
  81. }
  82. } elseif (2 == $message->u_type) {
  83. // todo, 从人群包中获取.
  84. }
  85. }
  86. }
  87. }
  88. private function getMessage() {
  89. $message = DB::table('wechat_kf_messages')
  90. ->where('id', $this->info['messageId'])
  91. ->first();
  92. if(!$message) {
  93. myLog('KFMessageSend')->error('消息不存在', [
  94. 'info' => $this->info,
  95. 'traceInfo' => $this->traceContext->getTraceInfo(),
  96. ]);
  97. return false;
  98. }
  99. if(1 != $message->message_type) {
  100. myLog('KFMessageSend')->error('不支持的消息类型', [
  101. 'info' => $this->info,
  102. 'traceInfo' => $this->traceContext->getTraceInfo(),
  103. ]);
  104. return false;
  105. }
  106. return $message;
  107. }
  108. /**
  109. *
  110. * @param $officialAccount Application
  111. */
  112. private function getUserOpenids($officialAccount, $next_openid) {
  113. $result = $officialAccount->getClient()
  114. ->get('cgi-bin/user/get', [
  115. 'query' => [
  116. 'next_openid' => $next_openid,
  117. ]
  118. ])->toArray();
  119. if(0 != ($result['errcode'] ?? 0)) {
  120. return false;
  121. }
  122. return $result;
  123. }
  124. /**
  125. * 获取公众号调用对象
  126. * @param $gzh
  127. * @return \EasyWeChat\OfficialAccount\Application
  128. * @throws \EasyWeChat\Kernel\Exceptions\BadResponseException
  129. * @throws \EasyWeChat\Kernel\Exceptions\HttpException
  130. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  131. * @throws \Psr\SimpleCache\InvalidArgumentException
  132. * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
  133. * @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface
  134. * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
  135. * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
  136. * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  137. */
  138. private function getOfficialAccount($gzh) {
  139. try{
  140. return WechatPlatform::buildApplication($gzh)
  141. ->getOfficialAccountWithRefreshToken($gzh->authorizer_appid, $gzh->authorizer_refresh_token);
  142. } catch (\Throwable $exception) {
  143. myLog('KFMessageSend')->error('获取公众号调用对象失败', [
  144. 'exceptionMessage' => $exception->getMessage(),
  145. 'traceInfo' => $this->traceContext->getTraceInfo()
  146. ]);
  147. return false;
  148. }
  149. }
  150. private function getGZH() {
  151. $gzh = DB::table('wechat_authorization_infos as a')
  152. ->join('wechat_open_platform_infos as o', 'a.component_appid', 'o.app_id')
  153. ->where([
  154. ['a.id', '=', $this->info['gzhId']],
  155. ['a.is_enabled', '=', 1],
  156. ['b.is_enabled', '=', 1]
  157. ])->select('a.authorizer_appid', 'a.authorizer_refresh_token', 'b.app_id', 'b.secret', 'b.token', 'b.aes_key')
  158. ->first();
  159. if(!$gzh) {
  160. myLog('KFMessageSend')->error('公众号不可用', [
  161. 'traceInfo' => $this->traceContext->getTraceInfo(),
  162. ]);
  163. }
  164. return $gzh;
  165. }
  166. }