MqDiscussion.php 5.0 KB

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