wzq 5 anni fa
parent
commit
8c040e74fe

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

@@ -407,4 +407,19 @@ class PostController extends Controller
         return $this->postRepositories->memberPostStatistics($request['uid']);
     }
 
+    /**
+     * 删除内容
+     */
+    public function delete(Request $request)
+    {
+        $validator = Validator::make($request->all(), [
+            'id' => 'required|integer',
+        ]);
+        if ($validator->fails()) {
+            return jsonError($validator->errors()->first());
+        }
+
+        return $this->postRepositories->delete($request->all());
+    }
+
 }

+ 15 - 0
app/Models/PostLog.php

@@ -0,0 +1,15 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/7/9
+ * Time: 15:09
+ */
+namespace App\Models;
+use Illuminate\Database\Eloquent\Model;
+
+class PostLog extends Model
+{
+    protected $table = 'post_log';
+    protected $guarded = [];
+}

+ 47 - 0
app/Repositories/PostRepositories.php

@@ -17,6 +17,7 @@ use App\Models\PostData;
 use App\Models\PostDislike;
 use App\Models\PostImgs;
 use App\Models\PostLike;
+use App\Models\PostLog;
 use App\Models\PostShare;
 use App\Models\Topic;
 use App\Service\AliYunVodService;
@@ -43,6 +44,7 @@ class PostRepositories
                                 PostComment $postComment,
                                 PostCollect $postCollect,
                                 PostShare $postShare,
+                                PostLog $postLog,
                                 DetectionService $detectionService,
                                 AliYunVodService $aliYunVodService,
                                 Topic $topic)
@@ -53,6 +55,7 @@ class PostRepositories
         $this->postComment = $postComment;
         $this->postCollect = $postCollect;
         $this->postShare = $postShare;
+        $this->postLog = $postLog;
         $this->detectionService = $detectionService;
         $this->topic = $topic;
         $this->aliYunVodService = $aliYunVodService;
@@ -619,4 +622,48 @@ class PostRepositories
         return jsonSuccess($data);
     }
 
+    /**
+     * 删除内容
+     */
+    public function delete($request)
+    {
+        //验证用户信息
+        $userInfo = $this->getUserInfo();
+        if (empty($userInfo)) {
+            return jsonError('获取用户信息失败');
+        }
+
+        $post = $this->post->find($request['id']);
+        if(!$post){
+            return jsonError('获取内容信息失败');
+        }
+        if($post->uid != $userInfo['uid']){
+            return jsonError('只能删除自己发布的内容');
+        }
+        $logData = [
+            'uid' => $userInfo['uid'],
+            'operator_type' => 'user',
+            'post_id' => $request['id'],
+            'username' => $userInfo['username'],
+            'log_type' => 'delete',
+            'content' => json_encode(['delete' => $request['id']]),
+        ];
+
+        DB::beginTransaction();
+        try{
+            $post->delete();
+
+            $this->postLog->create($logData);
+
+            DB::commit();
+            Log::debug('删除内容失败:'.$request['id']);
+            return jsonSuccess('删除内容成功');
+
+        }catch (QueryException $exception){
+            DB::rollBack();
+            Log::debug('删除内容失败:'.$request['id'].$exception->getMessage());
+            return jsonError('删除内容失败,请重试');
+        }
+    }
+
 }

+ 2 - 0
routes/api.php

@@ -34,6 +34,8 @@ $api->version('v1', [
     $api->group(['middleware' => ['chxq_jwt_auth','chxq_sign']], function ($api) {
         //发布内容
         $api->post('post', 'PostController@create');
+        //删除内容
+        $api->delete('post', 'PostController@delete');
         //个人中心内容
         $api->get('post/my', 'PostController@myPost');
         //内容列表