MqPr.php 3.5 KB

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