MqPr.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Http\Api\Mq;
  5. use App\Models\Sentence;
  6. use App\Models\WebHook;
  7. use App\Models\PaliSentence;
  8. use App\Tools\WebHook as WebHookSend;
  9. use App\Http\Api\MdRender;
  10. use Illuminate\Support\Facades\Log;
  11. class MqPr extends Command
  12. {
  13. /**
  14. * The name and signature of the console command.
  15. *
  16. * @var string
  17. */
  18. protected $signature = 'mq:pr';
  19. /**
  20. * The console command description.
  21. *
  22. * @var string
  23. */
  24. protected $description = 'push pr message to mq';
  25. /**
  26. * Create a new command instance.
  27. *
  28. * @return void
  29. */
  30. public function __construct()
  31. {
  32. parent::__construct();
  33. }
  34. /**
  35. * Execute the console command.
  36. *
  37. * @return int
  38. */
  39. public function handle()
  40. {
  41. if(\App\Tools\Tools::isStop()){
  42. return 0;
  43. }
  44. $exchange = 'router';
  45. $queue = 'suggestion';
  46. $this->info(" [*] Waiting for {$queue}. To exit press CTRL+C");
  47. Log::debug("mq:pr start.");
  48. Mq::worker($exchange,$queue,function ($message){
  49. /**生成消息内容 */
  50. $msgTitle = '修改建议';
  51. $username = $message->editor->nickName;
  52. $palitext = PaliSentence::where('book',$message->book)
  53. ->where('paragraph',$message->paragraph)
  54. ->where('word_begin',$message->word_start)
  55. ->where('word_end',$message->word_end)
  56. ->value('text');
  57. $prtext = mb_substr($message->content,0,140,"UTF-8");
  58. $sent_num = "{$message->book}-{$message->paragraph}-{$message->word_start}-{$message->word_end}";
  59. $link = config('app.url')."/pcd/article/para/{$message->book}-{$message->paragraph}";
  60. $link .= "?book={$message->book}&par={$message->paragraph}&channel={$message->channel->id}";
  61. $msgContent = "{$username} 就文句`{$palitext}`提出了修改建议:\n";
  62. $msgContent .= ">内容摘要:<font color=\"comment\">{$prtext}</font>,\n";
  63. $msgContent .= ">句子编号:<font color=\"info\">{$sent_num}</font>\n";
  64. $msgContent .= "欢迎大家[点击链接]({$link})查看并讨论。";
  65. $webhooks = WebHook::where('res_id',$message->channel->id)
  66. ->where('status','active')
  67. ->get();
  68. $result=0;
  69. foreach ($webhooks as $key => $hook) {
  70. $event = json_decode($hook->event);
  71. if(!in_array('pr',$event)){
  72. continue;
  73. }
  74. $command = '';
  75. $whSend = new WebHookSend;
  76. switch ($hook->receiver) {
  77. case 'dingtalk':
  78. $ok = $whSend->dingtalk($hook->url,$msgTitle,$msgContent);
  79. break;
  80. case 'wechat':
  81. $ok = $whSend->wechat($hook->url,null,$msgContent);
  82. break;
  83. default:
  84. $ok=2;
  85. break;
  86. }
  87. $this->info("{$command} ok={$ok}");
  88. $result+=$ok;
  89. if($ok === 0){
  90. Log::debug('mq:pr: send success {url}',['url'=>$hook->url]);
  91. WebHook::where('id',$hook->id)->increment('success');
  92. }else{
  93. Log::error('mq:pr: send fail {url}',['url'=>$hook->url]);
  94. WebHook::where('id',$hook->id)->increment('fail');
  95. }
  96. }
  97. return $result;
  98. });
  99. return 0;
  100. }
  101. }