CircleMessageRepository.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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 > 5) {
  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. $userInfo = $this->getUserInfo();
  175. // $userInfo['sns_status']=1;
  176. // $userInfo['uid']=268;
  177. // $userInfo['username']='测试用户';
  178. // $userInfo['avatar']='';
  179. if (empty($userInfo)) {
  180. return jsonError('获取用户信息失败');
  181. }
  182. if (!$userInfo['sns_status']) {
  183. return jsonError('您已被禁言');
  184. }
  185. $circleUser = $this->interestCircleUser
  186. ->where('circle_id', $request['circle_id'])
  187. ->where('uid', $userInfo['uid'])
  188. ->first();
  189. if ($circleUser) {
  190. if ($circleUser->is_black) {
  191. return jsonError('您在本圈子内的权限受限');
  192. }
  193. } else {
  194. return jsonError('抱歉,加入圈子才能互动');
  195. }
  196. $oneHourTime = Carbon::now()->addHours(-1)->toDateTimeString();
  197. $oneHourCommentCount = $this->interestCircleMessageComment->where('uid', $userInfo['uid'])->where('created_at', '>', $oneHourTime)->count();
  198. if ($oneHourCommentCount > 59) {
  199. return jsonError('回复了这么多,休息休息,喝口水吧!');
  200. }
  201. $detectionTextResult = $this->detectionService->checkText($request['content']);
  202. if ($detectionTextResult['code'] < 0) {
  203. return jsonError('内容违规,请修正哦');
  204. }
  205. $post = $this->interestCircleMessage->find($request['msg_id']);
  206. if (!$post) {
  207. return jsonError('获取提问信息失败');
  208. }
  209. $data = [
  210. 'uid' => $userInfo['uid'],
  211. 'circle_id' => $request['circle_id'],
  212. 'msg_id' => $request['msg_id'],
  213. 'parent_id' => 0,
  214. 'username' => $userInfo['username'],
  215. 'reply_uid' => 0,
  216. 'reply_username' => '',
  217. 'avatar' => $userInfo['avatar'] ?? '',
  218. 'content' => $request['content'],
  219. 'is_delete' => 0,
  220. ];
  221. if (isset($request['parent_id']) && $request['parent_id'] != 0) {
  222. $comment = $this->interestCircleMessageComment->find($request['parent_id']);
  223. if (!$comment || $comment->msg_id != $post->id) {
  224. return jsonError('获取评论信息失败');
  225. }
  226. if ($comment->parent_id) {
  227. return jsonError('只能回复评论');
  228. }
  229. if ($comment->is_delete) {
  230. return jsonError('不能回复已删除评论');
  231. }
  232. $data['parent_id'] = $request['parent_id'];
  233. if (isset($request['reply_uid']) && isset($request['reply_username'])) {
  234. $data['reply_uid'] = $request['reply_uid'];
  235. $data['reply_username'] = $request['reply_username'];
  236. } else {
  237. $data['reply_uid'] = 0;
  238. $data['reply_username'] = '';
  239. }
  240. }
  241. DB::beginTransaction();
  242. try {
  243. $newComment = $this->interestCircleMessageComment->create($data);
  244. if ($newComment->parent_id) {
  245. $this->interestCircleMessageComment->where('id', $newComment->parent_id)->increment('reply_count');
  246. }
  247. $this->interestCircleMessage->where('id', $request['msg_id'])->increment('comment_count');
  248. DB::commit();
  249. if ($newComment->parent_id) {
  250. Redis::DEL('circle_message_new_reply_' . $newComment->parent_id);
  251. } else {
  252. Redis::DEL('circle_message_new_comment_' . $newComment->post_id);
  253. }
  254. $key = "community_calc_circle_score";
  255. Redis::sadd($key,$request['msg_id']);
  256. return jsonSuccess(['id' => $newComment->id], '评论成功');
  257. } catch (QueryException $exception) {
  258. DB::rollBack();
  259. Log::debug('评论内容失败:' . $exception->getMessage());
  260. return jsonError('评论内容失败,请重试');
  261. }
  262. }
  263. /**
  264. * 评论列表
  265. */
  266. public function commentList($request)
  267. {
  268. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  269. return $this->interestCircleMessageComment
  270. ->where('msg_id', $request['msg_id'])
  271. ->where('parent_id', 0)
  272. ->orderBy('id', 'desc')
  273. ->paginate($perPage);
  274. }
  275. /**
  276. * 提问评论数
  277. */
  278. public function getCommentCount($id)
  279. {
  280. $commentCount = 0;
  281. $post = $this->interestCircleMessage->find($id);
  282. if ($post) {
  283. $commentCount = $this->interestCircleMessageComment->where('msg_id', $id)->count();
  284. }
  285. return $commentCount;
  286. }
  287. /**
  288. * 回复列表
  289. */
  290. public function replyList($request)
  291. {
  292. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  293. return $this->interestCircleMessageComment
  294. ->where('parent_id', $request['comment_id'])
  295. ->orderBy('id', 'desc')
  296. ->paginate($perPage);
  297. }
  298. /**
  299. * 提问顶踩
  300. * @param $request
  301. * @return array
  302. */
  303. public function messageAction($request)
  304. {
  305. $userInfo = $this->getUserInfo();
  306. // $userInfo['sns_status'] = 1;
  307. // $userInfo['uid'] = 268;
  308. // $userInfo['username'] = '测试用户';
  309. // $userInfo['avatar'] = '';
  310. if (empty($userInfo)) {
  311. return jsonError('获取用户信息失败');
  312. }
  313. $messageInfo = $this->interestCircleMessage->find($request['msg_id']);
  314. if (empty($messageInfo)) {
  315. return jsonError('该提问不存在');
  316. }
  317. $circleUser = $this->interestCircleUser
  318. ->where('circle_id', $messageInfo->circle_id)
  319. ->where('uid', $userInfo['uid'])
  320. ->first();
  321. if ($circleUser) {
  322. if ($circleUser->is_black) {
  323. return jsonError('您在本圈子内的权限受限');
  324. }
  325. } else {
  326. return jsonError('抱歉,加入圈子才能互动');
  327. }
  328. $actionRow = InterestCircleMessageRecord::where([['msg_id', $request['msg_id']], ['uid', $userInfo['uid']]])->first();
  329. if ($actionRow) {
  330. return jsonSuccess();
  331. }
  332. DB::beginTransaction();
  333. try {
  334. $data['msg_id'] = $request['msg_id'];
  335. $data['uid'] = $userInfo['uid'];
  336. $data['action'] = $request['action'];
  337. InterestCircleMessageRecord::insert($data);
  338. if ($request['action'] == 1) {
  339. InterestCircleMessage::where('id', $request['msg_id'])->increment('good');
  340. } elseif ($request['action'] == -1) {
  341. InterestCircleMessage::where('id', $request['msg_id'])->increment('bad');
  342. }
  343. DB::commit();
  344. $key = "community_calc_circle_score";
  345. Redis::sadd($key,$request['msg_id']);
  346. return jsonSuccess();
  347. } catch (QueryException $exception) {
  348. DB::rollBack();
  349. Log::debug('顶踩提问失败:' . $exception->getMessage());
  350. return jsonError('操作失败,请重试');
  351. }
  352. }
  353. /**
  354. * 删除评论
  355. */
  356. public function commentDelete($request)
  357. {
  358. $userInfo = $this->getUserInfo();
  359. if (empty($userInfo)) {
  360. return jsonError('获取用户信息失败');
  361. }
  362. $comment = $this->interestCircleMessageComment->find($request['id']);
  363. if (!$comment) {
  364. return jsonError('获取评论信息失败');
  365. }
  366. if ($userInfo['uid'] != $comment->uid) {
  367. return jsonError('只能删除自己的评论');
  368. }
  369. if ($comment->is_delete == 1) {
  370. return jsonError('该评论已经删除');
  371. }
  372. DB::beginTransaction();
  373. try {
  374. $comment->is_delete = 1;
  375. $comment->save();
  376. DB::commit();
  377. if (!$comment->parent_id) {
  378. Redis::DEL('circle_message_new_comment_' . $comment->post_id);
  379. } else {
  380. Redis::DEL('circle_message_new_reply_' . $comment->id);
  381. }
  382. Redis::SADD('delete_circle_message_comment_ids', $comment->id);
  383. return jsonSuccess('删除评论成功');
  384. } catch (QueryException $exception) {
  385. DB::rollBack();
  386. Log::debug('删除评论:' . $request['id'] . $exception->getMessage());
  387. return jsonError('删除评论失败');
  388. }
  389. }
  390. /**
  391. * 删除提问
  392. */
  393. public function deleteMessage($request)
  394. {
  395. //验证用户信息
  396. $userInfo = $this->getUserInfo();
  397. if (empty($userInfo)) {
  398. return jsonError('获取用户信息失败');
  399. }
  400. $post = $this->interestCircleMessage->find($request['id']);
  401. if (!$post) {
  402. return jsonError('获取提问信息失败');
  403. }
  404. if ($post->uid != $userInfo['uid']) {
  405. return jsonError('只能删除自己发布的提问');
  406. }
  407. DB::beginTransaction();
  408. try {
  409. $post->delete();
  410. $this->interestCircle->where('circle_id', $post->circle_id)->decrement('message_count');
  411. DB::commit();
  412. Redis::SADD('delete_circle_message_ids', $request['id']);
  413. Log::debug('删除提问失败:' . $request['id']);
  414. return jsonSuccess('删除提问成功');
  415. } catch (QueryException $exception) {
  416. DB::rollBack();
  417. Log::debug('删除提问失败:' . $request['id'] . $exception->getMessage());
  418. return jsonError('删除提问失败,请重试');
  419. }
  420. }
  421. /**
  422. * 创建相册
  423. */
  424. public function createPictures($request)
  425. {
  426. //验证小号
  427. $userInfo = $this->getUserInfo();
  428. // $userInfo['sns_status']=1;
  429. // $userInfo['uid']=268;
  430. if (empty($userInfo)) {
  431. return jsonError('获取用户信息失败');
  432. }
  433. if (!$userInfo['sns_status']) {
  434. return jsonError('您已被禁言');
  435. }
  436. $circleUser = $this->interestCircleUser
  437. ->where('circle_id', $request['circle_id'])
  438. ->where('uid', $userInfo['uid'])
  439. ->first();
  440. if ($circleUser) {
  441. if ($circleUser->is_black) {
  442. return jsonError('您在本圈子内的权限受限');
  443. }
  444. } else {
  445. return jsonError('抱歉,加入圈子才能互动');
  446. }
  447. $oneHourTime = Carbon::now()->addHours(-1)->toDateTimeString();
  448. $oneHourPostCount = $this->interestCirclePicture
  449. ->where('uid', $userInfo['uid'])
  450. ->where('is_main', 1)
  451. ->where('created_at', '>', $oneHourTime)
  452. ->count();
  453. if ($oneHourPostCount > 5) {
  454. return jsonError('创作欲望太强啦,休息一下,看看其他用户的相册吧!');
  455. }
  456. $imgs = json_decode($request['imgs'], true);
  457. $imgCount = count($imgs);
  458. if ($imgCount == 0 || $imgCount > 9) {
  459. return jsonError('所传图集必须1-9个');
  460. }
  461. $allImg = $imgs;
  462. foreach ($allImg as &$img) {
  463. if (Str::contains($img, '?')) {
  464. $img = $img . '&x-oss-process=image/resize,p_50/quality,Q_50';
  465. } else {
  466. $img = $img . '?x-oss-process=image/resize,p_50/quality,Q_50';
  467. }
  468. }
  469. $detectionImageResult = $this->detectionService->checkImg($allImg);
  470. if ($detectionImageResult['code'] < 0) {
  471. Log::debug('图片违规,请修正哦' . json_encode($detectionImageResult));
  472. return jsonError('图片违规,请修正哦');
  473. }
  474. $date = date('Y-m-d H:i:s');
  475. $patchNum = Str::random(32);
  476. DB::beginTransaction();
  477. try {
  478. $imgData = [];
  479. foreach ($imgs as $k => $img) {
  480. $isMain = 0;
  481. if ($k == 0) {
  482. $isMain = 1;
  483. }
  484. $imgData[] = [
  485. 'circle_id' => $request['circle_id'],
  486. 'uid' => $userInfo['uid'],
  487. 'image' => $img,
  488. 'patch_num' => $patchNum,
  489. 'is_main' => $isMain,
  490. 'created_at' => $date,
  491. 'updated_at' => $date
  492. ];
  493. }
  494. $this->interestCirclePicture->insert($imgData);
  495. $this->interestCircle->where('id', $request['circle_id'])->increment('picture_count', $imgCount);
  496. $key = 'circle_picture_' . $patchNum;
  497. Redis::set($key, json_encode($imgs));
  498. DB::commit();
  499. return jsonSuccess();
  500. } catch (QueryException $exception) {
  501. DB::rollBack();
  502. Log::debug('上传相册失败:' . $exception->getMessage());
  503. return jsonError('上传失败');
  504. }
  505. }
  506. /**
  507. * 相册列表
  508. */
  509. public function pictureLists($request)
  510. {
  511. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  512. $where[] = ['circle_id', $request['circle_id']];
  513. $where[] = ['is_main', 1];
  514. return $this->interestCirclePicture
  515. ->where($where)
  516. ->orderBy('id', 'desc')
  517. ->paginate($perPage);
  518. }
  519. /**
  520. * 删除相册图片
  521. */
  522. public function deletePicture($request)
  523. {
  524. $circle = $this->interestCirclePicture->where('patch_num', $request['id'])->first();
  525. if (!$circle) {
  526. return jsonError('相册不存在');
  527. }
  528. $userInfo = $this->getUserInfo();
  529. // $userInfo['sns_status']=1;
  530. // $userInfo['uid']=268;
  531. if (empty($userInfo)) {
  532. return jsonError('获取用户信息失败');
  533. }
  534. if ($userInfo['uid'] != $circle->uid) {
  535. return jsonError('只能删除自己的相册');
  536. }
  537. DB::beginTransaction();
  538. try {
  539. $picCount = $this->interestCirclePicture->where('patch_num', $request['id'])->count();
  540. $this->interestCirclePicture->where('patch_num', $request['id'])->delete();
  541. $this->interestCircle->where('circle_id', $circle->circle_id)->decrement('picture_count', $picCount);
  542. DB::commit();
  543. return jsonSuccess();
  544. } catch (QueryException $exception) {
  545. DB::rollBack();
  546. Log::debug('删除相册图片:' . $request['id'] . $exception->getMessage());
  547. return jsonError('操作失败,请重试');
  548. }
  549. }
  550. }