MqDiscussion.php 5.6 KB

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