PostRepositories.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: edz
  5. * Date: 2019-06-10
  6. * Time: 17:53
  7. */
  8. namespace App\Repositories;
  9. use App\Models\Behavior;
  10. use App\Models\CategoryTopic;
  11. use App\Models\MemberFollowTopic;
  12. use App\Models\Post;
  13. use App\Models\PostCollect;
  14. use App\Models\PostComment;
  15. use App\Models\PostCommentLike;
  16. use App\Models\PostData;
  17. use App\Models\PostDislike;
  18. use App\Models\PostImgs;
  19. use App\Models\PostLike;
  20. use App\Models\PostLog;
  21. use App\Models\PostShare;
  22. use App\Models\Topic;
  23. use App\Service\AliYunVodService;
  24. use App\Service\DetectionService;
  25. use App\Traits\CmsTrait;
  26. use App\Traits\PostTrait;
  27. use App\Traits\UserTrait;
  28. use Illuminate\Database\QueryException;
  29. use Illuminate\Support\Carbon;
  30. use Illuminate\Support\Facades\Log;
  31. use Illuminate\Support\Facades\Redis;
  32. use Illuminate\Support\Facades\DB;
  33. use Tymon\JWTAuth\Facades\JWTAuth;
  34. class PostRepositories
  35. {
  36. use UserTrait;
  37. use PostTrait;
  38. use CmsTrait;
  39. public function __construct(Post $post,
  40. PostData $postData,
  41. PostImgs $postImgs,
  42. PostComment $postComment,
  43. PostCollect $postCollect,
  44. PostShare $postShare,
  45. PostLog $postLog,
  46. DetectionService $detectionService,
  47. AliYunVodService $aliYunVodService,
  48. MemberFollowTopic $memberFollowTopic,
  49. CategoryTopic $categoryTopic,
  50. Topic $topic)
  51. {
  52. $this->post = $post;
  53. $this->postData = $postData;
  54. $this->postImgs = $postImgs;
  55. $this->postComment = $postComment;
  56. $this->postCollect = $postCollect;
  57. $this->postShare = $postShare;
  58. $this->postLog = $postLog;
  59. $this->detectionService = $detectionService;
  60. $this->topic = $topic;
  61. $this->memberFollowTopic = $memberFollowTopic;
  62. $this->categoryTopic = $categoryTopic;
  63. $this->aliYunVodService = $aliYunVodService;
  64. }
  65. /**
  66. * 发布内容
  67. */
  68. public function create($request)
  69. {
  70. //验证小号
  71. $userInfo = $this->getUserInfo();
  72. if (empty($userInfo)) {
  73. return jsonError('获取用户信息失败');
  74. }
  75. if (!$userInfo['sns_status']) {
  76. return jsonError('您已被禁言');
  77. }
  78. $isValid = 0;
  79. if ($userInfo['strength']) {
  80. $isValid = 1;
  81. }
  82. $oneHourTime = Carbon::now()->addHours(-1)->toDateTimeString();
  83. $oneHourPostCount = $this->post->where('uid', $userInfo['uid'])->where('created_at', '>', $oneHourTime)->count();
  84. if ($oneHourPostCount > 5) {
  85. return jsonError('创作欲望太强啦,休息一下,看看其他用户的内容吧!');
  86. }
  87. $detectionText = $request['title'] . ',' . $request['content'];
  88. $detectionTextResult = $this->detectionService->checkText($detectionText);
  89. if ($detectionTextResult['code'] < 0) {
  90. return jsonError('内容违规,请修正哦');
  91. }
  92. $topicIds = json_decode($request['topic_ids'], true);
  93. $topicCount = count($topicIds);
  94. if ($topicCount == 0 || $topicCount > 5) {
  95. return jsonError('所选话题必须1-5个');
  96. }
  97. //验证话题
  98. $hasTopicCount = $this->topic->whereIn('id', $topicIds)->count();
  99. if ($topicCount != $hasTopicCount) {
  100. Log::error('所选话题非法' . $request['topic_ids']);
  101. return jsonError('所选话题非法');
  102. }
  103. $imgs = [];
  104. if ($request['type'] == 'image') {
  105. $imgs = json_decode($request['imgs'], true);
  106. $imgCount = count($imgs);
  107. if ($imgCount == 0 || $imgCount > 9) {
  108. return jsonError('所传图集必须1-9个');
  109. }
  110. }
  111. $allImg = array_merge($imgs, [$request['img']]);
  112. foreach ($allImg as &$img) {
  113. $img = $img . '&x-oss-process=image/resize,p_50/quality,Q_50';
  114. }
  115. $detectionImageResult = $this->detectionService->checkImg($allImg);
  116. if ($detectionImageResult['code'] < 0) {
  117. Log::debug('图片违规,请修正哦' . json_encode($detectionImageResult));
  118. return jsonError('图片违规,请修正哦');
  119. }
  120. $videoUrl = "";
  121. $videoId = "";
  122. if (isset($request['video']) && $request['video']) {
  123. $videoId = $request['video'];
  124. for ($i = 0; $i < 3; $i++) {
  125. $videoUrl = $this->aliYunVodService->getPlayUrlByVideoId($request['video']);
  126. Log::debug('video-url:' . $videoUrl);
  127. if ($videoUrl) {
  128. break;
  129. }
  130. }
  131. if (empty($videoUrl)) {
  132. return jsonError('发布失败,请重试');
  133. }
  134. }
  135. $fresh = (Carbon::now()->timestamp) - (Carbon::parse("2019-05-01 00:00:00")->timestamp);
  136. $score = $fresh / 43200;
  137. $data = [
  138. 'uid' => $userInfo['uid'],
  139. 'username' => $userInfo['username'],
  140. 'mobile' => $userInfo['mobile'],
  141. 'avatar' => $userInfo['avatar'] ?? '',
  142. 'type' => $request['type'],
  143. 'img' => $request['img'],
  144. 'video' => $videoUrl,
  145. 'video_id' => $videoId,
  146. 'topic_ids' => implode(',', $topicIds),
  147. 'title' => isset($request['title']) ? $request['title'] : '',
  148. 'content' => $request['content'],
  149. 'location' => isset($request['location']) ? $request['location'] : '',
  150. 'is_suggest' => 0,
  151. 'is_hide' => 0,
  152. 'weight' => $score
  153. ];
  154. $date = date('Y-m-d H:i:s');
  155. DB::beginTransaction();
  156. try {
  157. $post = $this->post->create($data);
  158. $postData = $this->postData->create([
  159. 'post_id' => $post->id,
  160. 'pv' => 0,
  161. 'pv_real' => 0,
  162. 'dislike_count' => 0,
  163. 'praise_count' => 0,
  164. 'praise_real_count' => 0,
  165. 'share_count' => 0,
  166. 'share_real_count' => 0,
  167. 'comment_count' => 0,
  168. 'collect_count' => 0,
  169. 'collect_real_count' => 0,
  170. 'available_bean' => $this->availableBean(),
  171. 'will_collect_bean' => rand(100, 200),
  172. 'collect_bean' => 0
  173. ]);
  174. if ($imgs) {
  175. $imgData = [];
  176. foreach ($imgs as $img) {
  177. $imgData[] = [
  178. 'post_id' => $post->id,
  179. 'img' => $img,
  180. 'created_at' => $date,
  181. 'updated_at' => $date
  182. ];
  183. }
  184. $this->postImgs->insert($imgData);
  185. }
  186. DB::commit();
  187. Redis::zadd('post_trigger_type', $isValid, $post->id);
  188. foreach ($topicIds as $id) {
  189. Redis::zincrby('topic.user_uid' . $userInfo['uid'], 1, $id);
  190. Redis::zincrby('topic.just_use_count', 1, $id);
  191. }
  192. Redis::HSET('post_info_' . $post->id,
  193. 'id', $post->id,
  194. 'uid', $post->uid,
  195. 'type', $post->type,
  196. 'img', $post->img,
  197. 'imgs', json_encode($imgs),
  198. 'video', $post->video,
  199. 'topic_ids', $post->topic_ids,
  200. 'is_fine', $post->is_fine,
  201. 'title', $post->title,
  202. 'content', $post->content,
  203. 'location', $post->location,
  204. 'pv', $postData->pv,
  205. 'dislike_count', $postData->dislike_count,
  206. 'praise_count', $postData->praise_count,
  207. 'share_count', $postData->share_count,
  208. 'comment_count', $postData->comment_count,
  209. 'collect_count', $postData->collect_count,
  210. 'available_bean', $postData->available_bean,
  211. 'will_collect_bean', $postData->will_collect_bean,
  212. 'create_bean', $postData->create_bean,
  213. 'collect_bean', $postData->collect_bean,
  214. 'created_at', $post->created_at);
  215. Log::info('post_create:' . $post->id . ',post_author:' . $post->uid . ',author_ip:' . getClientIp());
  216. return jsonSuccess([
  217. 'post_id' => $post->id,
  218. 'h5url' => config('customer.share_post_h5url') . "?post_id={$post->id}&invite_code={$userInfo['invite_code']}",
  219. 'bean' => $postData->available_bean,
  220. ]);
  221. } catch (QueryException $exception) {
  222. DB::rollBack();
  223. Log::debug('发布内容失败:' . $exception->getMessage());
  224. return jsonError('发布内容失败,请重试');
  225. }
  226. }
  227. /**
  228. * 评论&回复
  229. */
  230. public function comment($request)
  231. {
  232. //验证小号
  233. $userInfo = $this->getUserInfo();
  234. if (empty($userInfo)) {
  235. return jsonError('获取用户信息失败');
  236. }
  237. if (!$userInfo['sns_status']) {
  238. return jsonError('您已被禁言');
  239. }
  240. $oneHourTime = Carbon::now()->addHours(-1)->toDateTimeString();
  241. $oneHourCommentCount = $this->postComment->where('uid', $userInfo['uid'])->where('created_at', '>', $oneHourTime)->count();
  242. if ($oneHourCommentCount > 59) {
  243. return jsonError('回复了这么多,休息休息,喝口水吧!');
  244. }
  245. $detectionTextResult = $this->detectionService->checkText($request['content']);
  246. if ($detectionTextResult['code'] < 0) {
  247. return jsonError('内容违规,请修正哦');
  248. }
  249. $post = $this->post->find($request['post_id']);
  250. if (!$post) {
  251. return jsonError('获取内容信息失败');
  252. }
  253. $data = [
  254. 'uid' => $userInfo['uid'],
  255. 'post_id' => $request['post_id'],
  256. 'parent_id' => 0,
  257. 'username' => $userInfo['username'],
  258. 'reply_uid' => 0,
  259. 'reply_username' => '',
  260. 'avatar' => $userInfo['avatar'] ?? '',
  261. 'content' => $request['content'],
  262. 'is_delete' => 0,
  263. ];
  264. if (isset($request['parent_id']) && $request['parent_id'] != 0) {
  265. $comment = $this->postComment->find($request['parent_id']);
  266. if (!$comment || $comment->post_id != $post->id) {
  267. return jsonError('获取评论信息失败');
  268. }
  269. if ($comment->parent_id) {
  270. return jsonError('只能回复评论');
  271. }
  272. if ($comment->is_delete) {
  273. return jsonError('不能回复已删除评论');
  274. }
  275. $data['parent_id'] = $request['parent_id'];
  276. if (isset($request['reply_uid']) && isset($request['reply_username'])) {
  277. $data['reply_uid'] = $request['reply_uid'];
  278. $data['reply_username'] = $request['reply_username'];
  279. } else {
  280. $data['reply_uid'] = 0;
  281. $data['reply_username'] = '';
  282. }
  283. }
  284. DB::beginTransaction();
  285. try {
  286. $newComment = $this->postComment->create($data);
  287. if ($newComment->parent_id) {
  288. $this->postComment->where('id', $newComment->parent_id)->increment('reply_count');
  289. }
  290. DB::commit();
  291. if ($newComment->parent_id) {
  292. Redis::DEL('post_new_reply_' . $newComment->parent_id);
  293. } else {
  294. Redis::DEL('post_new_comment_' . $newComment->post_id);
  295. }
  296. return jsonSuccess(['id' => $newComment->id], '评论成功');
  297. } catch (QueryException $exception) {
  298. DB::rollBack();
  299. Log::debug('评论内容失败:' . $exception->getMessage());
  300. return jsonError('评论内容失败,请重试');
  301. }
  302. }
  303. /**
  304. * 删除评论
  305. */
  306. public function commentDelete($request)
  307. {
  308. $userInfo = $this->getUserInfo();
  309. if (empty($userInfo)) {
  310. return jsonError('获取用户信息失败');
  311. }
  312. $comment = $this->postComment->find($request['id']);
  313. if (!$comment) {
  314. return jsonError('获取评论信息失败');
  315. }
  316. if ($userInfo['uid'] != $comment->uid) {
  317. return jsonError('只能删除自己的评论');
  318. }
  319. if ($comment->is_delete == 1) {
  320. return jsonError('该评论已经删除');
  321. }
  322. DB::beginTransaction();
  323. try {
  324. $comment->is_delete = 1;
  325. $comment->save();
  326. DB::commit();
  327. if (!$comment->parent_id) {
  328. Redis::DEL('post_new_comment_' . $comment->post_id);
  329. } else {
  330. Redis::DEL('post_new_reply_' . $comment->id);
  331. }
  332. Redis::SADD('delete_post_comment_ids', $comment->id);
  333. return jsonSuccess('删除评论成功');
  334. } catch (QueryException $exception) {
  335. DB::rollBack();
  336. Log::debug('删除评论:' . $request['id'] . $exception->getMessage());
  337. return jsonError('删除评论失败');
  338. }
  339. }
  340. /**
  341. * 内容列表
  342. */
  343. public function lists($request)
  344. {
  345. Log::debug('内容列表' . json_encode($request));
  346. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  347. return $this->post
  348. ->where(function ($query) use ($request) {
  349. if (isset($request['keyword'])) {
  350. $query->where('title', 'like', "%{$request['keyword']}%")
  351. ->orWhere('content', 'like', "%{$request['keyword']}%");
  352. }
  353. })
  354. ->where(function ($query) use ($request) {
  355. if (isset($request['topic_ids']) && $request['topic_ids']) {
  356. $topicIds = json_decode($request['topic_ids'], true);
  357. foreach ($topicIds as $key => $id) {
  358. if ($key == 0) {
  359. $query = $query->whereRaw('FIND_IN_SET(' . $id . ', topic_ids)');
  360. } else {
  361. $query = $query->orWhereRaw('FIND_IN_SET(' . $id . ', topic_ids)');
  362. }
  363. }
  364. }
  365. })
  366. ->orderBy('weight', 'desc')
  367. ->orderBy('id', 'desc')
  368. ->paginate($perPage);
  369. }
  370. /**
  371. * 视频列表
  372. */
  373. public function video($request)
  374. {
  375. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  376. $where = [];
  377. $id = 0;
  378. if (isset($request['id'])) {
  379. $id = $request['id'];
  380. }
  381. $where[] = ['type', 'video'];
  382. if (isset($request['type']) && $request['type']) {
  383. if ($request['type'] == 'hot') {
  384. $ids = Redis::get('hotVideoIds');
  385. if (!$ids) {
  386. $ids = $this->hotVideoIds();
  387. if (!$ids) {
  388. $ids = '';
  389. }
  390. }
  391. Log::debug('热门视频ids' . $ids);
  392. return $this->post
  393. ->select('*', DB::raw("IF (id = {$id},1,0) as sort"))
  394. ->where($where)
  395. ->whereIn('id', explode(',', $ids))
  396. ->orderBy('sort', 'desc')
  397. ->orderByRaw(DB::raw("FIELD(id,{$ids})"))
  398. ->paginate($perPage);
  399. } elseif ($request['type'] == 'one') {
  400. $where[] = ['id', $id];
  401. return $this->post
  402. ->where($where)
  403. ->paginate($perPage);
  404. }
  405. }
  406. return $this->post
  407. ->select('*', DB::raw("IF (id = {$id},1,0) as sort"))
  408. ->where($where)
  409. ->where(function ($query) use ($request) {
  410. if (isset($request['topic_id']) && $request['topic_id']) {
  411. $query->whereRaw('FIND_IN_SET(' . $request['topic_id'] . ', topic_ids)');
  412. }
  413. })
  414. ->orderBy('sort', 'desc')
  415. ->orderBy('weight', 'desc')
  416. ->orderBy('id', 'desc')
  417. ->paginate($perPage);
  418. }
  419. /**
  420. * 个人中心内容列表
  421. */
  422. public function myPost($request, $uid)
  423. {
  424. $type = $request['type'];
  425. $perPage = isset($request['per_page']) ? $request['per_page'] : 18;
  426. $where = [];
  427. if ($type == 'create') {
  428. $where[] = ['post.uid', $uid];
  429. $post = $this->post;
  430. $order = 'post.id';
  431. } elseif ($type == 'collect') {
  432. $post = $this->post->withTrashed()->join('post_collect', 'post_collect.post_id', '=', 'post.id');
  433. $where[] = ['post_collect.uid', $uid];
  434. $order = 'post_collect.id';
  435. } else {
  436. $post = $this->post->withTrashed()->join('post_share', 'post_share.post_id', '=', 'post.id');
  437. $where[] = ['post_share.uid', $uid];
  438. $order = 'post_share.updated_at';
  439. }
  440. return $post
  441. ->select('post.*')
  442. ->where($where)
  443. ->orderBy($order, 'desc')
  444. ->paginate($perPage);
  445. }
  446. /**
  447. * 内容详情
  448. */
  449. public function detail($id)
  450. {
  451. return $this->post
  452. ->find($id);
  453. }
  454. /**
  455. * 内容是否存在
  456. */
  457. public function detailExists($id)
  458. {
  459. return $this->post->where('id', $id)->exists();
  460. }
  461. /**
  462. * 推荐内容列表
  463. */
  464. public function suggestPost($request, $uid)
  465. {
  466. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  467. if ($uid) {
  468. $blacklist = Redis::smembers('blacklist_' . $uid);
  469. } else {
  470. $blacklist = [];
  471. }
  472. $topicIds = [];
  473. if (isset($request['category_id']) && $request['category_id']) {
  474. $topicIds = $this->categoryTopic->where('category_id', $request['category_id'])->pluck('topic_id')->toArray();
  475. }
  476. return $this->post
  477. ->where(function ($query) use ($blacklist) {
  478. if ($blacklist) {
  479. $query->whereNotIn('uid', $blacklist);
  480. }
  481. })
  482. ->where(function ($query) use ($topicIds) {
  483. if ($topicIds) {
  484. foreach ($topicIds as $key => $id) {
  485. if ($key == 0) {
  486. $query = $query->whereRaw('FIND_IN_SET(' . $id . ', topic_ids)');
  487. } else {
  488. $query = $query->orWhereRaw('FIND_IN_SET(' . $id . ', topic_ids)');
  489. }
  490. }
  491. }
  492. })
  493. ->where('is_hide', 0)
  494. ->orderBy('is_suggest', 'desc')
  495. ->orderBy('weight', 'desc')
  496. ->orderBy('id', 'desc')
  497. ->paginate($perPage);
  498. }
  499. /**
  500. * 话题内容
  501. */
  502. public function topicPost($request)
  503. {
  504. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  505. return $this->post
  506. ->whereRaw('FIND_IN_SET(' . $request['id'] . ',topic_ids)')
  507. ->orderBy('id', 'desc')
  508. ->paginate($perPage);
  509. }
  510. /**
  511. * 评论列表
  512. */
  513. public function commentList($request)
  514. {
  515. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  516. return $this->postComment
  517. ->where('post_id', $request['post_id'])
  518. ->where('parent_id', 0)
  519. ->orderBy('like_count', 'desc')
  520. ->orderBy('id', 'desc')
  521. ->paginate($perPage);
  522. }
  523. /**
  524. * 回复列表
  525. */
  526. public function replyList($request)
  527. {
  528. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  529. return $this->postComment
  530. ->where('parent_id', $request['id'])
  531. ->orderBy('id', 'desc')
  532. ->paginate($perPage);
  533. }
  534. /**
  535. * 根据评论id获取内容id
  536. */
  537. public function getPostId($commentId)
  538. {
  539. return $this->postComment
  540. ->where('id', $commentId)
  541. ->value('post_id');
  542. }
  543. /**
  544. * 话题列表
  545. */
  546. public function topicList($request)
  547. {
  548. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  549. $where = [];
  550. $topic = $this->topic;
  551. if (isset($request['name'])) {
  552. $where[] = ['topic.name', 'like', "%{$request['name']}%"];
  553. }
  554. if (isset($request['category_id']) && $request['category_id']) {
  555. if ($request['category_id'] == -2) {
  556. $where[] = ['topic.is_hot', 1];
  557. } else {
  558. $topic = $topic->join('category_topic', 'category_topic.topic_id', '=', 'topic.id')->select('topic.*', 'category_topic.id as cid');
  559. $where[] = ['category_topic.category_id', $request['category_id']];
  560. $where[] = ['topic.is_open', 1];
  561. return $topic
  562. ->where($where)
  563. ->orderBy('cid', 'asc')
  564. ->paginate($perPage);
  565. }
  566. }
  567. return $topic
  568. ->where($where)
  569. ->orderBy('id', 'desc')
  570. ->paginate($perPage);
  571. }
  572. /**
  573. * 我的话题列表
  574. */
  575. public function myTopicList($request)
  576. {
  577. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  578. $uid = 0;
  579. $user = $this->getUserInfo();
  580. if ($user) {
  581. $uid = $user['uid'];
  582. }
  583. return $this->memberFollowTopic
  584. ->where('uid', $uid)
  585. ->orderBy('id', 'desc')
  586. ->paginate($perPage);
  587. }
  588. /**
  589. * 更新帖子统计数量
  590. * @param $request
  591. * @return mixed
  592. */
  593. public function updatePostData($request)
  594. {
  595. $postId = isset($request['post_id']) ? $request['post_id'] : 0;
  596. if (empty($postId)) {
  597. Log::debug("非帖子类操作,不操作帖子统计数量" . json_encode($request));
  598. return true;
  599. }
  600. //帖子缓存信息key
  601. $postInfoKey = "post_info_" . $postId;
  602. $post = PostData::where('post_id', $postId)->first();
  603. if (!$post) return true;
  604. $row = false;
  605. if (isset($request['behavior_flag']) && $request['behavior_flag'] == 'read') {
  606. $post->pv += 1;
  607. $post->pv_real += 1;
  608. Redis::HINCRBY($postInfoKey, 'pv', 1);
  609. $topicIdStr = Redis::HGET($postInfoKey, 'topic_ids');
  610. if ($topicIdStr) {
  611. Log::debug('话题增加浏览量' . $topicIdStr);
  612. $topicIds = explode(',', $topicIdStr);
  613. foreach ($topicIds as $id) {
  614. $this->topic->where('id', $id)->increment('pv');
  615. }
  616. }
  617. $row = $post->save();
  618. Log::debug("帖子:" . $postId . "被阅读,pv +1");
  619. } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'unlike') {
  620. //用户不喜欢帖子key
  621. $postUnLikeKey = "post_unlike_" . $postId;
  622. $post->dislike_count += 1;
  623. PostDislike::create(['uid' => $request['target_id'], 'post_id' => $request['post_id']]);
  624. Redis::sadd($postUnLikeKey, $request['target_id']);
  625. Redis::HINCRBY($postInfoKey, 'dislike_count', 1);
  626. $row = $post->save();
  627. Log::debug("帖子:" . $postId . "被不喜欢,unlike +1");
  628. } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'like') {
  629. //用户点赞帖子
  630. $postLikeKey = "post_like_" . $postId;
  631. if ($request['behavior_value']) {
  632. $post->praise_count += 1;
  633. $post->praise_real_count += 1;
  634. PostLike::create(['uid' => $request['target_id'], 'post_id' => $request['post_id']]);
  635. Redis::sadd($postLikeKey, $request['target_id']);
  636. Redis::HINCRBY($postInfoKey, 'praise_count', 1);
  637. Log::debug("帖子:" . $postId . "被点赞,praise_count +1");
  638. } else {
  639. $post->praise_count -= 1;
  640. $post->praise_real_count -= 1;
  641. PostLike::where(['uid' => $request['target_id'], 'post_id' => $request['post_id']])->delete();
  642. Redis::srem($postLikeKey, $request['target_id']);
  643. Redis::HINCRBY($postInfoKey, 'praise_count', -1);
  644. Log::debug("帖子:" . $postId . "被取消点赞,praise_count -1");
  645. }
  646. $row = $post->save();
  647. } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'comment_like') {
  648. //用户点赞评论
  649. $comment = PostComment::where('id', $request['comment_id'])->first();
  650. $postLikeKey = "comment_like_" . $postId;
  651. if ($request['behavior_value']) {
  652. $comment->like_count += 1;
  653. PostCommentLike::create(['uid' => $request['target_id'], 'comment_id' => $request['comment_id']]);
  654. Redis::zadd($postLikeKey, $request['comment_id'], $request['target_id'] . '_' . $request['comment_id']);
  655. Log::debug("评论:" . $request['comment_id'] . "被点赞,like_count +1");
  656. } else {
  657. $comment->like_count -= 1;
  658. PostCommentLike::where(['uid' => $request['target_id'], 'comment_id' => $request['comment_id']])->delete();
  659. Redis::zrem($postLikeKey, $request['target_id'] . '_' . $request['comment_id']);
  660. Log::debug("评论:" . $request['comment_id'] . "被取消点赞,like_count -1");
  661. }
  662. $row = $comment->save();
  663. } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'forward') {
  664. $post->share_count += 1;
  665. $post->share_real_count += 1;
  666. Redis::HINCRBY($postInfoKey, 'share_count', 1);
  667. $shareRow = PostShare::where(['uid' => $request['target_id'], 'post_id' => $request['post_id']])->first();
  668. if ($shareRow) {
  669. PostShare::where(['uid' => $request['target_id'], 'post_id' => $request['post_id']])->update(['uid' => $request['target_id'], 'post_id' => $request['post_id']]);
  670. } else {
  671. PostShare::create(['uid' => $request['target_id'], 'post_id' => $request['post_id']]);
  672. }
  673. $row = $post->save();
  674. Log::debug("帖子:" . $postId . "被分享,share_count +1");
  675. } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'comment') {
  676. $post->comment_count += 1;
  677. Redis::HINCRBY($postInfoKey, 'comment_count', 1);
  678. $row = $post->save();
  679. Log::debug("帖子:" . $postId . "被评论,comment_count +1");
  680. } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'collect') {
  681. //用户收藏帖子
  682. $postCollectKey = "post_collect_" . $postId;
  683. if ($request['behavior_value']) {
  684. $post->collect_count += 1;
  685. $post->collect_real_count += 1;
  686. PostCollect::create(['uid' => $request['target_id'], 'post_id' => $request['post_id']]);
  687. Redis::sadd($postCollectKey, $request['target_id']);
  688. Redis::HINCRBY($postInfoKey, 'collect_count', 1);
  689. Log::debug("帖子:" . $postId . "被收藏,collect_count +1");
  690. } else {
  691. $post->collect_count -= 1;
  692. $post->collect_real_count -= 1;
  693. PostCollect::where(['uid' => $request['target_id'], 'post_id' => $request['post_id']])->delete();
  694. Redis::srem($postCollectKey, $request['target_id']);
  695. Redis::HINCRBY($postInfoKey, 'collect_count', -1);
  696. Log::debug("帖子:" . $postId . "被取消收藏,collect_count -1");
  697. }
  698. $row = $post->save();
  699. }
  700. $this->collectPostId($request['post_id']);
  701. return $row;
  702. }
  703. /**
  704. * 收集所有有操作的帖子,存入redis
  705. * 供后续计算帖子权重
  706. * @param $id
  707. */
  708. public function collectPostId($id)
  709. {
  710. $key = "community_calc_post_score";
  711. Redis::sadd($key, $id);
  712. Log::debug('存入帖子' . $id . '到权重列表');
  713. }
  714. /**
  715. * 话题详情
  716. */
  717. public function topicDetail($id)
  718. {
  719. return $this->topic
  720. ->find($id);
  721. }
  722. /**
  723. * 获取话题
  724. */
  725. public function getTopic($ids)
  726. {
  727. $topics = $this->topic
  728. ->whereIn('id', explode(',', $ids))
  729. ->orderByRaw(DB::raw("FIELD(id,{$ids})"))
  730. ->get();
  731. $data = [];
  732. foreach ($topics as $topic) {
  733. $data[] = [
  734. 'id' => $topic->id,
  735. 'name' => $topic->name,
  736. 'img' => $topic->img,
  737. 'follow_count' => getNumber($topic->use_count + $topic->base_count),
  738. ];
  739. }
  740. return $data;
  741. }
  742. /**
  743. * 获取内容视频组
  744. */
  745. public function getPostVideo($ids)
  746. {
  747. $posts = $this->post
  748. ->select('id', 'img', 'uid')
  749. ->whereIn('id', explode(',', $ids))
  750. ->where('type', 'video')
  751. ->get();
  752. foreach ($posts as &$post) {
  753. $user = $this->userInfo($post->uid);
  754. $post->username = $user['username'];
  755. $post->avatar = $user['avatar'];
  756. }
  757. return $posts;
  758. }
  759. //用户内容数,转发数,收藏数统计
  760. public function memberPostStatistics($uid)
  761. {
  762. $postCount = $this->post->where('uid', $uid)->count();
  763. $postCollectCount = $this->postCollect->where('uid', $uid)->count();
  764. $postShareCount = $this->postShare->where('uid', $uid)->count();
  765. $data = ['post_count' => $postCount, 'share_count' => $postShareCount, 'collect_count' => $postCollectCount];
  766. return jsonSuccess($data);
  767. }
  768. /**
  769. * 删除内容
  770. */
  771. public function delete($request)
  772. {
  773. //验证用户信息
  774. $userInfo = $this->getUserInfo();
  775. if (empty($userInfo)) {
  776. return jsonError('获取用户信息失败');
  777. }
  778. $post = $this->post->find($request['id']);
  779. if (!$post) {
  780. return jsonError('获取内容信息失败');
  781. }
  782. if ($post->uid != $userInfo['uid']) {
  783. return jsonError('只能删除自己发布的内容');
  784. }
  785. $logData = [
  786. 'uid' => $userInfo['uid'],
  787. 'operator_type' => 'user',
  788. 'post_id' => $request['id'],
  789. 'username' => $userInfo['username'],
  790. 'log_type' => 'delete',
  791. 'content' => json_encode(['delete' => $request['id']]),
  792. ];
  793. DB::beginTransaction();
  794. try {
  795. $post->delete();
  796. $this->postLog->create($logData);
  797. DB::commit();
  798. Redis::SADD('delete_post_ids', $request['id']);
  799. Log::debug('删除内容失败:' . $request['id']);
  800. return jsonSuccess('删除内容成功');
  801. } catch (QueryException $exception) {
  802. DB::rollBack();
  803. Log::debug('删除内容失败:' . $request['id'] . $exception->getMessage());
  804. return jsonError('删除内容失败,请重试');
  805. }
  806. }
  807. /**
  808. * 内容评论数
  809. */
  810. public function getCommentCount($id)
  811. {
  812. $commentCount = 0;
  813. $post = $this->post->find($id);
  814. if ($post) {
  815. $commentCount = $post->data->comment_count;
  816. }
  817. return $commentCount;
  818. }
  819. /**
  820. * 内容评论数
  821. */
  822. public function checkImage($img)
  823. {
  824. return $this->detectionService->checkImg($img);
  825. }
  826. }