MqPr.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. class MqPr extends Command
  14. {
  15. /**
  16. * The name and signature of the console command.
  17. * php artisan mq:pr
  18. * @var string
  19. */
  20. protected $signature = 'mq:pr';
  21. /**
  22. * The console command description.
  23. *
  24. * @var string
  25. */
  26. protected $description = 'push pr message to mq';
  27. /**
  28. * Create a new command instance.
  29. *
  30. * @return void
  31. */
  32. public function __construct()
  33. {
  34. parent::__construct();
  35. }
  36. /**
  37. * Execute the console command.
  38. *
  39. * @return int
  40. */
  41. public function handle()
  42. {
  43. if(\App\Tools\Tools::isStop()){
  44. return 0;
  45. }
  46. $exchange = 'router';
  47. $queue = 'suggestion';
  48. $this->info(" [*] Waiting for {$queue}. To exit press CTRL+C");
  49. Log::debug("mq:pr start.");
  50. Mq::worker($exchange,$queue,function ($message){
  51. /**生成消息内容 */
  52. $msgTitle = '修改建议';
  53. $prData = $message->data;
  54. $sent_num = "{$prData->book}-{$prData->paragraph}-{$prData->word_start}-{$prData->word_end}";
  55. $this->info('request'.$sent_num);
  56. $username = $prData->editor->nickName;
  57. $palitext = PaliSentence::where('book',$prData->book)
  58. ->where('paragraph',$prData->paragraph)
  59. ->where('word_begin',$prData->word_start)
  60. ->where('word_end',$prData->word_end)
  61. ->value('text');
  62. $orgText = Sentence::where('book_id',$prData->book)
  63. ->where('paragraph',$prData->paragraph)
  64. ->where('word_start',$prData->word_start)
  65. ->where('word_end',$prData->word_end)
  66. ->where('channel_uid',$prData->channel->id)
  67. ->value('content');
  68. $prtext = mb_substr($prData->content,0,140,"UTF-8");
  69. $link = config('app.url')."/pcd/article/para/{$prData->book}-{$prData->paragraph}";
  70. $link .= "?book={$prData->book}&par={$prData->paragraph}&channel={$prData->channel->id}";
  71. $msgContent = "{$username} 就文句`{$palitext}`提出了修改建议:\n";
  72. $msgContent .= ">内容摘要:<font color=\"comment\">{$prtext}</font>,\n";
  73. $msgContent .= ">句子编号:<font color=\"info\">{$sent_num}</font>\n";
  74. $msgContent .= "欢迎大家[点击链接]({$link})查看并讨论。";
  75. //标题
  76. if($prData->book<1000){
  77. $path = json_decode(PaliTextApi::getChapterPath($prData->book,$prData->paragraph)) ;
  78. $title = $path[0]->title;
  79. }else{
  80. $title = '';
  81. }
  82. $notificationContent = "**{$title}**\n\n";
  83. $notificationContent .= ">{$orgText}\n\n";
  84. $notificationContent .= "{$prtext}";
  85. $result=0;
  86. //发送站内信
  87. $url = config('app.url').'/api/v2/notification';
  88. $response = Http::withToken($message->token)
  89. ->post($url,
  90. [
  91. 'to'=> $prData->channel->studio_id,
  92. 'title'=> $msgTitle,
  93. 'content'=> $notificationContent,
  94. 'url'=>$link,
  95. 'res_type'=> 'suggestion',
  96. 'res_id'=> $prData->uid,
  97. ]);
  98. if($response->failed()){
  99. Log::error('send notification failed');
  100. }else{
  101. $this->info("send notification");
  102. }
  103. //发送webhook
  104. $webhooks = WebHook::where('res_id',$prData->channel->id)
  105. ->where('status','active')
  106. ->get();
  107. foreach ($webhooks as $key => $hook) {
  108. $event = json_decode($hook->event);
  109. if(!in_array('pr',$event)){
  110. continue;
  111. }
  112. $command = '';
  113. $whSend = new WebHookSend;
  114. switch ($hook->receiver) {
  115. case 'dingtalk':
  116. $ok = $whSend->dingtalk($hook->url,$msgTitle,$msgContent);
  117. break;
  118. case 'wechat':
  119. $ok = $whSend->wechat($hook->url,null,$msgContent);
  120. break;
  121. default:
  122. $ok=2;
  123. break;
  124. }
  125. $this->info("{$command} ok={$ok}");
  126. $result+=$ok;
  127. if($ok === 0){
  128. Log::debug('mq:pr: send success {url}',['url'=>$hook->url]);
  129. WebHook::where('id',$hook->id)->increment('success');
  130. }else{
  131. Log::error('mq:pr: send fail {url}',['url'=>$hook->url]);
  132. WebHook::where('id',$hook->id)->increment('fail');
  133. }
  134. }
  135. return $result;
  136. });
  137. return 0;
  138. }
  139. }