CustomMsgSendJob.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace App\Jobs;
  3. use App\Modules\OfficialAccount\Models\CustomSendMsgs;
  4. use App\Modules\OfficialAccount\Services\ForceSubscribeService;
  5. use Illuminate\Bus\Queueable;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Bus\Dispatchable;
  8. use Illuminate\Queue\InteractsWithQueue;
  9. use Illuminate\Queue\SerializesModels;
  10. use Redis;
  11. class CustomMsgSendJob implements ShouldQueue
  12. {
  13. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  14. protected $data;
  15. /**
  16. * Create a new job instance.
  17. *
  18. * @return void
  19. */
  20. public function __construct($data)
  21. {
  22. //
  23. \Log::info('========初始化一个实例');
  24. $this->data = $data;
  25. }
  26. /**
  27. * Execute the job.
  28. *
  29. * @return void
  30. */
  31. public function handle()
  32. {
  33. //
  34. try {
  35. $customSendMsgsPrams = $this->data;
  36. $customSendMsgs = CustomSendMsgs::customSendMsgsById($customSendMsgsPrams['id']);
  37. if (!$customSendMsgs || $customSendMsgs['del_flag'] == 1 || $customSendMsgs['status'] == '4'){
  38. \Log::info('========客服消息已被停止或删除,不发送==========task_id = '.$customSendMsgs['task_id']);
  39. return;
  40. }else{
  41. \Log::info('========现将客服消息设置成发送状态 "8" 用户不能操作(删除,编辑,停止) = '.$customSendMsgs['id']);
  42. $customSendMsgs['status'] = '8';
  43. $customSendMsgs->save();
  44. $officialAccountArray = $customSendMsgs->toArray();
  45. Redis::hset('send_wechat_msg:task_id:'.$customSendMsgs['task_id'], 'wechat_msg', json_encode($officialAccountArray));
  46. }
  47. $appids = $customSendMsgsPrams['appid'];
  48. $appidArray = explode(',', $appids);
  49. $usercountnum = 0;
  50. for ($j=0; $j < count($appidArray); $j++) {
  51. $skip = 0;
  52. while ( $skip <= 2500000) {
  53. \Log::info('------------------分页取强关用户,目前页数----------'.$skip);
  54. //5,获取所选条件下筛选出的用户
  55. $sign_time = 'z';
  56. $force_user = ForceSubscribeService::forceUserCountByCustomPrams($appidArray[$j],$customSendMsgsPrams['distribution_channel_id'],$customSendMsgsPrams['subscribe_time'],$customSendMsgsPrams['sex'],$customSendMsgsPrams['balance'],$customSendMsgsPrams['category_id'],$customSendMsgsPrams['order_type'],$sign_time,$skip);
  57. if (count($force_user)>0) {
  58. $usercountnum += count($force_user);
  59. for ($i=0; $i < count($force_user); $i++) {
  60. if ($i == count($force_user)-1) {
  61. // \Log::info('--------------------客服消息接收用户 last_task');
  62. // \Log::info($force_user[$i]['openid']);
  63. // \Log::info($appidArray[$j]);
  64. // 7,循环用户发送客服消息
  65. $data = array();
  66. $data['openid'] = $force_user[$i]['openid'];
  67. $data['appid'] = $appidArray[$j];
  68. $data['news_content'] = $customSendMsgsPrams['content'];
  69. $data['type'] = 'last_task';
  70. $data['task_id'] = $customSendMsgsPrams['task_id'];
  71. $data['send_time'] = $customSendMsgsPrams['send_time'];
  72. $send_data=array(
  73. 'send_time'=>$customSendMsgsPrams['send_time'],
  74. 'data' => $data
  75. );
  76. $now_time = strtotime($customSendMsgsPrams['send_time'])-time();
  77. \Log::info('------------------!!!发送客服消息时间!!!----------');
  78. \Log::info($customSendMsgsPrams['send_time']);
  79. \Log::info($now_time);
  80. $delay = $now_time;
  81. // \Log::info('客服消息找到用户并发送===================================');
  82. // \Log::info($data['openid']);
  83. // \Log::info($data['appid']);
  84. // \Log::info($data['news_content']);
  85. \Log::info('------------------发送最后一条客服消息----------'.$customSendMsgsPrams['task_id']);
  86. $job = (new SendNews($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_news_list');
  87. dispatch($job);
  88. }else{
  89. // \Log::info('--------------------客服消息接收用户 one_task');
  90. // \Log::info($force_user[$i]['openid']);
  91. // \Log::info($appidArray[$j]);
  92. //7,循环用户发送客服消息
  93. $data = array();
  94. $data['openid'] = $force_user[$i]['openid'];
  95. $data['appid'] = $appidArray[$j];
  96. $data['news_content'] = $customSendMsgsPrams['content'];
  97. $data['type'] = 'one_task';
  98. $data['task_id'] = $customSendMsgsPrams['task_id'];
  99. $data['send_time'] = $customSendMsgsPrams['send_time'];
  100. $send_data=array(
  101. 'send_time'=>$customSendMsgsPrams['send_time'],
  102. 'data' => $data
  103. );
  104. $now_time = strtotime($customSendMsgsPrams['send_time'])-time();
  105. \Log::info('------------------!!!发送客服消息时间!!!----------');
  106. \Log::info($customSendMsgsPrams['send_time']);
  107. \Log::info($now_time);
  108. $delay = $now_time;
  109. // \Log::info('客服消息找到用户并发送===================================');
  110. // \Log::info($data['openid']);
  111. // \Log::info($data['appid']);
  112. // \Log::info($data['news_content']);
  113. $job = (new SendNews($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_news_list');
  114. dispatch($job);
  115. }
  116. }
  117. }else{
  118. \Log::info('------------------发送客服消息没有查找到强关用户----------'.$customSendMsgsPrams['task_id']);
  119. break;
  120. }
  121. $skip += count($force_user);
  122. }
  123. }
  124. $customSendMsgs['user_num'] = $usercountnum;
  125. $customSendMsgs->save();
  126. return 1;
  127. // }
  128. } catch (\Exception $e) {
  129. \Log::info('===================================发送客服消息报错');
  130. \Log::info($e->getMessage());
  131. return 0;
  132. }
  133. }
  134. }