xielin лет назад: 5
Родитель
Сommit
b7f56df7d3

+ 16 - 468
app/Http/Controllers/V1/CircleController.php

@@ -8,6 +8,9 @@
 
 namespace App\Http\Controllers\V1;
 
+use App\Repositories\Circle\CircleRepository;
+use App\Traits\UserTrait;
+use App\Transformers\Circle\DetailTransformer;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Log;
 use Illuminate\Support\Facades\Redis;
@@ -20,493 +23,38 @@ use League\Fractal\Resource\Item;
 
 class CircleController extends Controller
 {
-
-    public function __construct()
-    {
-    }
-
-    /**
-     * 发布内容
-     */
-    public function create(Request $request)
-    {
-        $validator = Validator::make($request->all(), [
-            'type' => ['required', Rule::in('image', 'video', 'html')],
-            'img' => 'required|url',
-            'video' => 'required_if:type,video|string',
-            'topic_ids' => 'required|string|max:64',
-            'title' => 'nullable|string|max:20',
-            'content' => 'required|string|max:1000',
-            'location' => 'nullable|string|max:32',
-            'imgs' => 'required_if:type,image|string',
-        ]);
-        if ($validator->fails()) {
-            return jsonError($validator->errors()->first());
-        }
-
-        return $this->postRepositories->create($request->all());
-    }
-
-    /**
-     * 评论&回复
-     */
-    public function comment(Request $request)
+    use UserTrait;
+    public function __construct(CircleRepository $circleRepository)
     {
-        $validator = Validator::make($request->all(), [
-            'post_id' => 'required|integer',
-            'content' => 'required|string|max:150',
-        ]);
-        if ($validator->fails()) {
-            return jsonError($validator->errors()->first());
-        }
-
-        return $this->postRepositories->comment($request->all());
+        $this->circleRepository = $circleRepository;
     }
 
     /**
-     * 删除评论
-     */
-    public function commentDelete(Request $request)
-    {
-        $validator = Validator::make($request->all(), [
-            'id' => 'required|integer',
-        ]);
-        if ($validator->fails()) {
-            return jsonError($validator->errors()->first());
-        }
-        return  $this->postRepositories->commentDelete($request->all());
-    }
-
-    /**
-     * 内容列表
+     * 圈子首页
+     * @param Request $request
      */
     public function index(Request $request)
-    {
-        Log::debug('内容搜索' . json_encode($request));
-        $userInfo = $this->getUserInfo();
-        if ($userInfo) {
-            $uid = $userInfo['uid'];
-            $inviteCode = $userInfo['invite_code'];
-        }else{
-            $uid = 0;
-            $inviteCode = '';
-        }
-        $list = $this->postRepositories->lists($request->all());
-        $fractal = new Manager();
-        $resource = new Collection($list, new ListTransformer($uid, $inviteCode));
-        $resource->setPaginator(new IlluminatePaginatorAdapter($list));
-        $data = $fractal->createData($resource)->toArray();
-
-        return jsonSuccess($data);
-    }
-
-    /**
-     * 视频列表
-     */
-    public function video(Request $request)
     {
         $userInfo = $this->getUserInfo();
         if ($userInfo) {
             $uid = $userInfo['uid'];
-            $inviteCode = $userInfo['invite_code'];
         }else{
             $uid = 0;
-            $inviteCode = '';
         }
-        $list = $this->postRepositories->video($request->all());
-        $fractal = new Manager();
-        $resource = new Collection($list, new VideoTransformer($uid, $inviteCode));
-        $resource->setPaginator(new IlluminatePaginatorAdapter($list));
-        $data = $fractal->createData($resource)->toArray();
-
-        return jsonSuccess($data);
-    }
-
-    /**
-     * 个人中心内容
-     */
-    public function myPost(Request $request)
-    {
         $validator = Validator::make($request->all(), [
-            'type' => ['required', Rule::in('create', 'collect', 'share')],
+            'id' => 'required|exists:interest_circles'
         ]);
         if ($validator->fails()) {
-            return jsonError($validator->errors()->first());
+            return $this->jsonError($validator->errors()->first());
         }
-
-        $userInfo = $this->getUserInfo();
-        if (empty($userInfo)) {
-            Log::debug('获取用户信息失败myPost'.json_encode($request));
-            return jsonError('获取用户信息失败');
-        }
-        $param = $request->all();
-        if (isset($param['uid'])) {
-            $uid = $param['uid'];
-        } else {
-            $uid = $userInfo['uid'];
+        $detail = $this->circleRepository->detail($request->all());
+        if ($detail) {
+            $fractal = new Manager();
+            $res = new Item($detail, new DetailTransformer($uid));
+            $data = $fractal->createData($res)->toArray();
         }
-
-        $list = $this->postRepositories->myPost($param, $uid);
-        $fractal = new Manager();
-        $resource = new Collection($list, new MyTransformer());
-        $resource->setPaginator(new IlluminatePaginatorAdapter($list));
-        $data = $fractal->createData($resource)->toArray();
-
-        return jsonSuccess($data);
+        return $this->jsonSuccess($data);
     }
 
-    /**
-     * 推荐内容列表
-     */
-    public function suggestPost(Request $request)
-    {
-        $userInfo = $this->getUserInfo();
-        if ($userInfo) {
-            $uid = $userInfo['uid'];
-            $inviteCode = $userInfo['invite_code'];
-        }else{
-            $uid = 0;
-            $inviteCode = '';
-        }
-
-        $param = $request->all();
-        $list = $this->postRepositories->suggestPost($param,$uid);
-        $fractal = new Manager();
-        $resource = new Collection($list, new SuggestTransformer($uid, $inviteCode));
-        $resource->setPaginator(new IlluminatePaginatorAdapter($list));
-        $data = $fractal->createData($resource)->toArray();
-
-        if (!(isset($param['page']) && $param['page'] > 1) && !(isset($param['category_id']) && $param['category_id'])) {
-            $key = 'suggest_post_floor';
-            $floor = Redis::get($key);
-            if (!$floor) {
-                $floor = $this->getFloorInfo();
-                if ($floor) {
-                    Redis::set($key, json_encode($floor));
-                    Redis::expire($key, 600);
-                }
-
-            } else {
-                $floor = json_decode($floor, true);
-            }
-            if ($floor) {
-                $newData = [];
-                foreach ($data['data'] as $key => $val) {
-                    if (isset($floor[$key + 1])) {
-                        if ($floor[$key + 1]['show_type'] == 'banner') {
-                            $bannerData = [];
-                            foreach ($floor[$key + 1]['data'] as $item) {
-                                if ($item['type'] == 1) {
-                                    $postInfo = $this->getPostInfo($item['link_content_id'], 1);
-                                    if (!$postInfo || !$postInfo['type']) {
-                                        Log::info('banner类型为内容,未找到内容,被丢弃' . json_encode($item));
-                                        continue;
-                                    }
-                                    $bannerData[] = array_merge($item, ['post_type' => $postInfo['type']]);
-                                } else {
-                                    $bannerData[] = $item;
-                                }
-                            }
-                            $newData[] = [
-                                'show_type' => 'banner',
-                                'data' => $bannerData,
-                            ];
-                        } elseif ($floor[$key + 1]['show_type'] == 'user') {
-                            $userData = [];
-                            foreach ($floor[$key + 1]['data'] as $item) {
-                                $followStatus = $uid ? $this->getFollowStatus($userInfo['uid'], $item['uid']) : 0;
-                                $userData[] = array_merge($item, ['follow_status' => $followStatus]);
-                            }
-                            if ($userData) {
-                                $newData[] = [
-                                    'show_type' => 'user',
-                                    'data' => $userData,
-                                ];
-                            }
-                        } elseif ($floor[$key + 1]['show_type'] == 'video') {
-                            $newData[] = [
-                                'show_type' => 'video',
-                                'data' => $floor[$key + 1]['data'],
-                            ];
-                        } elseif ($floor[$key + 1]['show_type'] == 'topic') {
-                            $newData[] = [
-                                'show_type' => 'topic',
-                                'data' => $floor[$key + 1]['data'],
-                            ];
-                        }
-                    }
-                    $newData[] = $val;
-                }
-                $data['data'] = $newData;
-            }
-        }
-        return jsonSuccess($data);
-    }
-
-    /**
-     * 内容详情
-     */
-    public function detail(Request $request)
-    {
-        Log::debug("内容详情-参数".json_encode($request));
-        $validator = Validator::make($request->all(), [
-            'id' => 'required|integer',
-        ]);
-        if ($validator->fails()) {
-            return jsonError($validator->errors()->first());
-        }
-        $userInfo = $this->getUserInfo();
-        if ($userInfo) {
-            $uid = $userInfo['uid'];
-            $inviteCode = $userInfo['invite_code'];
-        }else{
-            $uid = 0;
-            $inviteCode = '';
-        }
-        $detail = $this->postRepositories->detail($request['id']);
-        if (!$detail) {
-            return jsonError('内容飞走了');
-        }
-        $fractal = new Manager();
-        $res = new Item($detail, new DetailTransformer($uid, $inviteCode));
-        $data = $fractal->createData($res)->toArray();
-
-        return jsonSuccess($data);
-    }
-
-    /**
-     * 评论列表
-     */
-    public function commentList(Request $request)
-    {
-        $validator = Validator::make($request->all(), [
-            'post_id' => 'required|integer',
-        ]);
-        if ($validator->fails()) {
-            return jsonError($validator->errors()->first());
-        }
-        $exists = $this->postRepositories->detailExists($request['post_id']);
-        if (!$exists) {
-            return jsonError('内容飞走了');
-        }
-        $userInfo = $this->getUserInfo();
-        if ($userInfo) {
-            $uid = $userInfo['uid'];
-        }else{
-            $uid = 0;
-        }
-        $list = $this->postRepositories->commentList($request->all());
-        $fractal = new Manager();
-        $resource = new Collection($list, new CommentTransformer($request['post_id'], $uid));
-        $resource->setPaginator(new IlluminatePaginatorAdapter($list));
-        $data = $fractal->createData($resource)->toArray();
-
-        $commentCount = $this->postRepositories->getCommentCount($request['post_id']);
-        return jsonSuccess($data, '成功', ['comment_count' => $commentCount]);
-    }
-
-    /**
-     * 回复列表
-     */
-    public function replyList(Request $request)
-    {
-        $validator = Validator::make($request->all(), [
-            'id' => 'required|exists:post_comment',
-        ]);
-        if ($validator->fails()) {
-            return jsonError($validator->errors()->first());
-        }
-
-        $userInfo = $this->getUserInfo();
-        if ($userInfo) {
-            $uid = $userInfo['uid'];
-        }else{
-            $uid = 0;
-        }
-        $postId = $this->postRepositories->getPostId($request['id']);
-        $list = $this->postRepositories->replyList($request->all());
-        $fractal = new Manager();
-        $resource = new Collection($list, new ReplyTransformer($postId, $uid));
-        $resource->setPaginator(new IlluminatePaginatorAdapter($list));
-        $data = $fractal->createData($resource)->toArray();
-
-        return jsonSuccess($data);
-    }
-
-    /**
-     * 话题列表
-     */
-    public function topicList(Request $request)
-    {
-        $fractal = new Manager();
-        $param = $request->all();
-        if(isset($param['category_id']) && $param['category_id'] == -1){
-            $list = $this->postRepositories->myTopicList($request->all());
-            $resource = new Collection($list, new MyTopicListTransformer());
-            $resource->setPaginator(new IlluminatePaginatorAdapter($list));
-        }else{
-            $list = $this->postRepositories->topicList($request->all());
-            $resource = new Collection($list, new TopicListTransformer());
-            $resource->setPaginator(new IlluminatePaginatorAdapter($list));
-        }
-        $data = $fractal->createData($resource)->toArray();
-
-        return jsonSuccess($data);
-    }
-
-
-    /**
-     * 话题详情
-     */
-    public function topicDetail(Request $request)
-    {
-        $validator = Validator::make($request->all(), [
-            'id' => 'required|integer',
-        ]);
-        if ($validator->fails()) {
-            return jsonError($validator->errors()->first());
-        }
-        $userInfo = $this->getUserInfo();
-        if ($userInfo) {
-            $uid = $userInfo['uid'];
-        }else{
-            $uid = 0;
-        }
-        $detail = $this->postRepositories->topicDetail($request['id']);
-        if (!$detail) {
-            return jsonError('获取话题信息失败');
-        }
-        $fractal = new Manager();
-        $res = new Item($detail, new TopicDetailTransformer($uid));
-        $data = $fractal->createData($res)->toArray();
-
-        return jsonSuccess($data);
-    }
-
-    /**
-     * 话题内容列表
-     */
-    public function topicPost(Request $request)
-    {
-        $userInfo = $this->getUserInfo();
-        if ($userInfo) {
-            $uid = $userInfo['uid'];
-            $inviteCode = $userInfo['invite_code'];
-        }else{
-            $uid = 0;
-            $inviteCode = '';
-        }
-        $list = $this->postRepositories->topicPost($request->all());
-        $fractal = new Manager();
-        $resource = new Collection($list, new TopicPostTransformer($uid, $inviteCode));
-        $resource->setPaginator(new IlluminatePaginatorAdapter($list));
-        $data = $fractal->createData($resource)->toArray();
-
-        return jsonSuccess($data);
-    }
-
-    /**
-     * 获取话题
-     */
-    public function getTopic(Request $request)
-    {
-        $validator = Validator::make($request->all(), [
-            'ids' => 'required|string',
-        ]);
-        if ($validator->fails()) {
-            return jsonError($validator->errors()->first());
-        }
-
-        $data = $this->postRepositories->getTopic($request['ids']);
-        return jsonSuccess($data);
-    }
-
-    /**
-     * 获取内容视频组
-     */
-    public function getPostVideo(Request $request)
-    {
-        $validator = Validator::make($request->all(), [
-            'ids' => 'required|string',
-        ]);
-        if ($validator->fails()) {
-            return jsonError($validator->errors()->first());
-        }
-
-        $data = $this->postRepositories->getPostVideo($request['ids']);
-        return jsonSuccess($data);
-    }
-
-    //用户内容数统计
-    public function memberPostStatistics(Request $request)
-    {
-        $validator = Validator::make($request->all(), [
-            'uid' => 'required|int',
-        ]);
-        if ($validator->fails()) {
-            return jsonError($validator->errors()->first());
-        }
-        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());
-    }
-
-    /**
-     * 查询帖子内容详情(内部接口使用)
-     */
-    public function find(Request $request)
-    {
-        $validator = Validator::make($request->all(), [
-            'id' => 'required|integer',
-        ]);
-        if ($validator->fails()) {
-            return jsonError($validator->errors()->first());
-        }
-        $detail = $this->postRepositories->detail($request['id']);
-        if (!$detail) {
-            return jsonError('获取内容信息失败');
-        }
-        $fractal = new Manager();
-        $res = new Item($detail, new PostTransformer());
-        $data = $fractal->createData($res)->toArray();
-        return jsonSuccess($data);
-    }
-
-    /**
-     * 图片验证
-     */
-    public function checkImage(Request $request)
-    {
-        $validator = Validator::make($request->all(), [
-            'image' => 'required|url',
-        ]);
-        if ($validator->fails()) {
-            return jsonError($validator->errors()->first());
-        }
-        $res = $this->postRepositories->checkImage($request['image']);
-       
-        return jsonSuccess($res);
-    }
-
-    /**
-     * 下载量
-     */
-    public function downloadCount()
-    {
-        Redis::INCRBY('app_h5_download_count', 1);
-        return jsonSuccess();
-    }
 
 }

