瀏覽代碼

Merge branch 'develop' of http://git.caihongxingqiu.net/rainbow/community-manage into develop

xielin 5 年之前
父節點
當前提交
548f954ca7

+ 2 - 1
app/Console/Commands/UpdatePostInfo.php

@@ -73,7 +73,8 @@ class UpdatePostInfo extends Command
                     'available_bean', $post->data->available_bean,
                     'will_collect_bean', $post->data->will_collect_bean,
                     'create_bean', $post->data->create_bean,
-                    'collect_bean', $post->data->collect_bean);
+                    'collect_bean', $post->data->collect_bean,
+                    'created_at', $post->created_at);
             }
             usleep(100000);
         });

+ 68 - 0
app/Console/Commands/UpdateReplyCount.php

@@ -0,0 +1,68 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/8/13
+ * Time: 10:59
+ */
+namespace App\Console\Commands;
+
+
+use App\Models\PostComment;
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Redis;
+
+class UpdateReplyCount extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'post:update_reply_count';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '更新评论回复数量';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct(PostComment $postComment)
+    {
+        parent::__construct();
+        $this->postComment = $postComment;
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        $this->line("开始更新评论回复数量");
+
+        $bar = $this->output->createProgressBar($this->postComment->where('parent_id', 0)->count());
+        $this->postComment->where('parent_id', 0)->chunk(100, function($comments) use ($bar){
+            foreach($comments as $comment){
+                $replyCount = $this->postComment->where('parent_id', $comment->id)->count();
+                if($replyCount){
+                    $comment->reply_count = $replyCount;
+                    $comment->save();
+                }
+
+                $bar->advance();
+            }
+            usleep(100000);
+        });
+        $bar->finish();
+        $this->line("\n更新评论回复数量结束");
+
+    }
+}

+ 2 - 0
app/Console/Kernel.php

@@ -12,6 +12,7 @@ use App\Console\Commands\PostStatistics;
 use App\Console\Commands\PostYesterday;
 use App\Console\Commands\UpdatePostInfo;
 use App\Console\Commands\UpdatePostStatus;
+use App\Console\Commands\UpdateReplyCount;
 use App\Console\Commands\VirusAdd;
 use Illuminate\Console\Scheduling\Schedule;
 use Laravel\Lumen\Console\Kernel as ConsoleKernel;
@@ -33,6 +34,7 @@ class Kernel extends ConsoleKernel
         PostCollectBean::class,
         UpdatePostInfo::class,
         UpdatePostStatus::class,
+        UpdateReplyCount::class,
         CommunityMemberStatistics::class,
         MusicImport::class
     ];

+ 1 - 0
app/Http/Controllers/ConfigController.php

@@ -39,6 +39,7 @@ class ConfigController extends Controller
             'log_type' => [
                 'add_data' => '增加数据',
                 'delete' => '删除内容',
+                'update_topic' => '编辑内容话题',
             ],
             //是否推荐
             'is_suggest' => [

+ 15 - 0
app/Http/Controllers/Post/PostController.php

@@ -331,4 +331,19 @@ class PostController extends Controller
         $end = Carbon::parse(!empty($request['end']) ? Carbon::parse($request['end'])->endOfDay()->toDateTimeString() : Carbon::yesterday())->endOfDay()->toDateTimeString();
         return $this->postRepository->statistics($start,$end);
     }
+
+    /**
+     * 编辑内容话题
+     */
+    public function updateTopic(Request $request)
+    {
+        $validator = Validator::make($request->all(), [
+            'id' => 'required|integer',
+            'topic_ids' => 'required|string|max:64',
+        ]);
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(), 500);
+        }
+        return  $this->postRepository->updateTopic($request->all());
+    }
 }

+ 38 - 0
app/Http/Middleware/SqlMiddleware.php

@@ -0,0 +1,38 @@
+<?php
+
+namespace App\Http\Middleware;
+
+use Closure;
+use Illuminate\Support\Facades\DB;
+
+class SqlMiddleware
+{
+    /**
+     * Handle an incoming request.
+     *
+     * @param  \Illuminate\Http\Request $request
+     * @param  \Closure $next
+     * @return mixed
+     */
+    public function handle($request, Closure $next)
+    {
+        if ($request->sql_debug==1) {
+            DB::connection()->enableQueryLog();
+        }
+
+        $response = $next($request);
+        if ($request->get('sql_debug')) {
+            $queries = DB::getQueryLog();
+
+            if (!empty($queries)) {
+                foreach ($queries as &$query) {
+                    $query['full_query'] = vsprintf(str_replace('?', '%s', $query['query']), $query['bindings']);
+                }
+            }
+
+            dd($queries);exit;
+        }
+
+        return $response;
+    }
+}

