MqDiscussion.php 5.7 KB

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