+ 21 - 1
app/Repositories/Circle/CircleRepository.php

@@ -10,6 +10,7 @@
 namespace App\Repositories\Circle;
 
 use App\Models\InterestCircle;
+use App\Models\InterestCircleUser;
 use Illuminate\Database\QueryException;
 use Dingo\Api\Http\Response;
 use Illuminate\Support\Carbon;
@@ -20,9 +21,11 @@ use Illuminate\Support\Facades\Redis;
 class CircleRepository
 {
 
-    public function __construct(InterestCircle $interestCircle)
+    public function __construct(InterestCircle $interestCircle,
+                                InterestCircleUser $interestCircleUser)
     {
         $this->interestCircle = $interestCircle;
+        $this->interestCircleUser = $interestCircleUser;
     }
 
     /**
@@ -103,4 +106,21 @@ class CircleRepository
             ]);
         }
     }
+
+    /**
+     * 圈子用户列表
+     */
+    public function memberLists($request)
+    {
+        $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
+        $where[] = ['is_black', 0];
+        $where[] = ['circle_id', $request['circle_id']];
+
+        $userModel = $this->interestCircleUser;
+
+        return $userModel
+            ->where($where)
+            ->orderBy('created_at', 'desc')
+            ->paginate($perPage);
+    }
 }

