MqDiscussion.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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\Models\Article;
  8. use App\Http\Api\Mq;
  9. use App\Tools\WebHook as WebHookSend;
  10. use App\Http\Api\MdRender;
  11. use App\Http\Api\UserApi;
  12. use Illuminate\Support\Facades\Log;
  13. class MqDiscussion extends Command
  14. {
  15. /**
  16. * The name and signature of the console command.
  17. * php artisan mq:discussion
  18. * @var string
  19. */
  20. protected $signature = 'mq:discussion';
  21. /**
  22. * The console command description.
  23. *
  24. * @var string
  25. */
  26. protected $description = 'Command description';
  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 = 'discussion';
  48. $this->info(" [*] Waiting for {$queue}. To exit press CTRL+C");
  49. Log::info("discussion worker start .");
  50. Mq::worker($exchange,$queue,function ($message){
  51. Log::info('mq discussion receive {message}',['message'=>json_encode($message,JSON_UNESCAPED_UNICODE)]);
  52. $result = 0;
  53. switch ($message->res_type) {
  54. case 'sentence':
  55. $sentence = Sentence::where('uid',$message->res_id)->first();
  56. if(!$sentence){
  57. return 0;
  58. }
  59. $contentHtml = MdRender::render($sentence->content,
  60. [$sentence->channel_uid],
  61. null,
  62. 'read',
  63. 'translation',
  64. $sentence->content_type);
  65. $contentTxt = strip_tags($contentHtml);
  66. /**生成消息内容 */
  67. $msgParam = array();
  68. $msgParam['anchor-content'] = $contentTxt;
  69. $msgParam['nickname'] = $message->editor->nickName;
  70. $link = config('app.url')."/pcd/discussion/topic/";
  71. if($message->parent){
  72. $msgParam['topic-title'] = Discussion::where('id',$message->parent)->value('title');
  73. $id = $message->id;
  74. $msgParam['link'] = $link . $message->parent.'#'.$id;
  75. $msgTitle = "回复讨论";
  76. $type = 'reply';
  77. }else{
  78. $msgParam['title'] = $message->title;
  79. $msgParam['link'] = $link . $message->id;
  80. $msgTitle = "创建讨论";
  81. $type = 'create';
  82. }
  83. if($message->content){
  84. $msgParam['content'] = $message->content;
  85. }
  86. $rootId = UserApi::getById(0)['uid'];
  87. $articleTitle = "webhook://discussion/{$type}/zh-hans";
  88. $tpl = Article::where('owner',$rootId)
  89. ->where('title',$articleTitle)
  90. ->value('content');
  91. if(empty($tpl)){
  92. Log::error('mq:discussion 模版不能为空',['tpl_title'=>$articleTitle]);
  93. return 1;
  94. }
  95. $m = new \Mustache_Engine(array('entity_flags'=>ENT_QUOTES,
  96. 'delimiters' => '{% %}',));
  97. $msgContent = $m->render($tpl,$msgParam);
  98. $webhooks = WebHook::where('res_id',$sentence->channel_uid)
  99. ->where('status','active')
  100. ->get();
  101. foreach ($webhooks as $key => $hook) {
  102. $event = json_decode($hook->event);
  103. if(is_array($event)){
  104. if(!in_array('discussion',$event)){
  105. continue;
  106. }
  107. }else{
  108. continue;
  109. }
  110. $command = '';
  111. $whSend = new WebHookSend;
  112. $ok = 0;
  113. switch ($hook->receiver) {
  114. case 'dingtalk':
  115. $ok = $whSend->dingtalk($hook->url,$msgTitle,$msgContent);
  116. break;
  117. case 'wechat':
  118. $ok = $whSend->wechat($hook->url,null,$msgContent);
  119. break;
  120. default:
  121. $ok=2;
  122. break;
  123. }
  124. $result += $ok;
  125. $logMsg = "{$command} ok={$ok}";
  126. if($ok === 0){
  127. $this->info($logMsg);
  128. }else{
  129. $this->error($logMsg);
  130. }
  131. if($ok === 0){
  132. Log::debug('mq:discussion: send success {url}',['url'=>$hook->url]);
  133. WebHook::where('id',$hook->id)->increment('success');
  134. }else{
  135. Log::error('mq:discussion: send fail {url}',['url'=>$hook->url]);
  136. WebHook::where('id',$hook->id)->increment('fail');
  137. }
  138. }
  139. break;
  140. default:
  141. # code...
  142. break;
  143. }
  144. return $result;
  145. });
  146. return 0;
  147. }
  148. }