MqDiscussion.php 3.4 KB

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