MqPr.php 6.2 KB

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