MqDiscussion.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\Sentence;
  5. use App\Models\WebHook;
  6. use App\Models\Discussion;
  7. use App\Http\Api\Mq;
  8. use App\Tools\WebHook as WebHookSend;
  9. use App\Http\Api\MdRender;
  10. class MqDiscussion extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'mq:discussion';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = 'Command description';
  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 = 'discussion';
  42. $this->info(" [*] Waiting for {$queue}. To exit press CTRL+C");
  43. Mq::worker($exchange,$queue,function ($message){
  44. switch ($message->res_type) {
  45. case 'sentence':
  46. $sentence = Sentence::where('uid',$message->res_id)->first();
  47. if(!$sentence){
  48. return 0;
  49. }
  50. $contentHtml = MdRender::render($sentence->content,
  51. [$sentence->channel_uid],
  52. null,
  53. 'read',
  54. 'translation',
  55. $sentence->content_type);
  56. $contentTxt = strip_tags($contentHtml);
  57. /**生成消息内容 */
  58. $msgContent = '';
  59. $msgContent .= "\[{$contentTxt}\]";
  60. $msgContent .= '**'. $message->editor->nickName.'**';
  61. $link = "https://staging.wikipali.org/pcd/discussion/topic/";
  62. if($message->parent){
  63. $parentTitle = Discussion::where('id',$message->parent)->value('title');
  64. $link .= $message->parent;
  65. $id = $message->id;
  66. $msgContent .= "回复了 [{$parentTitle}]({$link}#$id)";
  67. $msgTitle = "回复讨论";
  68. }else{
  69. $link .= $message->id;
  70. $msgContent .= "创建了讨论[$message->title]({$link})";
  71. $msgTitle = "创建讨论";
  72. }
  73. if($message->content){
  74. $msgContent .= "\n>".$message->content;
  75. }
  76. $webhooks = WebHook::where('res_id',$sentence->channel_uid)
  77. ->where('status','active')
  78. ->get();
  79. foreach ($webhooks as $key => $hook) {
  80. $event = json_decode($hook->event);
  81. if(!in_array('discussion',$event)){
  82. continue;
  83. }
  84. $command = '';
  85. $whSend = new WebHookSend;
  86. switch ($hook->receiver) {
  87. case 'dingtalk':
  88. $ok = $whSend->dingtalk($hook->url,$msgTitle,$msgContent);
  89. break;
  90. case 'wechat':
  91. $ok = $whSend->wechat($hook->url,null,$msgContent);
  92. break;
  93. default:
  94. $ok=2;
  95. break;
  96. }
  97. $this->info("{$command} ok={$ok}");
  98. if($ok===0){
  99. WebHook::where('id',$hook->id)->increment('success');
  100. }else{
  101. WebHook::where('id',$hook->id)->increment('fail');
  102. }
  103. }
  104. break;
  105. default:
  106. # code...
  107. break;
  108. }
  109. });
  110. return 0;
  111. }
  112. }