wzq 5 年 前
コミット
f938d3f65e
共有7 個のファイルを変更した186 個の追加3 個の削除を含む
  1. 2 2
      app/Http/Controllers/Post/PostController.php
  2. 17 0
      app/Models/PostComment.php
  3. 17 0
      app/Models/PostData.php
  4. 19 0
      app/Models/PostImgs.php
  5. 17 0
      app/Models/PostLog.php
  6. 17 0
      app/Models/Topic.php
  7. 97 1
      app/Repositories/Post/PostRepository.php

+ 2 - 2
app/Http/Controllers/Post/PostController.php

@@ -33,12 +33,12 @@ class PostController extends Controller
             'uid' => 'required|integer',
             'type' => ['required',Rule::in('image', 'video', 'text')],
             'img' => 'required|url',
-            'video' => 'nullable|string|url',
+            'video' => 'required_if:type,video|string|url',
             'topic_ids' => 'required|string|max:64',
             'title' => 'nullable|string|max:20',
             'content' => 'required|string|max:20',
             'location' => 'required|string',
-            'imgs' => 'required|array',
+            'imgs' => 'required_if:type,image|array',
             'imgs.*' => 'required|url',
         ]);
         if ($validator->fails()) {

+ 17 - 0
app/Models/PostComment.php

@@ -0,0 +1,17 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/6/6
+ * Time: 11:21
+ */
+
+namespace App\Models;
+use Illuminate\Database\Eloquent\Model;
+
+class PostComment extends Model
+{
+//
+    protected $table = 'post_comment';
+    protected $guarded = [];
+}

+ 17 - 0
app/Models/PostData.php

@@ -0,0 +1,17 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/6/6
+ * Time: 11:21
+ */
+
+namespace App\Models;
+use Illuminate\Database\Eloquent\Model;
+
+class PostData extends Model
+{
+//
+    protected $table = 'post_data';
+    protected $guarded = [];
+}

+ 19 - 0
app/Models/PostImgs.php

@@ -0,0 +1,19 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/6/6
+ * Time: 11:21
+ */
+
+namespace App\Models;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\SoftDeletes;
+
+class PostImgs extends Model
+{
+//
+    use SoftDeletes;
+    protected $table = 'post_imgs';
+    protected $guarded = [];
+}

+ 17 - 0
app/Models/PostLog.php

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

+ 17 - 0
app/Models/Topic.php

@@ -0,0 +1,17 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/6/6
+ * Time: 11:21
+ */
+
+namespace App\Models;
+use Illuminate\Database\Eloquent\Model;
+
+class Topic extends Model
+{
+//
+    protected $table = 'topic';
+    protected $guarded = [];
+}

+ 97 - 1
app/Repositories/Post/PostRepository.php

@@ -8,6 +8,12 @@
  */
 namespace App\Repositories\Post;
 
+use App\Models\Post;
+use App\Models\PostComment;
+use App\Models\PostData;
+use App\Models\PostImgs;
+use App\Models\PostLog;
+use App\Models\Topic;
 use Illuminate\Database\QueryException;
 use Dingo\Api\Http\Response;
 use Illuminate\Support\Facades\DB;
@@ -18,12 +24,102 @@ use Tymon\JWTAuth\Facades\JWTAuth;
 class PostRepository
 {
 
+    public function __construct(Post $post,
+                                PostData $postData,
+                                PostComment $postComment,
+                                PostImgs $postImgs,
+                                PostLog $postLog,
+                                Topic $topic)
+    {
+        $this->post = $post;
+        $this->postData = $postData;
+        $this->postComment = $postComment;
+        $this->postImgs = $postImgs;
+        $this->postLog = $postLog;
+        $this->topic = $topic;
+    }
+
     /**
      * 发布内容
      */
     public function create($request)
     {
-        return $request;
+        //验证小号
+
+        //验证话题
+        $topicIds = $this->topic->whereIn('id', explode(',', $request['topic_ids']))->pluck('id')->toArray();
+        $topicCount = count($topicIds);
+        if($topicCount == 0 || $topicCount >= 5){
+            throw new HttpException(500, '所选话题必须1-5个');
+        }
+        $topicIds = implode(',', $topicIds);
+
+        $data = [
+            'uid' => $request['uid'],
+            'username' => '暂无',
+            'mobile' => '暂无',
+            'avatar' => '暂无',
+            'type' => $request['type'],
+            'img' => $request['img'],
+            'video' => $request['video']??'',
+            'topic_ids' => $topicIds,
+            'title' => $request['title']??'',
+            'content' => $request['content'],
+            'location' => $request['location']??'',
+            'is_suggest' => 0,
+            'is_hide' => 0
+        ];
+
+        $date = date('Y-m-d H:i:s');
+
+
+        DB::beginTransaction();
+        try{
+            $post = $this->post->create($data);
+
+            $this->postData->create([
+                'post_id' => $post->id,
+                'pv' => 0,
+                'pv_real' => 0,
+                'dislike_count' => 0,
+                'praise_count' => 0,
+                'praise_real_count' => 0,
+                'share_count' => 0,
+                'share_real_count' => 0,
+                'comment_count' => 0,
+                'comment_real_count' => 0,
+                'collect_count' => 0,
+                'collect_real_count' => 0,
+                'available_bean' => 0,
+                'will_collect_bean' => 0,
+                'collect_bean' => 0,
+                'weight' => 0
+            ]);
+
+            if($request['imgs'] && $request['type'] == 'image'){
+                $imgData = [];
+                foreach($request['imgs'] as $img){
+                    $imgData[] = [
+                        'post_id' => $post->id,
+                        'img' => $img,
+                        'created_at' => $date,
+                        'updated_at' => $date
+                    ];
+                }
+                $this->postImgs->insert($imgData);
+            }
+
+            DB::commit();
+            return Response::create();
+
+        }catch (QueryException $exception){
+            DB::rollBack();
+            return Response::create([
+                'message'  => '发布失败,请重试',
+                'error' => $exception->getMessage(),
+                'status_code'   => 500
+            ]);
+        }
     }
 
 }