CircleMessageRepository.php 5.4 KB

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