CircleMessageRepository.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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\InterestCirclePicture;
  16. use App\Models\InterestCircleUser;
  17. use App\Models\Post;
  18. use App\Service\DetectionService;
  19. use App\Traits\UserTrait;
  20. use Illuminate\Database\QueryException;
  21. use Dingo\Api\Http\Response;
  22. use Illuminate\Support\Carbon;
  23. use Illuminate\Support\Facades\DB;
  24. use Illuminate\Support\Facades\Log;
  25. use Illuminate\Support\Facades\Redis;
  26. use Illuminate\Support\Str;
  27. class CircleMessageRepository
  28. {
  29. use UserTrait;
  30. public function __construct(InterestCircle $interestCircle,
  31. InterestCircleMessage $interestCircleMessage,
  32. InterestCirclePicture $interestCirclePicture,
  33. InterestCircleMessageImg $interestCircleMessageImg,
  34. InterestCircleUser $interestCircleUser,
  35. DetectionService $detectionService,
  36. InterestCircleMessageComment $interestCircleMessageComment
  37. )
  38. {
  39. $this->interestCircle = $interestCircle;
  40. $this->interestCircleMessage = $interestCircleMessage;
  41. $this->interestCirclePicture = $interestCirclePicture;
  42. $this->interestCircleMessageImg = $interestCircleMessageImg;
  43. $this->interestCircleUser = $interestCircleUser;
  44. $this->interestCircleMessageComment = $interestCircleMessageComment;
  45. $this->detectionService = $detectionService;
  46. }
  47. /**
  48. * 查询单个提问
  49. * @param $id
  50. * @return mixed
  51. */
  52. public function detail($id)
  53. {
  54. return $this->interestCircleMessage->find($id);
  55. }
  56. /**
  57. * 提问列表
  58. */
  59. public function lists($request)
  60. {
  61. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  62. $where[] = ['circle_id', $request['id']];
  63. return $this->interestCircleMessage
  64. ->where($where)
  65. ->with('imgs')
  66. ->orderBy('is_recommend', 'desc')
  67. ->orderBy('id', 'desc')
  68. ->paginate($perPage);
  69. }
  70. /**
  71. * 发布提问
  72. */
  73. public function create($request)
  74. {
  75. //验证小号
  76. $userInfo = $this->getUserInfo();
  77. // $userInfo['sns_status']=1;
  78. // $userInfo['uid']=268;
  79. if (empty($userInfo)) {
  80. return jsonError('获取用户信息失败');
  81. }
  82. if (!$userInfo['sns_status']) {
  83. return jsonError('您已被禁言');
  84. }
  85. $circleUser = $this->interestCircleUser
  86. ->where('circle_id', $request['circle_id'])
  87. ->where('uid', $userInfo['uid'])
  88. ->first();
  89. if ($circleUser) {
  90. if ($circleUser->is_black) {
  91. return jsonError('您在本圈子内的权限受限');
  92. }
  93. } else {
  94. return jsonError('抱歉,加入圈子才能互动');
  95. }
  96. $oneHourTime = Carbon::now()->addHours(-1)->toDateTimeString();
  97. $oneHourPostCount = $this->interestCircleMessage->where('uid', $userInfo['uid'])->where('created_at', '>', $oneHourTime)->count();
  98. if ($oneHourPostCount > 50) {
  99. return jsonError('创作欲望太强啦,休息一下,看看其他用户的提问吧!');
  100. }
  101. $detectionText = strip_tags($request['content']);
  102. $detectionTextResult = $this->detectionService->checkText($detectionText);
  103. if ($detectionTextResult['code'] < 0) {
  104. return jsonError('内容违规,请修正哦');
  105. }
  106. $imgs = [];
  107. if (isset($request['imgs']) && $request['imgs']) {
  108. $imgs = json_decode($request['imgs'], true);
  109. $imgCount = count($imgs);
  110. if ($imgCount > 3) {
  111. return jsonError('最多上传3张');
  112. }
  113. }
  114. if ($imgs) {
  115. $allImg = $imgs;
  116. foreach ($allImg as &$img) {
  117. if (Str::contains($img, '?')) {
  118. $img = $img . '&x-oss-process=image/resize,p_50/quality,Q_50';
  119. } else {
  120. $img = $img . '?x-oss-process=image/resize,p_50/quality,Q_50';
  121. }
  122. }
  123. $detectionImageResult = $this->detectionService->checkImg($allImg);
  124. if ($detectionImageResult['code'] < 0) {
  125. Log::debug('图片违规,请修正哦' . json_encode($detectionImageResult));
  126. return jsonError('图片违规,请修正哦');
  127. }
  128. }
  129. $fresh = (Carbon::now()->timestamp) - (Carbon::parse("2019-10-08 00:00:00")->timestamp);
  130. $score = ($fresh / 43200) * 14;
  131. $data = [
  132. 'circle_id' => $request['circle_id'],
  133. 'uid' => $userInfo['uid'],
  134. 'content' => $request['content'],
  135. 'good' => 0,
  136. 'bad' => 0,
  137. 'comment_count' => 0,
  138. 'weight' => $score
  139. ];
  140. $date = date('Y-m-d H:i:s');
  141. DB::beginTransaction();
  142. try {
  143. $message = $this->interestCircleMessage->create($data);
  144. if ($imgs) {
  145. $imgData = [];
  146. foreach ($imgs as $img) {
  147. $imgData[] = [
  148. 'msg_id' => $message->id,
  149. 'circle_id' => $message->circle_id,
  150. 'image' => $img,
  151. 'created_at' => $date,
  152. 'updated_at' => $date
  153. ];
  154. }
  155. $this->interestCircleMessageImg->insert($imgData);
  156. }
  157. $this->interestCircle->where('id', $request['circle_id'])->increment('message_count');
  158. DB::commit();
  159. Log::info('message_create:' . $message->id . ',post_author:' . $message->uid . ',author_ip:' . getClientIp());
  160. return jsonSuccess();
  161. } catch (QueryException $exception) {
  162. DB::rollBack();
  163. Log::debug('发布提问失败:' . $exception->getMessage());
  164. return jsonError('发布提问失败,请重试');
  165. }
  166. }
  167. /**
  168. * 评论&回复
  169. * @param $request
  170. * @return array
  171. */
  172. public function createComment($request)
  173. {
  174. Log::debug('comment-request:'.json_encode($request));
  175. $userInfo = $this->getUserInfo();
  176. // $userInfo['sns_status']=1;
  177. // $userInfo['uid']=268;
  178. // $userInfo['username']='测试用户';
  179. // $userInfo['avatar']='';
  180. if (empty($userInfo)) {
  181. return jsonError('获取用户信息失败');
  182. }
  183. if (!$userInfo['sns_status']) {
  184. return jsonError('您已被禁言');
  185. }
  186. $circleUser = $this->interestCircleUser
  187. ->where('circle_id', $request['circle_id'])
  188. ->where('uid', $userInfo['uid'])
  189. ->first();
  190. if ($circleUser) {
  191. if ($circleUser->is_black) {
  192. return jsonError('您在本圈子内的权限受限');
  193. }
  194. } else {
  195. return jsonError('抱歉,加入圈子才能互动');
  196. }
  197. $oneHourTime = Carbon::now()->addHours(-1)->toDateTimeString();
  198. $oneHourCommentCount = $this->interestCircleMessageComment->where('uid', $userInfo['uid'])->where('created_at', '>', $oneHourTime)->count();
  199. if ($oneHourCommentCount > 59) {
  200. return jsonError('回复了这么多,休息休息,喝口水吧!');
  201. }
  202. $detectionTextResult = $this->detectionService->checkText($request['content']);
  203. if ($detectionTextResult['code'] < 0) {
  204. return jsonError('内容违规,请修正哦');
  205. }
  206. $post = $this->interestCircleMessage->find($request['msg_id']);
  207. if (!$post) {
  208. return jsonError('获取提问信息失败');
  209. }
  210. $data = [
  211. 'uid' => $userInfo['uid'],
  212. 'circle_id' => $request['circle_id'],
  213. 'msg_id' => $request['msg_id'],
  214. 'parent_id' => 0,
  215. 'username' => $userInfo['username'],
  216. 'reply_uid' => 0,
  217. 'reply_username' => '',
  218. 'avatar' => $userInfo['avatar'] ?? '',
  219. 'content' => $request['content'],
  220. 'is_delete' => 0,
  221. ];
  222. if (isset($request['parent_id']) && $request['parent_id'] != 0) {
  223. $comment = $this->interestCircleMessageComment->find($request['parent_id']);
  224. if (!$comment || $comment->msg_id != $post->id) {
  225. return jsonError('获取评论信息失败');
  226. }
  227. if ($comment->parent_id) {
  228. return jsonError('只能回复评论');
  229. }
  230. if ($comment->is_delete) {
  231. return jsonError('不能回复已删除评论');
  232. }
  233. $data['parent_id'] = $request['parent_id'];
  234. if (isset($request['reply_uid']) && isset($request['reply_username'])) {
  235. $data['reply_uid'] = $request['reply_uid'];
  236. $data['reply_username'] = $request['reply_username'];
  237. } else {
  238. $data['reply_uid'] = 0;
  239. $data['reply_username'] = '';
  240. }
  241. }
  242. DB::beginTransaction();
  243. try {
  244. $newComment = $this->interestCircleMessageComment->create($data);
  245. if ($newComment->parent_id) {
  246. $this->interestCircleMessageComment->where('id', $newComment->parent_id)->increment('reply_count');
  247. }
  248. $this->interestCircleMessage->where('id', $request['msg_id'])->increment('comment_count');
  249. DB::commit();
  250. if ($newComment->parent_id) {
  251. Redis::DEL('circle_message_new_reply_' . $newComment->parent_id);
  252. Log::debug('删除回复缓存'.$newComment->id);
  253. } else {
  254. Redis::DEL('circle_message_new_comment_' . $newComment->msg_id);
  255. Log::debug('删除评论缓存'.$newComment->msg_id);
  256. }
  257. $key = "community_calc_circle_score";
  258. Redis::sadd($key,$request['msg_id']);
  259. return jsonSuccess(['id' => $newComment->id], '评论成功');
  260. } catch (QueryException $exception) {
  261. DB::rollBack();
  262. Log::debug('评论内容失败:' . $exception->getMessage());
  263. return jsonError('评论内容失败,请重试');
  264. }
  265. }
  266. /**
  267. * 评论列表
  268. */
  269. public function commentList($request)
  270. {
  271. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  272. return $this->interestCircleMessageComment
  273. ->where('msg_id', $request['msg_id'])
  274. ->where('parent_id', 0)
  275. ->orderBy('id', 'desc')
  276. ->paginate($perPage);
  277. }
  278. /**
  279. * 提问评论数
  280. */
  281. public function getCommentCount($id)
  282. {
  283. $commentCount = 0;
  284. $post = $this->interestCircleMessage->find($id);
  285. if ($post) {
  286. $commentCount = $this->interestCircleMessageComment->where('msg_id', $id)->count();
  287. }
  288. return $commentCount;
  289. }
  290. /**
  291. * 回复列表
  292. */
  293. public function replyList($request)
  294. {
  295. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  296. return $this->interestCircleMessageComment
  297. ->where('parent_id', $request['comment_id'])
  298. ->orderBy('id', 'desc')
  299. ->paginate($perPage);
  300. }
  301. /**
  302. * 提问顶踩
  303. * @param $request
  304. * @return array
  305. */
  306. public function messageAction($request)
  307. {
  308. $userInfo = $this->getUserInfo();
  309. // $userInfo['sns_status'] = 1;
  310. // $userInfo['uid'] = 268;
  311. // $userInfo['username'] = '测试用户';
  312. // $userInfo['avatar'] = '';
  313. if (empty($userInfo)) {
  314. return jsonError('获取用户信息失败');
  315. }
  316. $messageInfo = $this->interestCircleMessage->find($request['msg_id']);
  317. if (empty($messageInfo)) {
  318. return jsonError('该提问不存在');
  319. }
  320. $circleUser = $this->interestCircleUser
  321. ->where('circle_id', $messageInfo->circle_id)
  322. ->where('uid', $userInfo['uid'])
  323. ->first();
  324. if ($circleUser) {
  325. if ($circleUser->is_black) {
  326. return jsonError('您在本圈子内的权限受限');
  327. }
  328. } else {
  329. return jsonError('抱歉,加入圈子才能互动');
  330. }
  331. $actionRow = InterestCircleMessageRecord::where([['msg_id', $request['msg_id']], ['uid', $userInfo['uid']]])->first();
  332. if ($actionRow) {
  333. return jsonSuccess();
  334. }
  335. DB::beginTransaction();
  336. try {
  337. $data['msg_id'] = $request['msg_id'];
  338. $data['uid'] = $userInfo['uid'];
  339. $data['action'] = $request['action'];
  340. InterestCircleMessageRecord::insert($data);
  341. if ($request['action'] == 1) {
  342. InterestCircleMessage::where('id', $request['msg_id'])->increment('good');
  343. } elseif ($request['action'] == -1) {
  344. InterestCircleMessage::where('id', $request['msg_id'])->increment('bad');
  345. }
  346. DB::commit();
  347. $key = "community_calc_circle_score";
  348. Redis::sadd($key,$request['msg_id']);
  349. return jsonSuccess();
  350. } catch (QueryException $exception) {
  351. DB::rollBack();
  352. Log::debug('顶踩提问失败:' . $exception->getMessage());
  353. return jsonError('操作失败,请重试');
  354. }
  355. }
  356. /**
  357. * 删除评论
  358. */
  359. public function commentDelete($request)
  360. {
  361. $userInfo = $this->getUserInfo();
  362. if (empty($userInfo)) {
  363. return jsonError('获取用户信息失败');
  364. }
  365. $comment = $this->interestCircleMessageComment->find($request['id']);
  366. if (!$comment) {
  367. return jsonError('获取评论信息失败');
  368. }
  369. if ($userInfo['uid'] != $comment->uid) {
  370. return jsonError('只能删除自己的评论');
  371. }
  372. if ($comment->is_delete == 1) {
  373. return jsonError('该评论已经删除');
  374. }
  375. DB::beginTransaction();
  376. try {
  377. $comment->is_delete = 1;
  378. $comment->save();
  379. DB::commit();
  380. if (!$comment->parent_id) {
  381. Redis::DEL('circle_message_new_comment_' . $comment->msg_id);
  382. } else {
  383. Redis::DEL('circle_message_new_reply_' . $comment->id);
  384. }
  385. Redis::SADD('delete_circle_message_comment_ids', $comment->id);
  386. return jsonSuccess();
  387. } catch (QueryException $exception) {
  388. DB::rollBack();
  389. Log::debug('删除评论:' . $request['id'] . $exception->getMessage());
  390. return jsonError('删除评论失败');
  391. }
  392. }
  393. /**
  394. * 删除提问
  395. */
  396. public function deleteMessage($request)
  397. {
  398. //验证用户信息
  399. $userInfo = $this->getUserInfo();
  400. if (empty($userInfo)) {
  401. return jsonError('获取用户信息失败');
  402. }
  403. $post = $this->interestCircleMessage->find($request['id']);
  404. if (!$post) {
  405. return jsonError('获取提问信息失败');
  406. }
  407. if ($post->uid != $userInfo['uid']) {
  408. return jsonError('只能删除自己发布的提问');
  409. }
  410. DB::beginTransaction();
  411. try {
  412. $post->delete();
  413. $this->interestCircle->where('id', $post->circle_id)->decrement('message_count');
  414. DB::commit();
  415. Redis::SADD('delete_circle_message_ids', $request['id']);
  416. Log::debug('删除提问成功:' . $request['id']);
  417. return jsonSuccess();
  418. } catch (QueryException $exception) {
  419. DB::rollBack();
  420. Log::debug('删除提问失败:' . $request['id'] . $exception->getMessage());
  421. return jsonError('删除提问失败,请重试');
  422. }
  423. }
  424. /**
  425. * 创建相册
  426. */
  427. public function createPictures($request)
  428. {
  429. //验证小号
  430. $userInfo = $this->getUserInfo();
  431. // $userInfo['sns_status']=1;
  432. // $userInfo['uid']=268;
  433. if (empty($userInfo)) {
  434. return jsonError('获取用户信息失败');
  435. }
  436. if (!$userInfo['sns_status']) {
  437. return jsonError('您已被禁言');
  438. }
  439. $circleUser = $this->interestCircleUser
  440. ->where('circle_id', $request['circle_id'])
  441. ->where('uid', $userInfo['uid'])
  442. ->first();
  443. if ($circleUser) {
  444. if ($circleUser->is_black) {
  445. return jsonError('您在本圈子内的权限受限');
  446. }
  447. } else {
  448. return jsonError('抱歉,加入圈子才能互动');
  449. }
  450. $oneHourTime = Carbon::now()->addHours(-1)->toDateTimeString();
  451. $oneHourPostCount = $this->interestCirclePicture
  452. ->where('uid', $userInfo['uid'])
  453. ->where('is_main', 1)
  454. ->where('created_at', '>', $oneHourTime)
  455. ->count();
  456. if ($oneHourPostCount > 5) {
  457. return jsonError('创作欲望太强啦,休息一下,看看其他用户的相册吧!');
  458. }
  459. $imgs = json_decode($request['imgs'], true);
  460. $imgCount = count($imgs);
  461. if ($imgCount == 0 || $imgCount > 9) {
  462. return jsonError('所传图集必须1-9个');
  463. }
  464. $allImg = $imgs;
  465. foreach ($allImg as &$img) {
  466. if (Str::contains($img, '?')) {
  467. $img = $img . '&x-oss-process=image/resize,p_50/quality,Q_50';
  468. } else {
  469. $img = $img . '?x-oss-process=image/resize,p_50/quality,Q_50';
  470. }
  471. }
  472. $detectionImageResult = $this->detectionService->checkImg($allImg);
  473. if ($detectionImageResult['code'] < 0) {
  474. Log::debug('图片违规,请修正哦' . json_encode($detectionImageResult));
  475. return jsonError('图片违规,请修正哦');
  476. }
  477. $date = date('Y-m-d H:i:s');
  478. $patchNum = Str::random(32);
  479. DB::beginTransaction();
  480. try {
  481. $imgData = [];
  482. foreach ($imgs as $k => $img) {
  483. $isMain = 0;
  484. if ($k == 0) {
  485. $isMain = 1;
  486. }
  487. $imgData[] = [
  488. 'circle_id' => $request['circle_id'],
  489. 'uid' => $userInfo['uid'],
  490. 'image' => $img,
  491. 'patch_num' => $patchNum,
  492. 'is_main' => $isMain,
  493. 'created_at' => $date,
  494. 'updated_at' => $date
  495. ];
  496. }
  497. $this->interestCirclePicture->insert($imgData);
  498. $this->interestCircle->where('id', $request['circle_id'])->increment('picture_count', $imgCount);
  499. $key = 'circle_picture_' . $patchNum;
  500. Redis::set($key, json_encode($imgs));
  501. DB::commit();
  502. return jsonSuccess();
  503. } catch (QueryException $exception) {
  504. DB::rollBack();
  505. Log::debug('上传相册失败:' . $exception->getMessage());
  506. return jsonError('上传失败');
  507. }
  508. }
  509. /**
  510. * 相册列表
  511. */
  512. public function pictureLists($request)
  513. {
  514. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  515. $where[] = ['circle_id', $request['circle_id']];
  516. $where[] = ['is_main', 1];
  517. return $this->interestCirclePicture
  518. ->where($where)
  519. ->orderBy('id', 'desc')
  520. ->paginate($perPage);
  521. }
  522. /**
  523. * 删除相册图片
  524. */
  525. public function deletePicture($request)
  526. {
  527. $circle = $this->interestCirclePicture->where('patch_num', $request['id'])->first();
  528. if (!$circle) {
  529. return jsonError('相册不存在');
  530. }
  531. $userInfo = $this->getUserInfo();
  532. // $userInfo['sns_status']=1;
  533. // $userInfo['uid']=268;
  534. if (empty($userInfo)) {
  535. return jsonError('获取用户信息失败');
  536. }
  537. if ($userInfo['uid'] != $circle->uid) {
  538. return jsonError('只能删除自己的相册');
  539. }
  540. DB::beginTransaction();
  541. try {
  542. $picCount = $this->interestCirclePicture->where('patch_num', $request['id'])->count();
  543. $this->interestCirclePicture->where('patch_num', $request['id'])->delete();
  544. $this->interestCircle->where('circle_id', $circle->circle_id)->decrement('picture_count', $picCount);
  545. DB::commit();
  546. return jsonSuccess();
  547. } catch (QueryException $exception) {
  548. DB::rollBack();
  549. Log::debug('删除相册图片:' . $request['id'] . $exception->getMessage());
  550. return jsonError('操作失败,请重试');
  551. }
  552. }
  553. }