MqPr.php 5.9 KB

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