NoticeCommand.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace App\Console\Commands\Daily;
  3. use App\Models\Daily\DailyPushModel;
  4. use App\Models\Daily\SubscribeModel;
  5. use App\Services\NoticeService\Channels\WeChatTemplateChannel;
  6. use Illuminate\Console\Command;
  7. class NoticeCommand extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'daily:syj';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = '时遇记每日推送';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. private $public_id;
  31. private $title = "每日订阅通知-时遇记公众号";
  32. private $article;
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return mixed
  37. */
  38. public function handle()
  39. {
  40. $this->public_id = "gh_c94c95866ca5";
  41. $this->article = DailyPushModel::where('date', date("Y-m-d"))->first();
  42. if (!$this->article) {
  43. return $this->error("今日没有推送内容");
  44. }
  45. $this->kfAccount();
  46. $this->gzh();
  47. }
  48. public function kfAccount()
  49. {
  50. $kfService = \WeChat::KfAccount($this->public_id);
  51. // 客服消息推送
  52. $fillet_openids = \DB::connection('mysql_datalog')
  53. ->table("notice_logs")
  54. ->where('title', $this->title)
  55. ->where('date', date("Y-m-d"))
  56. ->pluck('openid');
  57. $unscrube_openids = SubscribeModel::where('subscribe', 0)->pluck('openid');
  58. $openids = \DB::table('pocket.kdgx_fans_5')
  59. ->where('public_id', $this->public_id)
  60. ->where('interaction_time', '>', strtotime("-2 day"))
  61. ->where('subscribe', 1)
  62. // ->where('openid','oBGbrw_176vAKgClwGdS-9WSc-xM')
  63. ->whereNotIn('openid', $fillet_openids)
  64. ->whereNotIn('openid', $unscrube_openids)
  65. ->pluck('openid');
  66. foreach ($openids as $openid) {
  67. $content = $this->article->push_content . "\n\n点击<a href='weixin://bizmsgmenu?msgmenucontent=今日文章&msgmenuid=234234'>【这里】</a>获取今日推送";
  68. try {
  69. $result = $kfService->send($openid, 'text', [
  70. 'content' => $content
  71. ]);
  72. \DB::connection('mysql_datalog')
  73. ->table("notice_logs")
  74. ->insert([
  75. 'openid' => $openid,
  76. 'title' => $this->title,
  77. 'uuid' => uuid(),
  78. 'notice_type' => '公众号客服消息',
  79. 'content' => $content,
  80. 'result' => true,
  81. 'date' => date("Y-m-d"),
  82. 'created_at' => time(),
  83. ]);
  84. dump($result);
  85. } catch (\Exception $e) {
  86. dump($e->getMessage());
  87. }
  88. }
  89. }
  90. // 发送模版消息
  91. public function gzh()
  92. {
  93. $fillet_openids = \DB::connection('mysql_datalog')
  94. ->table("notice_logs")
  95. ->where('title', $this->title)
  96. ->where('date', date("Y-m-d"))
  97. ->where('result', true)
  98. ->pluck('openid');
  99. $subscribe_openids = SubscribeModel::where('subscribe', 1)
  100. // ->where('openid','oBGbrw_176vAKgClwGdS-9WSc-xM')
  101. ->whereNotIn('openid', $fillet_openids)
  102. ->pluck('openid');
  103. foreach ($subscribe_openids as $openid) {
  104. $to_uid = $openid;
  105. $templateId = '-OlcUriUhBniEGGQ5j0EY1E6_QI9H-1Hs2dwIbhRTLo';
  106. $parameters = [
  107. 'first' => [
  108. 'value' => $this->article->first . "\n",
  109. ],
  110. 'keyword1' => [
  111. 'value' => $this->article->keyword1,
  112. ],
  113. 'keyword2' => [
  114. 'value' => $this->article->keyword2,
  115. ],
  116. 'remark' => [
  117. 'value' => "\n点此查看今日文章",
  118. 'color' => '#FF7E98',
  119. ],
  120. ];
  121. $page = "pages/article-redirect/article-redirect?url=" . urlencode($this->article->article_url);
  122. $weChatTemplate = new WeChatTemplateChannel();
  123. $weChatTemplate->setAppId('gh_c94c95866ca5')
  124. ->setTitle($this->title)
  125. ->toUser($to_uid)
  126. ->setPage($page)
  127. ->setTemplateId($templateId)
  128. ->setParameters($parameters)
  129. ->send();
  130. dump($weChatTemplate->getMessage());
  131. }
  132. }
  133. }