CircleMessageRepository.php 14 KB

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