123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019/6/5
- * Time: 16:03
- */
- namespace App\Repositories\Circle;
- use App\Models\InterestCircle;
- use App\Models\InterestCircleArticle;
- 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;
- use App\Traits\UserTrait;
- use Illuminate\Database\QueryException;
- use Dingo\Api\Http\Response;
- use Illuminate\Support\Carbon;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Redis;
- use Illuminate\Support\Str;
- class CircleMessageRepository
- {
- use UserTrait;
- public function __construct(InterestCircle $interestCircle,
- InterestCircleMessage $interestCircleMessage,
- InterestCirclePicture $interestCirclePicture,
- InterestCircleMessageImg $interestCircleMessageImg,
- InterestCircleUser $interestCircleUser,
- DetectionService $detectionService,
- InterestCircleMessageComment $interestCircleMessageComment
- )
- {
- $this->interestCircle = $interestCircle;
- $this->interestCircleMessage = $interestCircleMessage;
- $this->interestCirclePicture = $interestCirclePicture;
- $this->interestCircleMessageImg = $interestCircleMessageImg;
- $this->interestCircleUser = $interestCircleUser;
- $this->interestCircleMessageComment = $interestCircleMessageComment;
- $this->detectionService = $detectionService;
- }
- /**
- * 查询单个提问
- * @param $id
- * @return mixed
- */
- public function detail($id)
- {
- return $this->interestCircleMessage->find($id);
- }
- /**
- * 提问列表
- */
- public function lists($request)
- {
- $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
- $where[] = ['circle_id', $request['id']];
- return $this->interestCircleMessage
- ->where($where)
- ->with('imgs')
- ->orderBy('is_recommend', 'desc')
- ->orderBy('id', 'desc')
- ->paginate($perPage);
- }
- /**
- * 发布提问
- */
- public function create($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->interestCircleMessage->where('uid', $userInfo['uid'])->where('created_at', '>', $oneHourTime)->count();
- if ($oneHourPostCount > 50) {
- return jsonError('创作欲望太强啦,休息一下,看看其他用户的提问吧!');
- }
- $detectionText = strip_tags($request['content']);
- $detectionTextResult = $this->detectionService->checkText($detectionText);
- if ($detectionTextResult['code'] < 0) {
- return jsonError('内容违规,请修正哦');
- }
- $imgs = [];
- if (isset($request['imgs']) && $request['imgs']) {
- $imgs = json_decode($request['imgs'], true);
- $imgCount = count($imgs);
- if ($imgCount > 3) {
- return jsonError('最多上传3张');
- }
- }
- if ($imgs) {
- $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('图片违规,请修正哦');
- }
- }
- $fresh = (Carbon::now()->timestamp) - (Carbon::parse("2019-10-08 00:00:00")->timestamp);
- $score = ($fresh / 43200) * 14;
- $data = [
- 'circle_id' => $request['circle_id'],
- 'uid' => $userInfo['uid'],
- 'content' => $request['content'],
- 'good' => 0,
- 'bad' => 0,
- 'comment_count' => 0,
- 'weight' => $score
- ];
- $date = date('Y-m-d H:i:s');
- DB::beginTransaction();
- try {
- $message = $this->interestCircleMessage->create($data);
- if ($imgs) {
- $imgData = [];
- foreach ($imgs as $img) {
- $imgData[] = [
- 'msg_id' => $message->id,
- 'circle_id' => $message->circle_id,
- 'image' => $img,
- 'created_at' => $date,
- 'updated_at' => $date
- ];
- }
- $this->interestCircleMessageImg->insert($imgData);
- }
- $this->interestCircle->where('id', $request['circle_id'])->increment('message_count');
- DB::commit();
- Log::info('message_create:' . $message->id . ',post_author:' . $message->uid . ',author_ip:' . getClientIp());
- return jsonSuccess();
- } catch (QueryException $exception) {
- DB::rollBack();
- Log::debug('发布提问失败:' . $exception->getMessage());
- return jsonError('发布提问失败,请重试');
- }
- }
- /**
- * 评论&回复
- * @param $request
- * @return array
- */
- public function createComment($request)
- {
- Log::debug('comment-request:'.json_encode($request));
- $userInfo = $this->getUserInfo();
- // $userInfo['sns_status']=1;
- // $userInfo['uid']=268;
- // $userInfo['username']='测试用户';
- // $userInfo['avatar']='';
- 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();
- $oneHourCommentCount = $this->interestCircleMessageComment->where('uid', $userInfo['uid'])->where('created_at', '>', $oneHourTime)->count();
- if ($oneHourCommentCount > 59) {
- return jsonError('回复了这么多,休息休息,喝口水吧!');
- }
- $detectionTextResult = $this->detectionService->checkText($request['content']);
- if ($detectionTextResult['code'] < 0) {
- return jsonError('内容违规,请修正哦');
- }
- $post = $this->interestCircleMessage->find($request['msg_id']);
- if (!$post) {
- return jsonError('获取提问信息失败');
- }
- $data = [
- 'uid' => $userInfo['uid'],
- 'circle_id' => $request['circle_id'],
- 'msg_id' => $request['msg_id'],
- 'parent_id' => 0,
- 'username' => $userInfo['username'],
- 'reply_uid' => 0,
- 'reply_username' => '',
- 'avatar' => $userInfo['avatar'] ?? '',
- 'content' => $request['content'],
- 'is_delete' => 0,
- ];
- if (isset($request['parent_id']) && $request['parent_id'] != 0) {
- $comment = $this->interestCircleMessageComment->find($request['parent_id']);
- if (!$comment || $comment->msg_id != $post->id) {
- return jsonError('获取评论信息失败');
- }
- if ($comment->parent_id) {
- return jsonError('只能回复评论');
- }
- if ($comment->is_delete) {
- return jsonError('不能回复已删除评论');
- }
- $data['parent_id'] = $request['parent_id'];
- if (isset($request['reply_uid']) && isset($request['reply_username'])) {
- $data['reply_uid'] = $request['reply_uid'];
- $data['reply_username'] = $request['reply_username'];
- } else {
- $data['reply_uid'] = 0;
- $data['reply_username'] = '';
- }
- }
- DB::beginTransaction();
- try {
- $newComment = $this->interestCircleMessageComment->create($data);
- if ($newComment->parent_id) {
- $this->interestCircleMessageComment->where('id', $newComment->parent_id)->increment('reply_count');
- }
- $this->interestCircleMessage->where('id', $request['msg_id'])->increment('comment_count');
- DB::commit();
- if ($newComment->parent_id) {
- Redis::DEL('circle_message_new_reply_' . $newComment->parent_id);
- Log::debug('删除回复缓存'.$newComment->id);
- } else {
- Redis::DEL('circle_message_new_comment_' . $newComment->msg_id);
- Log::debug('删除评论缓存'.$newComment->msg_id);
- }
- $key = "community_calc_circle_score";
- Redis::sadd($key,$request['msg_id']);
- return jsonSuccess(['id' => $newComment->id], '评论成功');
- } catch (QueryException $exception) {
- DB::rollBack();
- Log::debug('评论内容失败:' . $exception->getMessage());
- return jsonError('评论内容失败,请重试');
- }
- }
- /**
- * 评论列表
- */
- public function commentList($request)
- {
- $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
- return $this->interestCircleMessageComment
- ->where('msg_id', $request['msg_id'])
- ->where('parent_id', 0)
- ->orderBy('id', 'desc')
- ->paginate($perPage);
- }
- /**
- * 提问评论数
- */
- public function getCommentCount($id)
- {
- $commentCount = 0;
- $post = $this->interestCircleMessage->find($id);
- if ($post) {
- $commentCount = $this->interestCircleMessageComment->where('msg_id', $id)->count();
- }
- return $commentCount;
- }
- /**
- * 回复列表
- */
- public function replyList($request)
- {
- $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
- return $this->interestCircleMessageComment
- ->where('parent_id', $request['comment_id'])
- ->orderBy('id', 'desc')
- ->paginate($perPage);
- }
- /**
- * 提问顶踩
- * @param $request
- * @return array
- */
- public function messageAction($request)
- {
- $userInfo = $this->getUserInfo();
- // $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();
- }
- DB::beginTransaction();
- try {
- $data['msg_id'] = $request['msg_id'];
- $data['uid'] = $userInfo['uid'];
- $data['action'] = $request['action'];
- InterestCircleMessageRecord::insert($data);
- if ($request['action'] == 1) {
- InterestCircleMessage::where('id', $request['msg_id'])->increment('good');
- } elseif ($request['action'] == -1) {
- InterestCircleMessage::where('id', $request['msg_id'])->increment('bad');
- }
- DB::commit();
- $key = "community_calc_circle_score";
- Redis::sadd($key,$request['msg_id']);
- return jsonSuccess();
- } catch (QueryException $exception) {
- DB::rollBack();
- Log::debug('顶踩提问失败:' . $exception->getMessage());
- return jsonError('操作失败,请重试');
- }
- }
- /**
- * 删除评论
- */
- public function commentDelete($request)
- {
- $userInfo = $this->getUserInfo();
- if (empty($userInfo)) {
- return jsonError('获取用户信息失败');
- }
- $comment = $this->interestCircleMessageComment->find($request['id']);
- if (!$comment) {
- return jsonError('获取评论信息失败');
- }
- if ($userInfo['uid'] != $comment->uid) {
- return jsonError('只能删除自己的评论');
- }
- if ($comment->is_delete == 1) {
- return jsonError('该评论已经删除');
- }
- DB::beginTransaction();
- try {
- $comment->is_delete = 1;
- $comment->save();
- DB::commit();
- if (!$comment->parent_id) {
- Redis::DEL('circle_message_new_comment_' . $comment->msg_id);
- } else {
- Redis::DEL('circle_message_new_reply_' . $comment->id);
- }
- Redis::SADD('delete_circle_message_comment_ids', $comment->id);
- return jsonSuccess();
- } catch (QueryException $exception) {
- DB::rollBack();
- Log::debug('删除评论:' . $request['id'] . $exception->getMessage());
- return jsonError('删除评论失败');
- }
- }
- /**
- * 删除提问
- */
- public function deleteMessage($request)
- {
- //验证用户信息
- $userInfo = $this->getUserInfo();
- if (empty($userInfo)) {
- return jsonError('获取用户信息失败');
- }
- $post = $this->interestCircleMessage->find($request['id']);
- if (!$post) {
- return jsonError('获取提问信息失败');
- }
- if ($post->uid != $userInfo['uid']) {
- return jsonError('只能删除自己发布的提问');
- }
- DB::beginTransaction();
- try {
- $post->delete();
- $this->interestCircle->where('id', $post->circle_id)->decrement('message_count');
- DB::commit();
- Redis::SADD('delete_circle_message_ids', $request['id']);
- Log::debug('删除提问成功:' . $request['id']);
- return jsonSuccess();
- } catch (QueryException $exception) {
- DB::rollBack();
- Log::debug('删除提问失败:' . $request['id'] . $exception->getMessage());
- 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('is_main', 1)
- ->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::random(32);
- DB::beginTransaction();
- try {
- $imgData = [];
- foreach ($imgs as $k => $img) {
- $isMain = 0;
- if ($k == 0) {
- $isMain = 1;
- }
- $imgData[] = [
- 'circle_id' => $request['circle_id'],
- 'uid' => $userInfo['uid'],
- 'image' => $img,
- 'patch_num' => $patchNum,
- 'is_main' => $isMain,
- 'created_at' => $date,
- 'updated_at' => $date
- ];
- }
- $this->interestCirclePicture->insert($imgData);
- $this->interestCircle->where('id', $request['circle_id'])->increment('picture_count', $imgCount);
- $key = 'circle_picture_' . $patchNum;
- Redis::set($key, json_encode($imgs));
- 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']];
- $where[] = ['is_main', 1];
- return $this->interestCirclePicture
- ->where($where)
- ->orderBy('id', 'desc')
- ->paginate($perPage);
- }
- /**
- * 删除相册图片
- */
- public function deletePicture($request)
- {
- $circle = $this->interestCirclePicture->where('patch_num', $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 {
- $picCount = $this->interestCirclePicture->where('patch_num', $request['id'])->count();
- $this->interestCirclePicture->where('patch_num', $request['id'])->delete();
- $this->interestCircle->where('circle_id', $circle->circle_id)->decrement('picture_count', $picCount);
- DB::commit();
- return jsonSuccess();
- } catch (QueryException $exception) {
- DB::rollBack();
- Log::debug('删除相册图片:' . $request['id'] . $exception->getMessage());
- return jsonError('操作失败,请重试');
- }
- }
- }
|