123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- <?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\InterestCircleUser;
- use App\Models\Post;
- use App\Service\DetectionService;
- 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;
- class CircleMessageRepository
- {
- public function __construct(InterestCircle $interestCircle,
- InterestCircleMessage $interestCircleMessage,
- InterestCircleMessageImg $interestCircleMessageImg,
- InterestCircleUser $interestCircleUser,
- DetectionService $detectionService,
- InterestCircleMessageComment $interestCircleMessageComment
- )
- {
- $this->interestCircle = $interestCircle;
- $this->interestCircleMessage = $interestCircleMessage;
- $this->interestCircleMessageImg = $interestCircleMessageImg;
- $this->interestCircleUser = $interestCircleUser;
- $this->interestCircleMessageComment = $interestCircleMessageComment;
- $this->detectionService = $detectionService;
- }
- /**
- * 提问列表
- */
- 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();
- if (empty($userInfo)) {
- return jsonError('获取用户信息失败');
- }
- if (!$userInfo['sns_status']) {
- return jsonError('您已被禁言');
- }
- $isBlack = $this->interestCircleUser->where('circle_id', $request['circle_id'])
- ->where('uid', $userInfo['uid'])
- ->where('is_black', 1)->exists();
- if ($isBlack) {
- 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('创作欲望太强啦,休息一下,看看其他用户的提问吧!');
- }
- $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) {
- $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('发布提问失败,请重试');
- }
- }
- }
|