MqPr.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. /**
  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(\App\Tools\Tools::isStop()){
  45. return 0;
  46. }
  47. $exchange = 'router';
  48. $queue = 'suggestion';
  49. $this->info(" [*] Waiting for {$queue}. Ver. 2023-12-24");
  50. Log::debug("mq:pr start.");
  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('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. try{
  79. $sendTo = array();
  80. $sendTo[] = $prData->channel->studio_id;
  81. if($orgText){
  82. //原文作者
  83. if(!in_array($orgText->editor_uid,$sendTo)){
  84. $sendTo[] = $orgText->editor_uid;
  85. }
  86. //原文采纳者
  87. if(!empty($orgText->acceptor_uid) && !in_array($orgText->acceptor_uid,$sendTo)){
  88. $sendTo[] = $orgText->acceptor_uid;
  89. }
  90. }
  91. $sendCount = NotificationController::insert($prData->editor->id,
  92. $sendTo,
  93. 'suggestion',
  94. $prData->uid,
  95. $prData->channel->id);
  96. $this->info("send notification success to [".$sendCount.'] users');
  97. }catch(\Exception $e){
  98. Log::error('send notification failed',['exception'=>$e]);
  99. }
  100. //发送webhook
  101. $webhooks = WebHook::where('res_id',$prData->channel->id)
  102. ->where('status','active')
  103. ->get();
  104. foreach ($webhooks as $key => $hook) {
  105. $event = json_decode($hook->event);
  106. if(!in_array('pr',$event)){
  107. continue;
  108. }
  109. $command = '';
  110. $whSend = new WebHookSend;
  111. switch ($hook->receiver) {
  112. case 'dingtalk':
  113. $ok = $whSend->dingtalk($hook->url,$msgTitle,$msgContent);
  114. break;
  115. case 'wechat':
  116. $ok = $whSend->wechat($hook->url,null,$msgContent);
  117. break;
  118. default:
  119. $ok=2;
  120. break;
  121. }
  122. $this->info("{$command} ok={$ok}");
  123. $result+=$ok;
  124. if($ok === 0){
  125. Log::debug('mq:pr: send success {url}',['url'=>$hook->url]);
  126. WebHook::where('id',$hook->id)->increment('success');
  127. }else{
  128. Log::error('mq:pr: send fail {url}',['url'=>$hook->url]);
  129. WebHook::where('id',$hook->id)->increment('fail');
  130. }
  131. }
  132. return $result;
  133. });
  134. return 0;
  135. }
  136. }