MqDiscussion.php 5.7 KB

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