VirusAdd.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/24
  6. * Time: 9:52
  7. */
  8. namespace App\Console\Commands;
  9. use GuzzleHttp\Exception\RequestException;
  10. use Illuminate\Console\Command;
  11. use Illuminate\Support\Facades\Log;
  12. use PhpAmqpLib\Connection\AMQPStreamConnection;
  13. use GuzzleHttp\Client;
  14. class VirusAdd extends Command
  15. {
  16. /**
  17. * The name and signature of the console command.
  18. *
  19. * @var string
  20. */
  21. protected $signature = 'virus:add';
  22. /**
  23. * The console command description.
  24. *
  25. * @var string
  26. */
  27. protected $description = 'virus添加数据';
  28. protected $channel;
  29. protected $connection;
  30. protected $queue = 'virus_add';
  31. /**
  32. * Create a new command instance.
  33. *
  34. * @return void
  35. */
  36. public function __construct()
  37. {
  38. parent::__construct();
  39. $this->connection = $this->getConnection();
  40. $this->channel = $this->connection->channel();
  41. $this->channel->exchange_declare($this->queue, 'fanout', false, true, false);
  42. // 初始化virus域名
  43. $this->client = new Client([
  44. 'base_uri' => config('customer.VIRUS_URL'),
  45. 'timeout' => 3
  46. ]);
  47. }
  48. public function getConnection()
  49. {
  50. $conn = false;
  51. if ($this->connection) {
  52. return $this->connection;
  53. }
  54. for ($i = 0; $i < 3; $i++) {
  55. $connection = $this->createConn();
  56. if ($connection) {
  57. $conn = $connection;
  58. break;
  59. }
  60. Log::info("create amqp conn retry=" . $i);
  61. }
  62. return $conn;
  63. }
  64. public function createConn()
  65. {
  66. try {
  67. $connection = new AMQPStreamConnection(env('MQ_HOST'), env('MQ_PORT'), env('MQ_USERNAME'), env('MQ_PWD'), env('MQ_VHOST'));
  68. } catch (\Exception $exception) {
  69. Log::info("AMQP connection Error" . $exception->getMessage());
  70. $connection = false;
  71. }
  72. return $connection;
  73. }
  74. /**
  75. * Execute the console command.
  76. *
  77. * @return mixed
  78. */
  79. public function handle()
  80. {
  81. Log::info('接收一条消息');
  82. $queue_name = $this->queue;
  83. $this->channel->queue_declare($queue_name, false, true, false, false);
  84. $this->channel->queue_bind($queue_name, $this->queue);
  85. $callback = function ($msg) {
  86. $this->line(date('Y-m-d H:i:S').$msg->body);
  87. $data = \GuzzleHttp\json_decode($msg->body, true);
  88. $this->line('接收一条消息收到数据' . $msg->body);
  89. $res = $this->send($data);
  90. if ($res) {
  91. $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
  92. }
  93. };
  94. $this->channel->basic_consume($queue_name, '', false, false, false, false, $callback);
  95. while (count($this->channel->callbacks)) {
  96. $this->channel->wait();
  97. }
  98. $this->channel->close();
  99. $this->connection->close();
  100. }
  101. private function send($data)
  102. {
  103. Log::debug('client:'.json_encode($this->client));
  104. try {
  105. // 签名设置
  106. $signKey = [
  107. 'app_id' => config('customer.VIRUS_APP_ID'),
  108. 'behavior_id' => $data['behavior_id'],
  109. 'target_id' => (string) $data['target_id'],
  110. 'action_id' => (string) $data['action_id'],
  111. 'behavior_flag' => $data['behavior_flag']
  112. ];
  113. ksort($signKey);
  114. $signKey = urldecode(http_build_query($signKey));
  115. $sign = md5(config('customer.VIRUS_APP_SECRET') . $signKey);
  116. Log::debug('签名:'.$sign);
  117. $json = [];
  118. if($data['behavior_flag'] == 'publish'){
  119. $json = [
  120. 'sign' => $sign,
  121. 'app_id' => config('customer.VIRUS_APP_ID'),
  122. 'behavior_id' => $data['behavior_id'],
  123. 'behavior_flag' => $data['behavior_flag'],
  124. 'post_id' => $data['post_id'],
  125. 'post_desc' => $data['post_desc'],
  126. 'post_cover' => $data['post_cover'],
  127. 'target_id' => (string) $data['target_id'],
  128. 'action_id' => (string) $data['action_id'],
  129. 'extra' => [],
  130. ];
  131. }elseif($data['behavior_flag'] == 'comment'){
  132. $json = [
  133. 'sign' => $sign,
  134. 'app_id' => config('customer.VIRUS_APP_ID'),
  135. 'behavior_id' => $data['behavior_id'],
  136. 'behavior_flag' => $data['behavior_flag'],
  137. 'post_id' => $data['post_id'],
  138. 'post_author_uid' => $data['post_author_uid'],
  139. 'post_desc' => $data['post_desc'],
  140. 'post_cover' => $data['post_cover'],
  141. 'comment_id' => $data['comment_id'],
  142. 'comment_content' => $data['comment_content'],
  143. 'parent_comment_id' => $data['parent_comment_id'],
  144. 'parent_comment_content' => $data['parent_comment_content'],
  145. 'parent_comment_uid' => $data['parent_comment_uid'],
  146. 'parent_comment_time' => $data['parent_comment_time'],
  147. 'reply_uid' => $data['reply_uid'],
  148. 'reply_username' => $data['reply_username'],
  149. 'target_id' => (string) $data['target_id'],
  150. 'action_id' => (string) $data['action_id'],
  151. 'extra' => [],
  152. ];
  153. }elseif(in_array($data['behavior_flag'], ['read', 'like', 'collect', 'forward'])){
  154. $json = [
  155. 'sign' => $sign,
  156. 'app_id' => config('customer.VIRUS_APP_ID'),
  157. 'behavior_id' => $data['behavior_id'],
  158. 'behavior_flag' => $data['behavior_flag'],
  159. 'behavior_value' => $data['behavior_value'],
  160. 'post_id' => $data['post_id'],
  161. 'post_author_uid' => $data['post_author_uid'],
  162. 'post_desc' => $data['post_desc'],
  163. 'post_cover' => $data['post_cover'],
  164. 'target_id' => (string) $data['target_id'],
  165. 'action_id' => (string) $data['action_id'],
  166. 'extra' => [],
  167. ];
  168. }else{
  169. Log::debug('行为类型错误'.json_encode($data));
  170. return false;
  171. }
  172. $response = $this->client->request('POST', '/v2/record/add', [
  173. 'headers' => [
  174. 'Content-Type' => 'application/json'
  175. ],
  176. 'json' => $json
  177. ]);
  178. Log::debug('virus添加数据成功:'.$response->getBody()->getContents());
  179. return true;
  180. } catch (RequestException $e) {
  181. Log::debug('virus添加数据失败:'.$e->getMessage());
  182. }
  183. }
  184. }