MqDiscussion.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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(!in_array('discussion',$event)){
  83. continue;
  84. }
  85. $command = '';
  86. $whSend = new WebHookSend;
  87. $ok = 0;
  88. switch ($hook->receiver) {
  89. case 'dingtalk':
  90. $ok = $whSend->dingtalk($hook->url,$msgTitle,$msgContent);
  91. break;
  92. case 'wechat':
  93. $ok = $whSend->wechat($hook->url,null,$msgContent);
  94. break;
  95. default:
  96. $ok=2;
  97. break;
  98. }
  99. $result += $ok;
  100. $this->info("{$command} ok={$ok}");
  101. if($ok===0){
  102. WebHook::where('id',$hook->id)->increment('success');
  103. }else{
  104. WebHook::where('id',$hook->id)->increment('fail');
  105. }
  106. }
  107. break;
  108. default:
  109. # code...
  110. break;
  111. }
  112. return $result;
  113. });
  114. return 0;
  115. }
  116. }