MqDiscussion.php 5.2 KB

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