GZHSendKFMessage.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 Illuminate\Support\Facades\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. /**
  29. * @var TraceContext
  30. */
  31. private $traceContext;
  32. /**
  33. * Create a new job instance.
  34. */
  35. public function __construct($info)
  36. {
  37. $this->info = $info;
  38. }
  39. /**
  40. * Execute the job.
  41. */
  42. public function handle(): void
  43. {
  44. $this->traceContext = TraceContext::newFromParent($this->info['traceInfo']);
  45. myLog('KFMessageSend')->info('公众号开始发送客服消息', [
  46. 'info' => $this->info,
  47. 'traceInfo' => $this->traceContext->getTraceInfo(),
  48. ]);
  49. $gzh = $this->getGZH();
  50. if(!$gzh) return;
  51. $message = $this->getMessage();
  52. if(!$message) return;
  53. $messageContent = collect(\json_decode($message->message_content, true));
  54. $messageStr = $messageContent->pluck('text')->join("\n");
  55. $officialAccount = $this->getOfficialAccount($gzh);
  56. if(false === $officialAccount) return;
  57. if($this->info['isTest'] ?? false) {
  58. $openid = $this->info['openid'] ?? '';
  59. if(!$openid) {
  60. myLog('KFMessageSend')->error('测试回传没有openid', [
  61. 'info' => $this->info
  62. ]);
  63. }
  64. GZHSendKFMessageService::sendText($officialAccount, $openid, $messageStr, $this->traceContext);
  65. } else {
  66. $next_openid = '';
  67. $loop = 1;
  68. while (true) {
  69. if($loop++ > 10000) {
  70. break;
  71. }
  72. if(1 == $message->u_type) {
  73. $info = $this->getUserOpenids($officialAccount, $next_openid);
  74. foreach ($info['data']['openid'] as $opid){
  75. // GZHSendKFMessageService::sendText($officialAccount, $opid, $messageStr, $this->traceContext);
  76. dump($opid);
  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->user->list($next_openid);
  114. if(0 != ($result['errcode'] ?? 0)) {
  115. return false;
  116. }
  117. return $result;
  118. }
  119. /**
  120. * 获取公众号调用对象
  121. * @param $gzh
  122. * @return \EasyWeChat\OfficialAccount\Application
  123. * @throws \EasyWeChat\Kernel\Exceptions\BadResponseException
  124. * @throws \EasyWeChat\Kernel\Exceptions\HttpException
  125. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  126. * @throws \Psr\SimpleCache\InvalidArgumentException
  127. * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
  128. * @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface
  129. * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
  130. * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
  131. * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  132. */
  133. private function getOfficialAccount($gzh) {
  134. try{
  135. return WechatPlatform::buildApplication($gzh)
  136. ->officialAccount($gzh->authorizer_appid, $gzh->authorizer_refresh_token);
  137. } catch (\Throwable $exception) {
  138. myLog('KFMessageSend')->error('获取公众号调用对象失败', [
  139. 'exceptionMessage' => $exception->getMessage(),
  140. 'traceInfo' => $this->traceContext->getTraceInfo()
  141. ]);
  142. return false;
  143. }
  144. }
  145. private function getGZH() {
  146. $gzh = DB::table('wechat_authorization_infos as a')
  147. ->join('wechat_open_platform_infos as b', 'a.component_appid', 'b.app_id')
  148. ->where([
  149. ['a.id', '=', $this->info['gzhId']],
  150. ['a.is_enabled', '=', 1],
  151. ['b.is_enabled', '=', 1]
  152. ])->select('a.authorizer_appid', 'a.authorizer_refresh_token', 'b.app_id', 'b.secret', 'b.token', 'b.aes_key')
  153. ->first();
  154. if(!$gzh) {
  155. myLog('KFMessageSend')->error('公众号不可用', [
  156. 'traceInfo' => $this->traceContext->getTraceInfo(),
  157. ]);
  158. }
  159. return $gzh;
  160. }
  161. }