CircleMessageRepository.php 20 KB

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