+ 4 - 4
app/Models/Post.php

@@ -27,9 +27,9 @@ class Post extends Model
         return $this->hasOne('App\Models\PostData', 'post_id', 'id');
     }
 
-    public function topic()
-    {
-        return Topic::whereIn('id', explode(',', $this->topic_ids))->pluck('name', 'id');
-    }
+//    public function topic()
+//    {
+//        return Topic::whereIn('id', explode(',', $this->topic_ids))->pluck('name', 'id');
+//    }
 
 }

+ 72 - 2
app/Repositories/Post/PostRepository.php

@@ -206,7 +206,8 @@ class PostRepository
                 'available_bean', $postData->available_bean,
                 'will_collect_bean', $postData->will_collect_bean,
                 'create_bean', $postData->create_bean,
-                'collect_bean', $postData->collect_bean);
+                'collect_bean', $postData->collect_bean,
+                'created_at', $post->created_at);
 
             return Response::create();
 
@@ -430,6 +431,10 @@ class PostRepository
         try {
             $comment = $this->postComment->create($data);
 
+            if($comment->parent_id){
+                $this->postComment->where('id', $comment->parent_id)->increment('reply_count');
+            }
+
             DB::commit();
 
             if(!$comment->parent_id){
@@ -506,6 +511,7 @@ class PostRepository
 
         return $post
             ->join('post_data', 'post_data.post_id', '=', 'post.id')
+            ->with('data')
             ->select('post.*')
             ->where($where)
             ->where(function ($query) use ($request) {
@@ -950,7 +956,7 @@ class PostRepository
                                 Carbon::parse($post->created_at)->toDateTimeString(),
                                 $post->username,
                                 $post->location,
-                                implode(' ', $post->topic()->toArray()),
+                                implode(' ', $this->getTopic($post->topic_ids)),
                                 subtext(strip_tags($post->content), 20),
                                 $post->data->pv_real,
                                 $post->data->pv,
@@ -1054,5 +1060,69 @@ class PostRepository
         return $info;
     }
 
+    /**
+     * 编辑内容话题
+     */
+    public function updateTopic($request)
+    {
+        $token = JWTAuth::decode(JWTAuth::getToken());
+        if($token['type'] != 1){
+            return Response::create([
+                'message' => '只有运营才能删除内容',
+                'status_code' => 500
+            ]);
+        }
+        $post = $this->post->where('id', $request['id'])->first();
+        if (!$post) {
+            return Response::create([
+                'message' => '获取内容信息失败',
+                'status_code' => 500
+            ]);
+        }
+
+        //验证话题
+        $topicIdsArray = $this->topic->whereIn('id', explode(',', $request['topic_ids']))->pluck('id')->toArray();
+        $topicCount = count($topicIdsArray);
+        if ($topicCount == 0 || $topicCount > 5) {
+            return Response::create([
+                'message' => '所选话题必须1-5个',
+                'status_code' => 500
+            ]);
+        }
+        $topicIds = implode(',', $topicIdsArray);
+
+        $logData = [
+            'uid' => $token['user']->id,
+            'operator_type' => 'admin',
+            'post_id' => $request['id'],
+            'username' => $token['user']->username,
+            'log_type' => 'update_topic',
+            'content' => json_encode(['update_topic' => $request['id'].'-'.$topicIds]),
+        ];
+
+        DB::beginTransaction();
+        try {
+            $post->topic_ids = $topicIds;
+            $post->save();
+
+            $this->postLog->create($logData);
+
+            DB::commit();
+
+            Redis::HSET('post_info_'.$request['id'], 'topic_ids', $topicIds);
+
+            return Response::create();
+
+        } catch (QueryException $exception) {
+            DB::rollBack();
+            Log::debug('编辑内容话题:'.$post->id . $exception->getMessage());
+            return Response::create([
+                'message' => '编辑失败,请重试',
+                'error' => $exception->getMessage(),
+                'status_code' => 500
+            ]);
+        }
+    }
+
 
 }

+ 22 - 0
app/Traits/PostTrait.php

@@ -31,4 +31,26 @@ trait PostTrait
         }
         return intval($num * $t);
     }
