CircleMessageRepository.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. * @param $id
  45. * @return mixed
  46. */
  47. public function detail($id)
  48. {
  49. return $this->interestCircleMessage->find($id);
  50. }
  51. /**
  52. * 提问列表
  53. */
  54. public function lists($request)
  55. {
  56. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  57. $where[] = ['circle_id', $request['id']];
  58. return $this->interestCircleMessage
  59. ->where($where)
  60. ->with('imgs')
  61. ->orderBy('is_recommend', 'desc')
  62. ->orderBy('id', 'desc')
  63. ->paginate($perPage);
  64. }
  65. /**
  66. * 发布提问
  67. */
  68. public function create($request)
  69. {
  70. //验证小号
  71. $userInfo = $this->getUserInfo();
  72. // $userInfo['sns_status']=1;
  73. // $userInfo['uid']=268;
  74. if (empty($userInfo)) {
  75. return jsonError('获取用户信息失败');
  76. }
  77. if (!$userInfo['sns_status']) {
  78. return jsonError('您已被禁言');
  79. }
  80. $isBlack = $this->interestCircleUser->where('circle_id', $request['circle_id'])
  81. ->where('uid', $userInfo['uid'])
  82. ->where('is_black', 1)->exists();
  83. if ($isBlack) {
  84. return jsonError('当前状态无法提问,请联系管理员');
  85. }
  86. $oneHourTime = Carbon::now()->addHours(-1)->toDateTimeString();
  87. $oneHourPostCount = $this->interestCircleMessage->where('uid', $userInfo['uid'])->where('created_at', '>', $oneHourTime)->count();
  88. if ($oneHourPostCount > 5) {
  89. return jsonError('创作欲望太强啦,休息一下,看看其他用户的提问吧!');
  90. }
  91. $detectionText = strip_tags($request['content']);
  92. $detectionTextResult = $this->detectionService->checkText($detectionText);
  93. if ($detectionTextResult['code'] < 0) {
  94. return jsonError('内容违规,请修正哦');
  95. }
  96. $imgs = [];
  97. if (isset($request['imgs']) && $request['imgs']) {
  98. $imgs = json_decode($request['imgs'], true);
  99. $imgCount = count($imgs);
  100. if ($imgCount > 3) {
  101. return jsonError('最多上传3张图片');
  102. }
  103. }
  104. if ($imgs) {
  105. $allImg = $imgs;
  106. foreach ($allImg as &$img) {
  107. $img = $img . '&x-oss-process=image/resize,p_50/quality,Q_50';
  108. }
  109. $detectionImageResult = $this->detectionService->checkImg($allImg);
  110. if ($detectionImageResult['code'] < 0) {
  111. Log::debug('图片违规,请修正哦' . json_encode($detectionImageResult));
  112. return jsonError('图片违规,请修正哦');
  113. }
  114. }
  115. $fresh = (Carbon::now()->timestamp) - (Carbon::parse("2019-10-08 00:00:00")->timestamp);
  116. $score = ($fresh / 43200) * 14;
  117. $data = [
  118. 'circle_id' => $request['circle_id'],
  119. 'uid' => $userInfo['uid'],
  120. 'content' => $request['content'],
  121. 'good' => 0,
  122. 'bad' => 0,
  123. 'comment_count' => 0,
  124. 'weight' => $score
  125. ];
  126. $date = date('Y-m-d H:i:s');
  127. DB::beginTransaction();
  128. try {
  129. $message = $this->interestCircleMessage->create($data);
  130. if ($imgs) {
  131. $imgData = [];
  132. foreach ($imgs as $img) {
  133. $imgData[] = [
  134. 'msg_id' => $message->id,
  135. 'circle_id' => $message->circle_id,
  136. 'image' => $img,
  137. 'created_at' => $date,
  138. 'updated_at' => $date
  139. ];
  140. }
  141. $this->interestCircleMessageImg->insert($imgData);
  142. }
  143. $this->interestCircle->where('id', $request['circle_id'])->increment('message_count');
  144. DB::commit();
  145. Log::info('message_create:' . $message->id . ',post_author:' . $message->uid . ',author_ip:' . getClientIp());
  146. return jsonSuccess();
  147. } catch (QueryException $exception) {
  148. DB::rollBack();
  149. Log::debug('发布提问失败:' . $exception->getMessage());
  150. return jsonError('发布提问失败,请重试');
  151. }
  152. }
  153. /**
  154. * 评论&回复
  155. * @param $request
  156. * @return array
  157. */
  158. public function createComment($request)
  159. {
  160. $userInfo = $this->getUserInfo();
  161. // $userInfo['sns_status']=1;
  162. // $userInfo['uid']=268;
  163. // $userInfo['username']='测试用户';
  164. // $userInfo['avatar']='';
  165. if (empty($userInfo)) {
  166. return jsonError('获取用户信息失败');
  167. }
  168. if (!$userInfo['sns_status']) {
  169. return jsonError('您已被禁言');
  170. }
  171. $isBlack = $this->interestCircleUser->where('circle_id', $request['circle_id'])
  172. ->where('uid', $userInfo['uid'])
  173. ->where('is_black', 1)->exists();
  174. if ($isBlack) {
  175. return jsonError('当前状态无法提问,请联系管理员');
  176. }
  177. $oneHourTime = Carbon::now()->addHours(-1)->toDateTimeString();
  178. $oneHourCommentCount = $this->interestCircleMessageComment->where('uid', $userInfo['uid'])->where('created_at', '>', $oneHourTime)->count();
  179. if ($oneHourCommentCount > 59) {
  180. return jsonError('回复了这么多,休息休息,喝口水吧!');
  181. }
  182. $detectionTextResult = $this->detectionService->checkText($request['content']);
  183. if ($detectionTextResult['code'] < 0) {
  184. return jsonError('内容违规,请修正哦');
  185. }
  186. $post = $this->interestCircleMessage->find($request['msg_id']);
  187. if (!$post) {
  188. return jsonError('获取提问信息失败');
  189. }
  190. $data = [
  191. 'uid' => $userInfo['uid'],
  192. 'msg_id' => $request['msg_id'],
  193. 'parent_id' => 0,
  194. 'username' => $userInfo['username'],
  195. 'reply_uid' => 0,
  196. 'reply_username' => '',
  197. 'avatar' => $userInfo['avatar'] ?? '',
  198. 'content' => $request['content'],
  199. 'is_delete' => 0,
  200. ];
  201. if (isset($request['parent_id']) && $request['parent_id'] != 0) {
  202. $comment = $this->interestCircleMessageComment->find($request['parent_id']);
  203. if (!$comment || $comment->msg_id != $post->id) {
  204. return jsonError('获取评论信息失败');
  205. }
  206. if ($comment->parent_id) {
  207. return jsonError('只能回复评论');
  208. }
  209. if ($comment->is_delete) {
  210. return jsonError('不能回复已删除评论');
  211. }
  212. $data['parent_id'] = $request['parent_id'];
  213. if (isset($request['reply_uid']) && isset($request['reply_username'])) {
  214. $data['reply_uid'] = $request['reply_uid'];
  215. $data['reply_username'] = $request['reply_username'];
  216. } else {
  217. $data['reply_uid'] = 0;
  218. $data['reply_username'] = '';
  219. }
  220. }
  221. DB::beginTransaction();
  222. try {
  223. $newComment = $this->interestCircleMessageComment->create($data);
  224. if ($newComment->parent_id) {
  225. $this->interestCircleMessageComment->where('id', $newComment->parent_id)->increment('reply_count');
  226. }
  227. DB::commit();
  228. if ($newComment->parent_id) {
  229. Redis::DEL('circle_message_new_reply_' . $newComment->parent_id);
  230. } else {
  231. Redis::DEL('circle_message_new_comment_' . $newComment->post_id);
  232. }
  233. return jsonSuccess(['id' => $newComment->id], '评论成功');
  234. } catch (QueryException $exception) {
  235. DB::rollBack();
  236. Log::debug('评论内容失败:' . $exception->getMessage());
  237. return jsonError('评论内容失败,请重试');
  238. }
  239. }
  240. /**
  241. * 评论列表
  242. */
  243. public function commentList($request)
  244. {
  245. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  246. return $this->interestCircleMessageComment
  247. ->where('msg_id', $request['msg_id'])
  248. ->where('parent_id', 0)
  249. ->orderBy('id', 'desc')
  250. ->paginate($perPage);
  251. }
  252. /**
  253. * 提问评论数
  254. */
  255. public function getCommentCount($id)
  256. {
  257. $commentCount = 0;
  258. $post = $this->interestCircleMessage->find($id);
  259. if ($post) {
  260. $commentCount = $this->interestCircleMessageComment->where('msg_id', $id)->count();
  261. }
  262. return $commentCount;
  263. }
  264. }