CircleMessageRepository.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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\InterestCircleMessageRecord;
  15. use App\Models\InterestCircleUser;
  16. use App\Models\Post;
  17. use App\Service\DetectionService;
  18. use App\Traits\UserTrait;
  19. use Illuminate\Database\QueryException;
  20. use Dingo\Api\Http\Response;
  21. use Illuminate\Support\Carbon;
  22. use Illuminate\Support\Facades\DB;
  23. use Illuminate\Support\Facades\Log;
  24. use Illuminate\Support\Facades\Redis;
  25. class CircleMessageRepository
  26. {
  27. use UserTrait;
  28. public function __construct(InterestCircle $interestCircle,
  29. InterestCircleMessage $interestCircleMessage,
  30. InterestCircleMessageImg $interestCircleMessageImg,
  31. InterestCircleUser $interestCircleUser,
  32. DetectionService $detectionService,
  33. InterestCircleMessageComment $interestCircleMessageComment
  34. )
  35. {
  36. $this->interestCircle = $interestCircle;
  37. $this->interestCircleMessage = $interestCircleMessage;
  38. $this->interestCircleMessageImg = $interestCircleMessageImg;
  39. $this->interestCircleUser = $interestCircleUser;
  40. $this->interestCircleMessageComment = $interestCircleMessageComment;
  41. $this->detectionService = $detectionService;
  42. }
  43. /**
  44. * 查询单个提问
  45. * @param $id
  46. * @return mixed
  47. */
  48. public function detail($id)
  49. {
  50. return $this->interestCircleMessage->find($id);
  51. }
  52. /**
  53. * 提问列表
  54. */
  55. public function lists($request)
  56. {
  57. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  58. $where[] = ['circle_id', $request['id']];
  59. return $this->interestCircleMessage
  60. ->where($where)
  61. ->with('imgs')
  62. ->orderBy('is_recommend', 'desc')
  63. ->orderBy('id', 'desc')
  64. ->paginate($perPage);
  65. }
  66. /**
  67. * 发布提问
  68. */
  69. public function create($request)
  70. {
  71. //验证小号
  72. $userInfo = $this->getUserInfo();
  73. // $userInfo['sns_status']=1;
  74. // $userInfo['uid']=268;
  75. if (empty($userInfo)) {
  76. return jsonError('获取用户信息失败');
  77. }
  78. if (!$userInfo['sns_status']) {
  79. return jsonError('您已被禁言');
  80. }
  81. $isBlack = $this->interestCircleUser->where('circle_id', $request['circle_id'])
  82. ->where('uid', $userInfo['uid'])
  83. ->where('is_black', 1)->exists();
  84. if ($isBlack) {
  85. return jsonError('当前状态无法提问,请联系管理员');
  86. }
  87. $oneHourTime = Carbon::now()->addHours(-1)->toDateTimeString();
  88. $oneHourPostCount = $this->interestCircleMessage->where('uid', $userInfo['uid'])->where('created_at', '>', $oneHourTime)->count();
  89. if ($oneHourPostCount > 5) {
  90. return jsonError('创作欲望太强啦,休息一下,看看其他用户的提问吧!');
  91. }
  92. $detectionText = strip_tags($request['content']);
  93. $detectionTextResult = $this->detectionService->checkText($detectionText);
  94. if ($detectionTextResult['code'] < 0) {
  95. return jsonError('内容违规,请修正哦');
  96. }
  97. $imgs = [];
  98. if (isset($request['imgs']) && $request['imgs']) {
  99. $imgs = json_decode($request['imgs'], true);
  100. $imgCount = count($imgs);
  101. if ($imgCount > 3) {
  102. return jsonError('最多上传3张图片');
  103. }
  104. }
  105. if ($imgs) {
  106. $allImg = $imgs;
  107. foreach ($allImg as &$img) {
  108. $img = $img . '&x-oss-process=image/resize,p_50/quality,Q_50';
  109. }
  110. $detectionImageResult = $this->detectionService->checkImg($allImg);
  111. if ($detectionImageResult['code'] < 0) {
  112. Log::debug('图片违规,请修正哦' . json_encode($detectionImageResult));
  113. return jsonError('图片违规,请修正哦');
  114. }
  115. }
  116. $fresh = (Carbon::now()->timestamp) - (Carbon::parse("2019-10-08 00:00:00")->timestamp);
  117. $score = ($fresh / 43200) * 14;
  118. $data = [
  119. 'circle_id' => $request['circle_id'],
  120. 'uid' => $userInfo['uid'],
  121. 'content' => $request['content'],
  122. 'good' => 0,
  123. 'bad' => 0,
  124. 'comment_count' => 0,
  125. 'weight' => $score
  126. ];
  127. $date = date('Y-m-d H:i:s');
  128. DB::beginTransaction();
  129. try {
  130. $message = $this->interestCircleMessage->create($data);
  131. if ($imgs) {
  132. $imgData = [];
  133. foreach ($imgs as $img) {
  134. $imgData[] = [
  135. 'msg_id' => $message->id,
  136. 'circle_id' => $message->circle_id,
  137. 'image' => $img,
  138. 'created_at' => $date,
  139. 'updated_at' => $date
  140. ];
  141. }
  142. $this->interestCircleMessageImg->insert($imgData);
  143. }
  144. $this->interestCircle->where('id', $request['circle_id'])->increment('message_count');
  145. DB::commit();
  146. Log::info('message_create:' . $message->id . ',post_author:' . $message->uid . ',author_ip:' . getClientIp());
  147. return jsonSuccess();
  148. } catch (QueryException $exception) {
  149. DB::rollBack();
  150. Log::debug('发布提问失败:' . $exception->getMessage());
  151. return jsonError('发布提问失败,请重试');
  152. }
  153. }
  154. /**
  155. * 评论&回复
  156. * @param $request
  157. * @return array
  158. */
  159. public function createComment($request)
  160. {
  161. $userInfo = $this->getUserInfo();
  162. // $userInfo['sns_status']=1;
  163. // $userInfo['uid']=268;
  164. // $userInfo['username']='测试用户';
  165. // $userInfo['avatar']='';
  166. if (empty($userInfo)) {
  167. return jsonError('获取用户信息失败');
  168. }
  169. if (!$userInfo['sns_status']) {
  170. return jsonError('您已被禁言');
  171. }
  172. $isBlack = $this->interestCircleUser->where('circle_id', $request['circle_id'])
  173. ->where('uid', $userInfo['uid'])
  174. ->where('is_black', 1)->exists();
  175. if ($isBlack) {
  176. return jsonError('当前状态无法提问,请联系管理员');
  177. }
  178. $oneHourTime = Carbon::now()->addHours(-1)->toDateTimeString();
  179. $oneHourCommentCount = $this->interestCircleMessageComment->where('uid', $userInfo['uid'])->where('created_at', '>', $oneHourTime)->count();
  180. if ($oneHourCommentCount > 59) {
  181. return jsonError('回复了这么多,休息休息,喝口水吧!');
  182. }
  183. $detectionTextResult = $this->detectionService->checkText($request['content']);
  184. if ($detectionTextResult['code'] < 0) {
  185. return jsonError('内容违规,请修正哦');
  186. }
  187. $post = $this->interestCircleMessage->find($request['msg_id']);
  188. if (!$post) {
  189. return jsonError('获取提问信息失败');
  190. }
  191. $data = [
  192. 'uid' => $userInfo['uid'],
  193. 'msg_id' => $request['msg_id'],
  194. 'parent_id' => 0,
  195. 'username' => $userInfo['username'],
  196. 'reply_uid' => 0,
  197. 'reply_username' => '',
  198. 'avatar' => $userInfo['avatar'] ?? '',
  199. 'content' => $request['content'],
  200. 'is_delete' => 0,
  201. ];
  202. if (isset($request['parent_id']) && $request['parent_id'] != 0) {
  203. $comment = $this->interestCircleMessageComment->find($request['parent_id']);
  204. if (!$comment || $comment->msg_id != $post->id) {
  205. return jsonError('获取评论信息失败');
  206. }
  207. if ($comment->parent_id) {
  208. return jsonError('只能回复评论');
  209. }
  210. if ($comment->is_delete) {
  211. return jsonError('不能回复已删除评论');
  212. }
  213. $data['parent_id'] = $request['parent_id'];
  214. if (isset($request['reply_uid']) && isset($request['reply_username'])) {
  215. $data['reply_uid'] = $request['reply_uid'];
  216. $data['reply_username'] = $request['reply_username'];
  217. } else {
  218. $data['reply_uid'] = 0;
  219. $data['reply_username'] = '';
  220. }
  221. }
  222. DB::beginTransaction();
  223. try {
  224. $newComment = $this->interestCircleMessageComment->create($data);
  225. if ($newComment->parent_id) {
  226. $this->interestCircleMessageComment->where('id', $newComment->parent_id)->increment('reply_count');
  227. }
  228. DB::commit();
  229. if ($newComment->parent_id) {
  230. Redis::DEL('circle_message_new_reply_' . $newComment->parent_id);
  231. } else {
  232. Redis::DEL('circle_message_new_comment_' . $newComment->post_id);
  233. }
  234. return jsonSuccess(['id' => $newComment->id], '评论成功');
  235. } catch (QueryException $exception) {
  236. DB::rollBack();
  237. Log::debug('评论内容失败:' . $exception->getMessage());
  238. return jsonError('评论内容失败,请重试');
  239. }
  240. }
  241. /**
  242. * 评论列表
  243. */
  244. public function commentList($request)
  245. {
  246. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  247. return $this->interestCircleMessageComment
  248. ->where('msg_id', $request['msg_id'])
  249. ->where('parent_id', 0)
  250. ->orderBy('id', 'desc')
  251. ->paginate($perPage);
  252. }
  253. /**
  254. * 提问评论数
  255. */
  256. public function getCommentCount($id)
  257. {
  258. $commentCount = 0;
  259. $post = $this->interestCircleMessage->find($id);
  260. if ($post) {
  261. $commentCount = $this->interestCircleMessageComment->where('msg_id', $id)->count();
  262. }
  263. return $commentCount;
  264. }
  265. /**
  266. * 回复列表
  267. */
  268. public function replyList($request)
  269. {
  270. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  271. return $this->interestCircleMessageComment
  272. ->where('parent_id', $request['comment_id'])
  273. ->orderBy('id', 'desc')
  274. ->paginate($perPage);
  275. }
  276. /**
  277. * 提问顶踩
  278. * @param $request
  279. * @return array
  280. */
  281. public function messageAction($request)
  282. {
  283. $userInfo = $this->getUserInfo();
  284. $userInfo['sns_status']=1;
  285. $userInfo['uid']=268;
  286. $userInfo['username']='测试用户';
  287. $userInfo['avatar']='';
  288. if (empty($userInfo)) {
  289. return jsonError('获取用户信息失败');
  290. }
  291. $actionRow = InterestCircleMessageRecord::where([['msg_id', $request['msg_id']], ['uid', $userInfo['uid']]])->first();
  292. if ($actionRow) {
  293. return jsonSuccess();
  294. }
  295. DB::beginTransaction();
  296. try {
  297. $data['msg_id'] = $request['msg_id'];
  298. $data['uid'] = $userInfo['uid'];
  299. $data['action'] = $request['action'];
  300. InterestCircleMessageRecord::insert($data);
  301. if($request['action']==1){
  302. InterestCircleMessage::where('id',$request['msg_id'])->increment('good');
  303. }elseif($request['action']==-1){
  304. InterestCircleMessage::where('id',$request['msg_id'])->increment('bad');
  305. }
  306. DB::commit();
  307. return jsonSuccess();
  308. } catch (QueryException $exception) {
  309. DB::rollBack();
  310. Log::debug('顶踩提问失败:' . $exception->getMessage());
  311. return jsonError('操作失败,请重试');
  312. }
  313. }
  314. }