+
+    //获取内容话题
+    public function getTopic($topic_ids, $type = 0)
+    {
+        $ids = explode(',', $topic_ids);
+        $topic = [];
+        foreach($ids as $id){
+            $name = Redis::ZRANGEBYSCORE('topic.name', $id, $id);
+            if($name && isset($name[0])){
+                if($type){
+                    $topic[] = [
+                        'id' => intval($id),
+                        'name' => $name[0],
+                    ];
+                }else{
+                    $topic[] = $name[0];
+                }
+
+            }
+        }
+        return $topic;
+    }
 }

+ 3 - 1
app/Transformers/Post/DetailTransformer.php

@@ -8,11 +8,13 @@
 
 namespace App\Transformers\Post;
 use App\Models\Post;
+use App\Traits\PostTrait;
 use Illuminate\Support\Carbon;
 use League\Fractal\TransformerAbstract;
 
 class DetailTransformer extends  TransformerAbstract
 {
+    use PostTrait;
     public function transform(Post $post)
     {
         $imgs = [];
@@ -25,7 +27,7 @@ class DetailTransformer extends  TransformerAbstract
             'uid' => $post['uid'],
             'username' => $post['username'],
             'avatar' => $post['avatar'],
-            'topic' => $post->topic(),
+            'topic' => $this->getTopic($post['topic_ids']),
             'title' => $post['title'],
             'content' => $post['content'],
             'type' => $post['type'],

+ 1 - 0
app/Transformers/Post/LogTransformer.php

@@ -21,6 +21,7 @@ class LogTransformer extends TransformerAbstract
             'add_collect_count' => '增加收藏数:',
             'add_share_count' => '增加分享数:',
             'delete' => '删除内容:',
+            'update_topic' => '编辑内容话题:',
         ];
         $data = json_decode($postLog['content']);
         $content = '';

+ 3 - 1
app/Transformers/Post/PostTransformer.php

@@ -9,11 +9,13 @@
 namespace App\Transformers\Post;
 
 use App\Models\Post;
+use App\Traits\PostTrait;
 use Illuminate\Support\Carbon;
 use League\Fractal\TransformerAbstract;
 
 class PostTransformer extends TransformerAbstract
 {
+    use PostTrait;
     public function transform(Post $post)
     {
         return [
@@ -22,7 +24,7 @@ class PostTransformer extends TransformerAbstract
             'uid' => $post['uid'],
             'username' => $post['username'],
             'avatar' => $post['avatar'],
-            'topic' => $post->topic(),
+            'topic' => $this->getTopic($post['topic_ids'], 1),
             'content' => subtext(strip_tags($post['content']), 20),
             'location' => $post['location'],
             'pv' => $post->data->pv_real.'/'.$post->data->pv,

+ 4 - 0
bootstrap/app.php

@@ -69,6 +69,10 @@ $app->singleton(
 //     App\Http\Middleware\ExampleMiddleware::class
 // ]);
 
+$app->middleware([
+    App\Http\Middleware\SqlMiddleware::class
+]);
+
 $app->routeMiddleware([
     'auth' => App\Http\Middleware\Authenticate::class,
     'jwt.chxq_auth' => App\Http\Middleware\JwtAuthMiddleware::class,

+ 32 - 0
database/migrations/2019_08_14_090944_add_idx_mid_category_id_to_post_music_category_rel_table.php

@@ -0,0 +1,32 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class AddIdxMidCategoryIdToPostMusicCategoryRelTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('post_music_category_rel', function (Blueprint $table) {
+            $table->index(['mid', 'music_category_id'], 'idx_mid_category_id');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('post_music_category_rel', function (Blueprint $table) {
+            //
+        });
+    }
+}

+ 35 - 0
database/migrations/2019_08_14_135159_add_reply_count_to_table_post_comment.php

@@ -0,0 +1,35 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class AddReplyCountToTablePostComment extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('post_comment', function (Blueprint $table) {
+            $table->integer('reply_count')
+                ->default(0)
+                ->after('content')
+                ->comment('回复数');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('post_comment', function (Blueprint $table) {
+            //
+        });
+    }
+}

+ 2 - 0
routes/api.php

@@ -40,6 +40,8 @@ $api->version('v1', [
             $api->get('statistics', 'PostController@statistics');
             //发布内容
             $api->post('post', 'PostController@create');
+            //编辑发布内容话题
+            $api->put('post/topic', 'PostController@updateTopic');
             //内容列表
             $api->get('post', 'PostController@index');
             //内容详情