|
@@ -0,0 +1,161 @@
|
|
|
+<?php
|
|
|
+/**
|
|
|
+ * Created by PhpStorm.
|
|
|
+ * User: Administrator
|
|
|
+ * Date: 2019/6/24
|
|
|
+ * Time: 9:52
|
|
|
+ */
|
|
|
+namespace App\Console\Commands;
|
|
|
+
|
|
|
+
|
|
|
+use GuzzleHttp\Exception\RequestException;
|
|
|
+use Illuminate\Console\Command;
|
|
|
+use Illuminate\Support\Facades\Log;
|
|
|
+use PhpAmqpLib\Connection\AMQPStreamConnection;
|
|
|
+use GuzzleHttp\Client;
|
|
|
+
|
|
|
+class VirusAdd extends Command
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * The name and signature of the console command.
|
|
|
+ *
|
|
|
+ * @var string
|
|
|
+ */
|
|
|
+ protected $signature = 'virus:add';
|
|
|
+
|
|
|
+ /**
|
|
|
+ * The console command description.
|
|
|
+ *
|
|
|
+ * @var string
|
|
|
+ */
|
|
|
+ protected $description = 'virus添加数据';
|
|
|
+
|
|
|
+ protected $channel;
|
|
|
+ protected $connection;
|
|
|
+
|
|
|
+ protected $queue = 'virus_add';
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Create a new command instance.
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function __construct()
|
|
|
+ {
|
|
|
+ parent::__construct();
|
|
|
+ $this->connection = $this->getConnection();
|
|
|
+ $this->channel = $this->connection->channel();
|
|
|
+ $this->channel->exchange_declare($this->queue, 'fanout', false, true, false);
|
|
|
+ // 初始化virus域名
|
|
|
+ $this->client = new Client([
|
|
|
+ 'base_uri' => config('customer.VIRUS_URL'),
|
|
|
+ 'timeout' => 3
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getConnection()
|
|
|
+ {
|
|
|
+ $conn = false;
|
|
|
+ if ($this->connection) {
|
|
|
+ return $this->connection;
|
|
|
+ }
|
|
|
+ for ($i = 0; $i < 3; $i++) {
|
|
|
+ $connection = $this->createConn();
|
|
|
+ if ($connection) {
|
|
|
+ $conn = $connection;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ Log::info("create amqp conn retry=" . $i);
|
|
|
+ }
|
|
|
+ return $conn;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function createConn()
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $connection = new AMQPStreamConnection(env('MQ_HOST'), env('MQ_PORT'), env('MQ_USERNAME'), env('MQ_PWD'), env('MQ_VHOST'));
|
|
|
+ } catch (\Exception $exception) {
|
|
|
+ Log::info("AMQP connection Error" . $exception->getMessage());
|
|
|
+ $connection = false;
|
|
|
+ }
|
|
|
+ return $connection;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Execute the console command.
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function handle()
|
|
|
+ {
|
|
|
+ Log::info('接收一条消息');
|
|
|
+ $queue_name = $this->queue;
|
|
|
+ $this->channel->queue_declare($queue_name, false, true, false, false);
|
|
|
+ $this->channel->queue_bind($queue_name, $this->queue);
|
|
|
+ $callback = function ($msg) {
|
|
|
+ $this->line(date('Y-m-d H:i:S').$msg->body);
|
|
|
+ $data = \GuzzleHttp\json_decode($msg->body, true);
|
|
|
+ $this->line('接收一条消息收到数据' . $msg->body);
|
|
|
+ $res = $this->send($data);
|
|
|
+ if ($res) {
|
|
|
+ $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ $this->channel->basic_consume($queue_name, '', false, false, false, false, $callback);
|
|
|
+
|
|
|
+ while (count($this->channel->callbacks)) {
|
|
|
+ $this->channel->wait();
|
|
|
+ }
|
|
|
+ $this->channel->close();
|
|
|
+ $this->connection->close();
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private function send($data)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ // 签名设置
|
|
|
+ $signKey = [
|
|
|
+ 'app_id' => config('customer.VIRUS_APP_ID'),
|
|
|
+ 'behavior_id' => $data['behavior_id'],
|
|
|
+ 'target_id' => (string) $data['target_id'],
|
|
|
+ 'action_id' => (string) $data['action_id'],
|
|
|
+ 'behavior_flag' => $data['behavior_flag']
|
|
|
+ ];
|
|
|
+ ksort($signKey);
|
|
|
+ $signKey = urldecode(http_build_query($signKey));
|
|
|
+ $sign = md5(config('customer.VIRUS_APP_SECRET') . $signKey);
|
|
|
+ Log::debug('签名:'.$sign);
|
|
|
+ $json = [];
|
|
|
+ if($data['behavior_flag'] == 'publish'){
|
|
|
+ $json = [
|
|
|
+ 'app_id' => config('customer.VIRUS_APP_ID'),
|
|
|
+ 'behavior_id' => $data['behavior_id'],
|
|
|
+ 'behavior_flag' => $data['behavior_flag'],
|
|
|
+ 'post_id' => $data['post_id'],
|
|
|
+ 'post_desc' => $data['post_desc'],
|
|
|
+ 'post_cover' => $data['post_cover'],
|
|
|
+ 'target_id' => (string) $data['target_id'],
|
|
|
+ 'action_id' => (string) $data['action_id'],
|
|
|
+ 'extra' => [],
|
|
|
+ ];
|
|
|
+ }else{
|
|
|
+ Log::debug('行为类型错误'.json_encode($data));
|
|
|
+ }
|
|
|
+
|
|
|
+ $response = $this->client->request('POST', 'v2/record/add', [
|
|
|
+ 'headers' => [
|
|
|
+ 'Content-Type' => 'application/json',
|
|
|
+ 'sign' => $sign
|
|
|
+ ],
|
|
|
+ 'json' => $json
|
|
|
+ ]);
|
|
|
+// $result = \GuzzleHttp\json_decode($response->getBody()->getContents(), true);
|
|
|
+ Log::debug('virus添加数据成功:'.$response->getBody()->getContents());
|
|
|
+ return true;
|
|
|
+ } catch (RequestException $e) {
|
|
|
+ Log::debug('virus添加数据失败:'.$e->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|