xielin 5 years ago
parent
commit
9f34bcf1c8

+ 52 - 0
app/Http/Controllers/V1/CircleController.php

@@ -18,6 +18,7 @@ use App\Transformers\Circle\CircleMemberTransformer;
 use App\Transformers\Circle\CommentTransformer;
 use App\Transformers\Circle\DetailTransformer;
 use App\Transformers\Circle\MessageListTransformer;
+use App\Transformers\Circle\PictureListTransformer;
 use App\Transformers\Circle\QuestionTransformer;
 use App\Transformers\Circle\ReplyTransformer;
 use Illuminate\Http\Request;
@@ -392,4 +393,55 @@ class CircleController extends Controller
         return  $this->circleMessageRepository->commentDelete($request->all());
     }
 
+    /**
+     * 上传相册
+     */
+    public function CreatePictures(Request $request)
+    {
+        $validator = Validator::make($request->all(), [
+            'circle_id' => 'required|integer',
+            'imgs' => 'required|string',
+        ]);
+        if ($validator->fails()) {
+            return jsonError($validator->errors()->first());
+        }
+        return $this->circleMessageRepository->createPictures($request->all());
+    }
+
+    /**
+     * 相册列表
+     * @param Request $request
+     * @return array
+     */
+    public function pictureList(Request $request){
+        $validator = Validator::make($request->all(), [
+            'circle_id' => 'required|integer',
+        ]);
+        if ($validator->fails()) {
+            return jsonError($validator->errors()->first());
+        }
+        $list = $this->circleMessageRepository->pictureLists($request->all());
+        $fractal = new Manager();
+        $resource = new Collection($list, new PictureListTransformer());
+        $resource->setPaginator(new IlluminatePaginatorAdapter($list));
+        $data = $fractal->createData($resource)->toArray();
+
+        return jsonSuccess($data);
+    }
+
+    /**
+     * 删除相册
+     * @param Request $request
+     * @return array
+     */
+    public function deletePicture(Request $request){
+        $validator = Validator::make($request->all(), [
+            'id' => 'required|integer',
+        ]);
+        if ($validator->fails()) {
+            return jsonError($validator->errors()->first());
+        }
+        return $this->circleMessageRepository->deletePicture($request->all());
+    }
+
 }

+ 170 - 16
app/Repositories/Circle/CircleMessageRepository.php

@@ -15,6 +15,7 @@ use App\Models\InterestCircleMessage;
 use App\Models\InterestCircleMessageComment;
 use App\Models\InterestCircleMessageImg;
 use App\Models\InterestCircleMessageRecord;
+use App\Models\InterestCirclePicture;
 use App\Models\InterestCircleUser;
 use App\Models\Post;
 use App\Service\DetectionService;
