wzq %!s(int64=5) %!d(string=hai) anos
pai
achega
f4eb5124c5

+ 9 - 0
app/Helper/helper.php

@@ -15,4 +15,13 @@ if ( ! function_exists('config_path'))
     {
         return app()->basePath() . '/config' . ($path ? '/' . $path : $path);
     }
+
+    function subtext($text, $length)
+    {
+        if(mb_strlen($text, 'utf8') > $length) {
+            return mb_substr($text, 0, $length, 'utf8').'...';
+        } else {
+        return $text;
+    }
+}
 }

+ 8 - 6
app/Http/Controllers/Post/PostController.php

@@ -38,11 +38,13 @@ class PostController extends Controller
         $data['extra'] = [
             'filters' => [
                 'keyword',
-                'category_id1',
-                'up_status',
-                'total_stock',
-                'shop_id',
-                'is_delete'
+                'content',
+                'topic_ids',
+                'category_ids',
+                'is_suggest',
+                'created_at',
+                'type',
+                'sort'
             ],
             'columns' => [
                 'id',
@@ -70,7 +72,7 @@ class PostController extends Controller
     {
         $validator = Validator::make($request->all(), [
             'uid' => 'required|integer',
-            'type' => ['required',Rule::in('image', 'video', 'text')],
+            'type' => ['required',Rule::in('image', 'video', 'html')],
             'is_suggest' => ['required',Rule::in(0, 1)],
             'img' => 'required|url',
             'video' => 'required_if:type,video|string|url',

+ 16 - 0
app/Models/Category.php

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

+ 16 - 0
app/Models/CategoryTopic.php

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

+ 1 - 1
app/Models/Topic.php

@@ -11,7 +11,7 @@ use Illuminate\Database\Eloquent\Model;
 
 class Topic extends Model
 {
-//
+    //
     protected $table = 'topic';
     protected $guarded = [];
 }

+ 47 - 9
app/Repositories/Post/PostRepository.php

@@ -8,6 +8,7 @@
  */
 namespace App\Repositories\Post;
 
+use App\Models\CategoryTopic;
 use App\Models\Post;
 use App\Models\PostComment;
 use App\Models\PostData;
@@ -30,6 +31,7 @@ class PostRepository
                                 PostComment $postComment,
                                 PostImgs $postImgs,
                                 PostLog $postLog,
+                                CategoryTopic $categoryTopic,
                                 Topic $topic)
     {
         $this->post = $post;
@@ -37,6 +39,7 @@ class PostRepository
         $this->postComment = $postComment;
         $this->postImgs = $postImgs;
         $this->postLog = $postLog;
+        $this->categoryTopic = $categoryTopic;
         $this->topic = $topic;
     }
 
@@ -131,23 +134,58 @@ class PostRepository
     {
         $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
         $where = [];
-        if(isset($request['up_status'])){
-            $where[] = ['up_status', $request['up_status']];
+        if(isset($request['content'])){
+            $where[] = ['content', 'like', "%{$request['content']}%"];
         }
-        if(isset($request['total_stock'])){
-            $where[] = ['total_stock', $request['total_stock']];
+        if(isset($request['is_suggest'])){
+            $where[] = ['is_suggest', $request['is_suggest']];
         }
-        if(isset($request['category_id1'])){
-            $where[] = ['category_id1', $request['category_id1']];
+        if(isset($request['type'])){
+            $where[] = ['type', $request['type']];
         }
 
-        return $this->post->where($where)
+        $sort = 'post.id';
+        if(isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count'])){
+            $sort = $request['sort'];
+        }
+
+        return $this->post
+            ->join('post_data', 'post_data.post_id', '=', 'post.id')
+            ->select('post.*')
+            ->where($where)
             ->where(function($query) use ($request){
                 if(isset($request['keyword'])){
-                    $query->where('name', 'like', "%{$request['keyword']}%")
-                        ->orWhere('spu_code', 'like', "%{$request['keyword']}%");
+                    $query->where('uid', '=', $request['keyword'])
+                        ->orWhere('username', 'like', "%{$request['keyword']}%")
+                        ->orWhere('mobile', 'like', "%{$request['keyword']}%");
+                }
+            })
+            ->where(function($query) use ($request){
+                if(isset($request['created_at'])){
+                    $time = explode('_', $request['created_at']);
+                    $query->whereBetween('post.created_at', $time);
+                }
+            })
+            ->where(function ($query) use ($request){
+                if(isset($request['category_ids']) || isset($request['topic_ids'])){
+                    $ids = [];
+                    if (isset($request['category_ids'])) {
+                        $categoryIds = explode('_', $request['category_ids']);
+                        $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
+                    }
+                    if (isset($request['topic_ids'])) {
+                        $ids = array_merge($ids, explode('_', $request['topic_ids']));
+                    }
+                    foreach ($ids as $key=>$id) {
+                        if ($key==0) {
+                            $query = $query->whereRaw('FIND_IN_SET('.$id.',topic_ids)');
+                        } else {
+                            $query = $query->orWhereRaw('FIND_IN_SET('.$id.',topic_ids)');
+                        }
+                    }
                 }
             })
+            ->orderBy($sort,'desc')
             ->paginate($perPage);
     }
 

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

@@ -27,7 +27,7 @@ class PostTransformer extends TransformerAbstract
             'username' => $post['username'],
             'avatar' => $post['avatar'],
             'topic' => $post->topic(),
-            'content' => $post['content'],
+            'content' => subtext($post['content'], 20),
             'location' => $post['location'],
             'pv' => $post->data->pv,
             'praise_count' => $post->data->praise_count,

+ 1 - 1
database/migrations/2019_06_03_162324_create_table_post.php

@@ -35,7 +35,7 @@ class CreateTablePost extends Migration
 
             $table->string('type', 16)
                 ->default('image')
-                ->comment('类型:image图片,video视频,text图文');
+                ->comment('类型:image图文,video视频,html富文本');
 
             $table->string('img', 255)
                 ->comment('主图');