Bladeren bron

取消关注删除feed

wzq 5 jaren geleden
bovenliggende
commit
0059ec1d14

+ 0 - 1
app/Console/Commands/ContentFeedCreate.php

@@ -3,7 +3,6 @@
 namespace App\Console\Commands;
 
 use App\Repositories\FeedRepositories;
-use App\Repositories\ProductSkuRepository;
 use Illuminate\Console\Command;
 use Illuminate\Support\Facades\Log;
 use PhpAmqpLib\Connection\AMQPStreamConnection;

+ 106 - 0
app/Console/Commands/ContentFeedDelete.php

@@ -0,0 +1,106 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/7/17
+ * Time: 9:33
+ */
+namespace App\Console\Commands;
+
+
+use App\Repositories\FeedRepositories;
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Log;
+use PhpAmqpLib\Connection\AMQPStreamConnection;
+
+class ContentFeedDelete extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'content:feed_delete';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '取消关注删除对应feed';
+
+    protected $channel;
+    protected $connection;
+
+    protected $queue = 'cancel_follow';
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct(FeedRepositories $feedRepositories)
+    {
+        parent::__construct();
+        $this->feedRepositories = $feedRepositories;
+        $this->connection = $this->getConnection();
+        $this->channel = $this->connection->channel();
+        $this->channel->exchange_declare($this->queue, 'fanout', false, true, false);
+    }
+
+    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('取消关注删除对应feed');
+        $queue_name = $this->queue.'_delete_feed';
+        $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);
+            $this->feedRepositories->contentFeedDelete($data);
+            $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();
+
+
+    }
+}

+ 2 - 0
app/Console/Kernel.php

@@ -6,6 +6,7 @@ use App\Console\Commands\Apollo;
 use App\Console\Commands\BehaviorRecord;
 use App\Console\Commands\CalcPostWeight;
 use App\Console\Commands\ContentFeedCreate;
+use App\Console\Commands\ContentFeedDelete;
 use App\Console\Commands\ExcellentResidents;
 use App\Console\Commands\RankingList;
 use Illuminate\Console\Scheduling\Schedule;
@@ -23,6 +24,7 @@ class Kernel extends ConsoleKernel
         BehaviorRecord::class,
         CalcPostWeight::class,
         ContentFeedCreate::class,
+        ContentFeedDelete::class,
         ExcellentResidents::class,
         RankingList::class
     ];

+ 14 - 0
app/Repositories/FeedRepositories.php

@@ -208,4 +208,18 @@ class FeedRepositories
        return $this->postComment->where(['post_id'=>$post_id,'parent_id'=>0,'is_delete'=>0  ])->select('id','uid','username','content')->orderBy('id','desc')->take(2)->get();
     }
 
+    /**
+     *  取消关注删除对应feed
+     * @param $data
+     */
+    public function contentFeedDelete($data){
+        try{
+            $this->feed->where('uid', $data['uid'])->where('follow_uid', $data['follow_uid'])->delete();
+            Log::debug("取消关注删除对应feed成功uid{$data['uid']}followUid{$data['follow_uid']}");
+        }catch (\Exception $exception){
+            Log::error('取消关注删除对应feed失败'.json_encode($data).$exception->getMessage());
+            return false;
+        }
+    }
+
 }