CircleMessageRepository.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/5
  6. * Time: 16:03
  7. */
  8. namespace App\Repositories\Circle;
  9. use App\Models\InterestCircle;
  10. use App\Models\InterestCircleArticle;
  11. use App\Models\InterestCircleMessage;
  12. use App\Models\InterestCircleMessageComment;
  13. use App\Models\InterestCircleMessageImg;
  14. use App\Models\InterestCircleUser;
  15. use App\Models\Post;
  16. use App\Service\DetectionService;
  17. use App\Traits\UserTrait;
  18. use Illuminate\Database\QueryException;
  19. use Dingo\Api\Http\Response;
  20. use Illuminate\Support\Carbon;
  21. use Illuminate\Support\Facades\DB;
  22. use Illuminate\Support\Facades\Log;
  23. use Illuminate\Support\Facades\Redis;
  24. class CircleMessageRepository
  25. {
  26. use UserTrait;
  27. public function __construct(InterestCircle $interestCircle,
  28. InterestCircleMessage $interestCircleMessage,
  29. InterestCircleMessageImg $interestCircleMessageImg,
  30. InterestCircleUser $interestCircleUser,
  31. DetectionService $detectionService,
  32. InterestCircleMessageComment $interestCircleMessageComment
  33. )
  34. {
  35. $this->interestCircle = $interestCircle;
  36. $this->interestCircleMessage = $interestCircleMessage;
  37. $this->interestCircleMessageImg = $interestCircleMessageImg;
  38. $this->interestCircleUser = $interestCircleUser;
  39. $this->interestCircleMessageComment = $interestCircleMessageComment;
  40. $this->detectionService = $detectionService;
  41. }
  42. /**
  43. * 提问列表
  44. */
  45. public function lists($request)
  46. {
  47. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  48. $where[] = ['circle_id', $request['id']];
  49. return $this->interestCircleMessage
  50. ->where($where)
  51. ->with('imgs')
  52. ->orderBy('is_recommend', 'desc')
  53. ->orderBy('id', 'desc')
  54. ->paginate($perPage);
  55. }
  56. /**
  57. * 发布提问
  58. */
  59. public function create($request)
  60. {
  61. //验证小号
  62. $userInfo = $this->getUserInfo();
  63. // $userInfo['sns_status']=1;
  64. // $userInfo['uid']=268;
  65. if (empty($userInfo)) {
  66. return jsonError('获取用户信息失败');
  67. }
  68. if (!$userInfo['sns_status']) {
  69. return jsonError('您已被禁言');
  70. }
  71. $isBlack = $this->interestCircleUser->where('circle_id', $request['circle_id'])
  72. ->where('uid', $userInfo['uid'])
  73. ->where('is_black', 1)->exists();
  74. if ($isBlack) {
  75. return jsonError('当前状态无法提问,请联系管理员');
  76. }
  77. $oneHourTime = Carbon::now()->addHours(-1)->toDateTimeString();
  78. $oneHourPostCount = $this->interestCircleMessage->where('uid', $userInfo['uid'])->where('created_at', '>', $oneHourTime)->count();
  79. if ($oneHourPostCount > 5) {
  80. return jsonError('创作欲望太强啦,休息一下,看看其他用户的提问吧!');
  81. }
  82. $detectionText = strip_tags($request['content']);
  83. $detectionTextResult = $this->detectionService->checkText($detectionText);
  84. if ($detectionTextResult['code'] < 0) {
  85. return jsonError('内容违规,请修正哦');
  86. }
  87. $imgs = [];
  88. if (isset($request['imgs']) && $request['imgs']) {
  89. $imgs = json_decode($request['imgs'], true);
  90. $imgCount = count($imgs);
  91. if ($imgCount > 3) {
  92. return jsonError('最多上传3张图片');
  93. }
  94. }
  95. if ($imgs) {
  96. $allImg = $imgs;
  97. foreach ($allImg as &$img) {
  98. $img = $img . '&x-oss-process=image/resize,p_50/quality,Q_50';
  99. }
  100. $detectionImageResult = $this->detectionService->checkImg($allImg);
  101. if ($detectionImageResult['code'] < 0) {
  102. Log::debug('图片违规,请修正哦' . json_encode($detectionImageResult));
  103. return jsonError('图片违规,请修正哦');
  104. }
  105. }
  106. $fresh = (Carbon::now()->timestamp) - (Carbon::parse("2019-10-08 00:00:00")->timestamp);
  107. $score = ($fresh / 43200) * 14;
  108. $data = [
  109. 'circle_id' => $request['circle_id'],
  110. 'uid' => $userInfo['uid'],
  111. 'content' => $request['content'],
  112. 'good' => 0,
  113. 'bad' => 0,
  114. 'comment_count' => 0,
  115. 'weight' => $score
  116. ];
  117. $date = date('Y-m-d H:i:s');
  118. DB::beginTransaction();
  119. try {
  120. $message = $this->interestCircleMessage->create($data);
  121. if ($imgs) {
  122. $imgData = [];
  123. foreach ($imgs as $img) {
  124. $imgData[] = [
  125. 'msg_id' => $message->id,
  126. 'circle_id' => $message->circle_id,
  127. 'image' => $img,
  128. 'created_at' => $date,
  129. 'updated_at' => $date
  130. ];
  131. }
  132. $this->interestCircleMessageImg->insert($imgData);
  133. }
  134. $this->interestCircle->where('id',$request['circle_id'])->increment('message_count');
  135. DB::commit();
  136. Log::info('message_create:' . $message->id . ',post_author:' . $message->uid . ',author_ip:' . getClientIp());
  137. return jsonSuccess();
  138. } catch (QueryException $exception) {
  139. DB::rollBack();
  140. Log::debug('发布提问失败:' . $exception->getMessage());
  141. return jsonError('发布提问失败,请重试');
  142. }
  143. }
  144. }