@@ -33,6 +34,7 @@ class CircleMessageRepository
 
     public function __construct(InterestCircle $interestCircle,
                                 InterestCircleMessage $interestCircleMessage,
+                                InterestCirclePicture $interestCirclePicture,
                                 InterestCircleMessageImg $interestCircleMessageImg,
                                 InterestCircleUser $interestCircleUser,
                                 DetectionService $detectionService,
@@ -41,6 +43,7 @@ class CircleMessageRepository
     {
         $this->interestCircle = $interestCircle;
         $this->interestCircleMessage = $interestCircleMessage;
+        $this->interestCirclePicture = $interestCirclePicture;
         $this->interestCircleMessageImg = $interestCircleMessageImg;
         $this->interestCircleUser = $interestCircleUser;
         $this->interestCircleMessageComment = $interestCircleMessageComment;
@@ -89,17 +92,22 @@ class CircleMessageRepository
         if (!$userInfo['sns_status']) {
             return jsonError('您已被禁言');
         }
-        $isBlack = $this->interestCircleUser->where('circle_id', $request['circle_id'])
+        $circleUser = $this->interestCircleUser
+            ->where('circle_id', $request['circle_id'])
             ->where('uid', $userInfo['uid'])
-            ->where('is_black', 1)->exists();
-        if ($isBlack) {
-            return jsonError('当前状态无法提问,请联系管理员');
+            ->first();
+        if ($circleUser) {
+            if ($circleUser->is_black) {
+                return jsonError('您在本圈子内的权限受限');
+            }
+        } else {
+            return jsonError('抱歉,加入圈子才能互动');
         }
 
         $oneHourTime = Carbon::now()->addHours(-1)->toDateTimeString();
         $oneHourPostCount = $this->interestCircleMessage->where('uid', $userInfo['uid'])->where('created_at', '>', $oneHourTime)->count();
         if ($oneHourPostCount > 5) {
-            return jsonError('创作欲望太强啦,休息一下,看看其他用户的提问吧!');
+            return jsonError('创作欲望太强啦,休息一下,看看其他用户的相册吧!');
         }
 
         $detectionText = strip_tags($request['content']);
@@ -112,15 +120,15 @@ class CircleMessageRepository
             $imgs = json_decode($request['imgs'], true);
             $imgCount = count($imgs);
             if ($imgCount > 3) {
-                return jsonError('最多上传3张图片');
+                return jsonError('最多上传3张');
             }
         }
         if ($imgs) {
             $allImg = $imgs;
             foreach ($allImg as &$img) {
-                if(Str::contains($img,'?')){
+                if (Str::contains($img, '?')) {
                     $img = $img . '&x-oss-process=image/resize,p_50/quality,Q_50';
-                }else{
+                } else {
                     $img = $img . '?x-oss-process=image/resize,p_50/quality,Q_50';
                 }
 
@@ -190,11 +198,16 @@ class CircleMessageRepository
         if (!$userInfo['sns_status']) {
             return jsonError('您已被禁言');
         }
-        $isBlack = $this->interestCircleUser->where('circle_id', $request['circle_id'])
+        $circleUser = $this->interestCircleUser
+            ->where('circle_id', $request['circle_id'])
             ->where('uid', $userInfo['uid'])
-            ->where('is_black', 1)->exists();
-        if ($isBlack) {
-            return jsonError('当前状态无法提问,请联系管理员');
+            ->first();
+        if ($circleUser) {
+            if ($circleUser->is_black) {
+                return jsonError('您在本圈子内的权限受限');
+            }
+        } else {
+            return jsonError('抱歉,加入圈子才能互动');
         }
         $oneHourTime = Carbon::now()->addHours(-1)->toDateTimeString();
         $oneHourCommentCount = $this->interestCircleMessageComment->where('uid', $userInfo['uid'])->where('created_at', '>', $oneHourTime)->count();
@@ -315,13 +328,28 @@ class CircleMessageRepository
     public function messageAction($request)
     {
         $userInfo = $this->getUserInfo();
-        $userInfo['sns_status'] = 1;
-        $userInfo['uid'] = 268;
-        $userInfo['username'] = '测试用户';
-        $userInfo['avatar'] = '';
+//        $userInfo['sns_status'] = 1;
+//        $userInfo['uid'] = 268;
+//        $userInfo['username'] = '测试用户';
+//        $userInfo['avatar'] = '';
         if (empty($userInfo)) {
             return jsonError('获取用户信息失败');
         }
+        $messageInfo = $this->interestCircleMessage->find($request['msg_id']);
+        if (empty($messageInfo)) {
+            return jsonError('该提问不存在');
+        }
+        $circleUser = $this->interestCircleUser
+            ->where('circle_id', $messageInfo->circle_id)
+            ->where('uid', $userInfo['uid'])
+            ->first();
+        if ($circleUser) {
+            if ($circleUser->is_black) {
+                return jsonError('您在本圈子内的权限受限');
+            }
+        } else {
+            return jsonError('抱歉,加入圈子才能互动');
+        }
         $actionRow = InterestCircleMessageRecord::where([['msg_id', $request['msg_id']], ['uid', $userInfo['uid']]])->first();
         if ($actionRow) {
             return jsonSuccess();
@@ -424,4 +452,130 @@ class CircleMessageRepository
             return jsonError('删除提问失败,请重试');
         }
     }
+
+    /**
+     * 创建相册
+     */
+    public function createPictures($request)
+    {
+        //验证小号
+        $userInfo = $this->getUserInfo();
+//        $userInfo['sns_status']=1;
+//        $userInfo['uid']=268;
+        if (empty($userInfo)) {
+            return jsonError('获取用户信息失败');
+        }
+        if (!$userInfo['sns_status']) {
+            return jsonError('您已被禁言');
+        }
+        $circleUser = $this->interestCircleUser
+            ->where('circle_id', $request['circle_id'])
+            ->where('uid', $userInfo['uid'])
+            ->first();
+        if ($circleUser) {
+            if ($circleUser->is_black) {
+                return jsonError('您在本圈子内的权限受限');
+            }
+        } else {
+            return jsonError('抱歉,加入圈子才能互动');
+        }
+
+        $oneHourTime = Carbon::now()->addHours(-1)->toDateTimeString();
+        $oneHourPostCount = $this->interestCirclePicture->where('uid', $userInfo['uid'])->where('created_at', '>', $oneHourTime)->count();
+        if ($oneHourPostCount > 5) {
+            return jsonError('创作欲望太强啦,休息一下,看看其他用户的提问吧!');
+        }
+
+        $imgs = json_decode($request['imgs'], true);
+        $imgCount = count($imgs);
+        if ($imgCount == 0 || $imgCount > 9) {
+            return jsonError('所传图集必须1-9个');
+        }
+        $allImg = $imgs;
+        foreach ($allImg as &$img) {
+            if (Str::contains($img, '?')) {
+                $img = $img . '&x-oss-process=image/resize,p_50/quality,Q_50';
+            } else {
+                $img = $img . '?x-oss-process=image/resize,p_50/quality,Q_50';
+            }
+
+        }
+        $detectionImageResult = $this->detectionService->checkImg($allImg);
+        if ($detectionImageResult['code'] < 0) {
+            Log::debug('图片违规,请修正哦' . json_encode($detectionImageResult));
+            return jsonError('图片违规,请修正哦');
+        }
+
+        $date = date('Y-m-d H:i:s');
+        $patchNum = Str::uuid();
+        DB::beginTransaction();
+        try {
+            $imgData = [];
+            foreach ($imgs as $img) {
+                $imgData[] = [
+                    'circle_id' => $request['circle_id'],
+                    'uid' => $userInfo['uid'],
+                    'image' => $img,
+                    'patch_num' => $patchNum,
+                    'created_at' => $date,
+                    'updated_at' => $date
+                ];
+            }
+            $this->interestCirclePicture->insert($imgData);
+
+            $this->interestCircle->where('id', $request['circle_id'])->increment('picture_count', $imgCount);
+            DB::commit();
+            return jsonSuccess();
+        } catch (QueryException $exception) {
+            DB::rollBack();
+            Log::debug('上传相册失败:' . $exception->getMessage());
+            return jsonError('上传失败');
+        }
+    }
+
+    /**
+     * 相册列表
+     */
+    public function pictureLists($request)
+    {
+        $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
+        $where[] = ['circle_id', $request['circle_id']];
+
+        return $this->interestCirclePicture
+            ->where($where)
+            ->groupby('patch_num')
+            ->orderBy('id', 'desc')
+            ->paginate($perPage);
+    }
+
+    /**
+     * 删除相册图片
+     */
+    public function deletePicture($request)
+    {
+        $circle = $this->interestCirclePicture->where('id', $request['id'])->first();
+        if (!$circle) {
+            return jsonError('相册不存在');
+        }
+        $userInfo = $this->getUserInfo();
+//        $userInfo['sns_status']=1;
+//        $userInfo['uid']=268;
+        if (empty($userInfo)) {
+            return jsonError('获取用户信息失败');
+        }
+        if ($userInfo['uid'] != $circle->uid) {
+            return jsonError('只能删除自己的相册');
+        }
+        DB::beginTransaction();
+        try {
+            $circle->delete();
+            DB::commit();
+            return jsonSuccess();
+
+        } catch (QueryException $exception) {
+            DB::rollBack();
+            Log::debug('删除相册图片:' . $request['id'] . $exception->getMessage());
+            return jsonError('操作失败,请重试');
+        }
+    }
 }

+ 0 - 47
app/Repositories/Circle/CircleRepository.php

@@ -183,53 +183,6 @@ class CircleRepository
     }
 
 
-    /**
-     * 相册列表
-     */
-    public function pictureLists($request)
-    {
-        $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
-        $where = [];
-        if (isset($request['circle_id'])) {
-            $where[] = ['circle_id', $request['circle_id']];
-        }
-        if (isset($request['uid'])) {
-            $where[] = ['uid', $request['uid']];
-        }
-
-        return $this->interestCirclePicture
-            ->where($where)
-            ->orderBy('created_at', 'desc')
-            ->paginate($perPage);
-    }
-
-    /**
-     * 删除相册图片
-     */
-    public function deletePicture($request)
-    {
-        $circle = $this->interestCirclePicture->where('id', $request['id'])->first();
-        if (!$circle) {
-            return Response::create([
-                'message' => '获取图片信息失败',
-                'status_code' => 500
-            ]);
-        }
-        DB::beginTransaction();
-        try {
-            $circle->delete();
-            DB::commit();
-            return Response::create();
 
-        } catch (QueryException $exception) {
-            DB::rollBack();
-            Log::debug('删除相册图片:' . $request['id'] . $exception->getMessage());
-            return Response::create([
-                'message' => '操作失败,请重试',
-                'error' => $exception->getMessage(),
-                'status_code' => 500
-            ]);
-        }
-    }
 
 }

+ 32 - 0
app/Transformers/Circle/PictureListTransformer.php

@@ -0,0 +1,32 @@
+<?php
+
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/6/6
+ * Time: 14:08
+ */
+
+namespace App\Transformers\Circle;
+
+use App\Models\InterestCircle;
+use App\Models\InterestCirclePicture;
+use App\Traits\UserTrait;
+use Illuminate\Support\Carbon;
+use League\Fractal\TransformerAbstract;
+
+class PictureListTransformer extends TransformerAbstract
+{
+    use UserTrait;
+
+    public function transform(InterestCirclePicture $interestCirclePicture)
+    {
+        $user = $this->userInfo($interestCirclePicture['uid']);
+        return [
+            'id' => $interestCirclePicture['id'],
+            'user' => $user ? $user : new \stdClass(),
+            'image' => $interestCirclePicture['image'],
+            'created_at' => Carbon::parse($interestCirclePicture['created_at'])->toDateTimeString(),
+        ];
+    }
+}