VirusAdd.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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->virusClient = new Client([
  44. 'base_uri' => config('customer.VIRUS_URL'),
  45. 'timeout' => 3
  46. ]);
  47. Log::debug('virusClient:'.json_encode($this->virusClient));
  48. }
  49. public function getConnection()
  50. {
  51. $conn = false;
  52. if ($this->connection) {
  53. return $this->connection;
  54. }
  55. for ($i = 0; $i < 3; $i++) {
  56. $connection = $this->createConn();
  57. if ($connection) {
  58. $conn = $connection;
  59. break;
  60. }
  61. Log::info("create amqp conn retry=" . $i);
  62. }
  63. return $conn;
  64. }
  65. public function createConn()
  66. {
  67. try {
  68. $connection = new AMQPStreamConnection(env('MQ_HOST'), env('MQ_PORT'), env('MQ_USERNAME'), env('MQ_PWD'), env('MQ_VHOST'));
  69. } catch (\Exception $exception) {
  70. Log::info("AMQP connection Error" . $exception->getMessage());
  71. $connection = false;
  72. }
  73. return $connection;
  74. }
  75. /**
  76. * Execute the console command.
  77. *
  78. * @return mixed
  79. */
  80. public function handle()
  81. {
  82. Log::info('接收一条消息');
  83. $queue_name = $this->queue;
  84. $this->channel->queue_declare($queue_name, false, true, false, false);
  85. $this->channel->queue_bind($queue_name, $this->queue);
  86. $callback = function ($msg) {
  87. $this->line(date('Y-m-d H:i:S').$msg->body);
  88. $data = \GuzzleHttp\json_decode($msg->body, true);
  89. $this->line('接收一条消息收到数据' . $msg->body);
  90. $res = $this->send($data);
  91. if ($res) {
  92. $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
  93. }
  94. };
  95. $this->channel->basic_consume($queue_name, '', false, false, false, false, $callback);
  96. while (count($this->channel->callbacks)) {
  97. $this->channel->wait();
  98. }
  99. $this->channel->close();
  100. $this->connection->close();
  101. }
  102. private function send($data)
  103. {
  104. Log::debug('client:'.json_encode($this->virusClient));
  105. try {
  106. // 签名设置
  107. $signKey = [
  108. 'app_id' => config('customer.VIRUS_APP_ID'),
  109. 'behavior_id' => $data['behavior_id'],
  110. 'target_id' => (string) $data['target_id'],
  111. 'action_id' => (string) $data['action_id'],
  112. 'behavior_flag' => $data['behavior_flag']
  113. ];
  114. ksort($signKey);
  115. $signKey = urldecode(http_build_query($signKey));
  116. $sign = md5(config('customer.VIRUS_APP_SECRET') . $signKey);
  117. Log::debug('签名:'.$sign);
  118. $json = [];
  119. if($data['behavior_flag'] == 'publish'){
  120. $json = [
  121. 'sign' => $sign,
  122. 'app_id' => config('customer.VIRUS_APP_ID'),
  123. 'behavior_id' => $data['behavior_id'],
  124. 'behavior_flag' => $data['behavior_flag'],
  125. 'post_id' => $data['post_id'],
  126. 'post_desc' => $data['post_desc'],
  127. 'post_cover' => $data['post_cover'],
  128. 'target_id' => (string) $data['target_id'],
  129. 'action_id' => (string) $data['action_id'],
  130. 'extra' => [],
  131. ];
  132. }elseif($data['behavior_flag'] == 'comment'){
  133. $json = [
  134. 'sign' => $sign,
  135. 'app_id' => config('customer.VIRUS_APP_ID'),
  136. 'behavior_id' => $data['behavior_id'],
  137. 'behavior_flag' => $data['behavior_flag'],
  138. 'post_id' => $data['post_id'],
  139. 'post_author_uid' => $data['post_author_uid'],
  140. 'post_desc' => $data['post_desc'],
  141. 'post_cover' => $data['post_cover'],
  142. 'comment_id' => $data['comment_id'],
  143. 'comment_content' => $data['comment_content'],
  144. 'parent_comment_id' => $data['parent_comment_id'],
  145. 'parent_comment_content' => $data['parent_comment_content'],
  146. 'parent_comment_uid' => $data['parent_comment_uid'],
  147. 'parent_comment_time' => $data['parent_comment_time'],
  148. 'reply_uid' => $data['reply_uid'],
  149. 'reply_username' => $data['reply_username'],
  150. 'target_id' => (string) $data['target_id'],
  151. 'action_id' => (string) $data['action_id'],
  152. 'extra' => [],
  153. ];
  154. }elseif(in_array($data['behavior_flag'], ['read', 'like', 'collect', 'forward'])){
  155. $json = [
  156. 'sign' => $sign,
  157. 'app_id' => config('customer.VIRUS_APP_ID'),
  158. 'behavior_id' => $data['behavior_id'],
  159. 'behavior_flag' => $data['behavior_flag'],
  160. 'behavior_value' => $data['behavior_value'],
  161. 'post_id' => $data['post_id'],
  162. 'post_author_uid' => $data['post_author_uid'],
  163. 'post_desc' => $data['post_desc'],
  164. 'post_cover' => $data['post_cover'],
  165. 'target_id' => (string) $data['target_id'],
  166. 'action_id' => (string) $data['action_id'],
  167. 'extra' => [],
  168. ];
  169. }else{
  170. Log::debug('行为类型错误'.json_encode($data));
  171. return false;
  172. }
  173. $response = $this->virusClient->request('POST', '/v2/record/add', [
  174. 'headers' => [
  175. 'Content-Type' => 'application/json'
  176. ],
  177. 'json' => $json
  178. ]);
  179. Log::debug('virus添加数据成功:'.$response->getBody()->getContents());
  180. return true;
  181. } catch (RequestException $e) {
  182. Log::debug('virus添加数据失败:'.$e->getMessage());
  183. }
  184. }
  185. }