MqPr.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. $exchange = 'router';
  41. $queue = 'suggestion';
  42. $this->info(" [*] Waiting for {$queue}. To exit press CTRL+C");
  43. Mq::worker($exchange,$queue,function ($message){
  44. /**生成消息内容 */
  45. $msgTitle = '修改建议';
  46. $username = $message->editor->nickName;
  47. $palitext = PaliSentence::where('book',$message->book)
  48. ->where('paragraph',$message->paragraph)
  49. ->where('word_begin',$message->word_start)
  50. ->where('word_end',$message->word_end)
  51. ->value('text');
  52. $prtext = mb_substr($message->content,0,140,"UTF-8");
  53. $sent_num = "{$message->book}-{$message->paragraph}-{$message->word_start}-{$message->word_end}";
  54. $link = env('APP_URL')."/pcd/article/para/{$message->book}-{$message->paragraph}";
  55. $link .= "?book={$message->book}&par={$message->paragraph}&channel={$message->channel->id}";
  56. $msgContent = "{$username} 就文句`{$palitext}`提出了修改建议:\n";
  57. $msgContent .= ">内容摘要:<font color=\"comment\">{$prtext}</font>,\n";
  58. $msgContent .= ">句子编号:<font color=\"info\">{$sent_num}</font>\n";
  59. $msgContent .= "欢迎大家[点击链接]({$link})查看并讨论。";
  60. $webhooks = WebHook::where('res_id',$message->channel->id)
  61. ->where('status','active')
  62. ->get();
  63. $result=0;
  64. foreach ($webhooks as $key => $hook) {
  65. $event = json_decode($hook->event);
  66. if(!in_array('pr',$event)){
  67. continue;
  68. }
  69. $command = '';
  70. $whSend = new WebHookSend;
  71. switch ($hook->receiver) {
  72. case 'dingtalk':
  73. $ok = $whSend->dingtalk($hook->url,$msgTitle,$msgContent);
  74. break;
  75. case 'wechat':
  76. $ok = $whSend->wechat($hook->url,null,$msgContent);
  77. break;
  78. default:
  79. $ok=2;
  80. break;
  81. }
  82. $this->info("{$command} ok={$ok}");
  83. $result+=$ok;
  84. if($ok===0){
  85. WebHook::where('id',$hook->id)->increment('success');
  86. }else{
  87. WebHook::where('id',$hook->id)->increment('fail');
  88. }
  89. }
  90. return $result;
  91. });
  92. return 0;
  93. }
  94. }