VirusAdd.php 7.5 KB

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