+ 77 - 0
app/Transformers/Circle/DetailTransformer.php

@@ -0,0 +1,77 @@
+<?php
+
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/6/6
+ * Time: 14:08
+ */
+
+namespace App\Transformers\Circle;
+
+use App\Models\InterestCircle;
+use App\Models\InterestCircleUser;
+use Illuminate\Support\Facades\Redis;
+use League\Fractal\TransformerAbstract;
+
+class DetailTransformer extends TransformerAbstract
+{
+
+    public function __construct($uid)
+    {
+        $this->uid = $uid;
+    }
+
+    public function transform(InterestCircle $interestCircle)
+    {
+        return [
+            'id' => $interestCircle['id'],
+            'name' => $interestCircle['name'],
+            'notice' => $interestCircle['notice'],
+            'image' => $interestCircle['image'],
+            'functions' => $this->getFunction($interestCircle),
+            'join_limit' => $interestCircle['join_limit'],
+            'limit_condition' => $interestCircle['limit_condition'],
+            'join_question' => (json_decode($interestCircle['join_question'], true)) ?? [],
+            'is_join' => $this->isJoin($this->uid, $interestCircle['id']),
+            'answer_error_count' => intval(Redis::get('circle_error_count_'.$this->uid))
+        ];
+    }
+
+    //判读当前用户是否加入圈子
+    public function isJoin($uid, $circleId)
+    {
+        $info = InterestCircleUser::where([['circle_id', $circleId], ['uid', $uid]])->first();
+        return $info ? 1 : 0;
+    }
+
+    /**
+     * 查询圈子开放功能
+     * @param InterestCircle $interestCircle
+     * @return array
+     */
+    private function getFunction(InterestCircle $interestCircle)
+    {
+        $functions = json_decode($interestCircle['contains_function'], true);
+        $info[] = ['name' => 'members', 'is_open' => 1, 'extra' => (string)$interestCircle['join_count']];
+        foreach ($functions as &$func) {
+            if ('pictures' == $func['function']) {
+                if (1 == $func['is_open']) {
+                    $info[] = ['name' => 'pictures', 'is_open' => 1, 'extra' => (string)$interestCircle['picture_count']];
+                } else {
+                    $info[] = ['name' => 'pictures', 'is_open' => 0, 'extra' => '0'];
+                }
+
+            }
+            if ('chatroom' == $func['function']) {
+                if (1 == $func['is_open']) {
+                    $info[] = ['name' => 'chatroom', 'is_open' => 1, 'extra' => (string)$interestCircle['relate_id']];
+                } else {
+                    $info[] = ['name' => 'chatroom', 'is_open' => 0, 'extra' => '0'];
+                }
+
+            }
+        }
+        return $info;
+    }
+}

+ 3 - 0
routes/api.php

@@ -71,6 +71,9 @@ $api->version('v1', [
     //图片验证
     $api->get('post/checkImage', 'PostController@checkImage');
 
+    //圈子相关
+    //圈子首页
+    $api->get('circle', 'CircleController@index');
     //登录+验签
     $api->group(['middleware' => ['chxq_jwt_auth','chxq_sign']], function ($api) {
         //发布内容