MqDiscussion.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. class MqDiscussion extends Command
  12. {
  13. /**
  14. * The name and signature of the console command.
  15. *
  16. * @var string
  17. */
  18. protected $signature = 'mq:discussion';
  19. /**
  20. * The console command description.
  21. *
  22. * @var string
  23. */
  24. protected $description = 'Command description';
  25. /**
  26. * Create a new command instance.
  27. *
  28. * @return void
  29. */
  30. public function __construct()
  31. {
  32. parent::__construct();
  33. }
  34. /**
  35. * Execute the console command.
  36. *
  37. * @return int
  38. */
  39. public function handle()
  40. {
  41. $exchange = 'router';
  42. $queue = 'discussion';
  43. $this->info(" [*] Waiting for {$queue}. To exit press CTRL+C");
  44. Mq::worker($exchange,$queue,function ($message){
  45. $result = 0;
  46. switch ($message->res_type) {
  47. case 'sentence':
  48. $sentence = Sentence::where('uid',$message->res_id)->first();
  49. if(!$sentence){
  50. return 0;
  51. }
  52. $contentHtml = MdRender::render($sentence->content,
  53. [$sentence->channel_uid],
  54. null,
  55. 'read',
  56. 'translation',
  57. $sentence->content_type);
  58. $contentTxt = strip_tags($contentHtml);
  59. /**生成消息内容 */
  60. $msgParam = array();
  61. $msgParam['anchor-content'] = $contentTxt;
  62. $msgParam['nickname'] = $message->editor->nickName;
  63. $link = env('APP_URL')."/pcd/discussion/topic/";
  64. if($message->parent){
  65. $msgParam['topic-title'] = Discussion::where('id',$message->parent)->value('title');
  66. $id = $message->id;
  67. $msgParam['link'] = $link . $message->parent.'#'.$id;
  68. $msgTitle = "回复讨论";
  69. $type = 'reply';
  70. }else{
  71. $msgParam['title'] = $message->title;
  72. $msgParam['link'] = $link . $message->id;
  73. $msgTitle = "创建讨论";
  74. $type = 'create';
  75. }
  76. if($message->content){
  77. $msgParam['content'] = $message->content;
  78. }
  79. $rootId = UserApi::getById(0)['uid'];
  80. $articleTitle = "webhook://discussion/{$type}/zh-hans";
  81. $tpl = Article::where('owner',$rootId)
  82. ->where('title',$articleTitle)
  83. ->value('content');
  84. $m = new \Mustache_Engine(array('entity_flags'=>ENT_QUOTES));
  85. $msgContent = $m->render($tpl,$msgParam);
  86. $webhooks = WebHook::where('res_id',$sentence->channel_uid)
  87. ->where('status','active')
  88. ->get();
  89. foreach ($webhooks as $key => $hook) {
  90. $event = json_decode($hook->event);
  91. if(is_array($event)){
  92. if(!in_array('discussion',$event)){
  93. continue;
  94. }
  95. }else{
  96. continue;
  97. }
  98. $command = '';
  99. $whSend = new WebHookSend;
  100. $ok = 0;
  101. switch ($hook->receiver) {
  102. case 'dingtalk':
  103. $ok = $whSend->dingtalk($hook->url,$msgTitle,$msgContent);
  104. break;
  105. case 'wechat':
  106. $ok = $whSend->wechat($hook->url,null,$msgContent);
  107. break;
  108. default:
  109. $ok=2;
  110. break;
  111. }
  112. $result += $ok;
  113. $this->info("{$command} ok={$ok}");
  114. if($ok===0){
  115. WebHook::where('id',$hook->id)->increment('success');
  116. }else{
  117. WebHook::where('id',$hook->id)->increment('fail');
  118. }
  119. }
  120. break;
  121. default:
  122. # code...
  123. break;
  124. }
  125. return $result;
  126. });
  127. return 0;
  128. }
  129. }