MqPr.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Http\Api\Mq;
  4. use App\Http\Controllers\NotificationController;
  5. use App\Models\PaliSentence;
  6. use App\Models\Sentence;
  7. use App\Models\WebHook;
  8. use App\Tools\Tools;
  9. use App\Tools\WebHook as WebHookSend;
  10. use Illuminate\Console\Command;
  11. use Illuminate\Support\Facades\Log;
  12. class MqPr extends Command
  13. {
  14. /**
  15. * The name and signature of the console command.
  16. * php artisan mq:pr
  17. *
  18. * @var string
  19. */
  20. protected $signature = 'mq:pr';
  21. protected $ver = '2024-1-2';
  22. /**
  23. * The console command description.
  24. *
  25. * @var string
  26. */
  27. protected $description = 'push pr message to mq';
  28. /**
  29. * Create a new command instance.
  30. *
  31. * @return void
  32. */
  33. public function __construct()
  34. {
  35. parent::__construct();
  36. }
  37. /**
  38. * Execute the console command.
  39. *
  40. * @return int
  41. */
  42. public function handle()
  43. {
  44. if (Tools::isStop()) {
  45. return 0;
  46. }
  47. $exchange = 'router';
  48. $queue = 'suggestion';
  49. $this->info(" [*] Waiting for {$queue}. Ver. ".$this->ver);
  50. Log::debug('mq:pr start. ver='.$this->ver);
  51. Mq::worker($exchange, $queue, function ($message) {
  52. /**生成消息内容 */
  53. $msgTitle = '修改建议';
  54. $prData = $message->data;
  55. $sent_num = "{$prData->book}-{$prData->paragraph}-{$prData->word_start}-{$prData->word_end}";
  56. $this->info('ver='.$this->ver.' request'.$sent_num);
  57. $username = $prData->editor->nickName;
  58. $palitext = PaliSentence::where('book', $prData->book)
  59. ->where('paragraph', $prData->paragraph)
  60. ->where('word_begin', $prData->word_start)
  61. ->where('word_end', $prData->word_end)
  62. ->value('text');
  63. $orgText = Sentence::where('book_id', $prData->book)
  64. ->where('paragraph', $prData->paragraph)
  65. ->where('word_start', $prData->word_start)
  66. ->where('word_end', $prData->word_end)
  67. ->where('channel_uid', $prData->channel->id)
  68. ->first();
  69. $prtext = mb_substr($prData->content, 0, 140, 'UTF-8');
  70. $link = config('app.url')."/pcd/article/para/{$prData->book}-{$prData->paragraph}";
  71. $link .= "?book={$prData->book}&par={$prData->paragraph}&channel={$prData->channel->id}";
  72. $msgContent = "{$username} 就文句`{$palitext}`提出了修改建议:\n";
  73. $msgContent .= ">内容摘要:<font color=\"comment\">{$prtext}</font>,\n";
  74. $msgContent .= ">句子编号:<font color=\"info\">{$sent_num}</font>\n";
  75. $msgContent .= "欢迎大家[点击链接]({$link})查看并讨论。";
  76. $result = 0;
  77. // 发送站内信
  78. if ($message->webhook) {
  79. try {
  80. $sendTo = [];
  81. if ($prData->editor->id !== $prData->channel->studio_id) {
  82. $sendTo[] = $prData->channel->studio_id;
  83. }
  84. if ($orgText) {
  85. // 原文作者
  86. if (
  87. ! in_array($orgText->editor_uid, $sendTo) &&
  88. $orgText->editor_uid !== $prData->editor->id
  89. ) {
  90. $sendTo[] = $orgText->editor_uid;
  91. }
  92. // 原文采纳者
  93. if (
  94. ! empty($orgText->acceptor_uid) &&
  95. ! in_array($orgText->acceptor_uid, $sendTo) &&
  96. $orgText->acceptor_uid !== $prData->editor->id
  97. ) {
  98. $sendTo[] = $orgText->acceptor_uid;
  99. }
  100. }
  101. if (count($sendTo) > 0) {
  102. $sendCount = NotificationController::insert(
  103. from: $prData->editor->id,
  104. to: $sendTo,
  105. res_type: 'suggestion',
  106. res_id: $prData->uid,
  107. channel: $prData->channel->id
  108. );
  109. }
  110. $this->info('send notification success to ['.count($sendTo).'] users');
  111. } catch (\Exception $e) {
  112. $this->error('send notification failed');
  113. Log::error('send notification failed', ['exception' => $e]);
  114. }
  115. }
  116. // 发送webhook
  117. if ($message->webhook) {
  118. $webhooks = WebHook::where('res_id', $prData->channel->id)
  119. ->where('status', 'active')
  120. ->get();
  121. foreach ($webhooks as $key => $hook) {
  122. $event = json_decode($hook->event);
  123. if (! in_array('pr', $event)) {
  124. continue;
  125. }
  126. $command = '';
  127. $whSend = new WebHookSend;
  128. switch ($hook->receiver) {
  129. case 'dingtalk':
  130. $ok = $whSend->dingtalk($hook->url, $msgTitle, $msgContent);
  131. break;
  132. case 'wechat':
  133. $ok = $whSend->wechat($hook->url, null, $msgContent);
  134. break;
  135. default:
  136. $ok = 2;
  137. break;
  138. }
  139. $this->info("{$command} ok={$ok}");
  140. $result += $ok;
  141. if ($ok === 0) {
  142. Log::debug('mq:pr: send success {url}', ['url' => $hook->url]);
  143. WebHook::where('id', $hook->id)->increment('success');
  144. } else {
  145. Log::error('mq:pr: send fail {url}', ['url' => $hook->url]);
  146. WebHook::where('id', $hook->id)->increment('fail');
  147. }
  148. }
  149. }
  150. return $result;
  151. });
  152. return 0;
  153. }
  154. }