MqDiscussion.php 4.4 KB

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