PostRepositories.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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. 'title', $post->title,
  201. 'content', $post->content,
  202. 'location', $post->location,
  203. 'pv', $postData->pv,
  204. 'dislike_count', $postData->dislike_count,
  205. 'praise_count', $postData->praise_count,
  206. 'share_count', $postData->share_count,
  207. 'comment_count', $postData->comment_count,
  208. 'collect_count', $postData->collect_count,
  209. 'available_bean', $postData->available_bean,
  210. 'will_collect_bean', $postData->will_collect_bean,
  211. 'create_bean', $postData->create_bean,
  212. 'collect_bean', $postData->collect_bean,
  213. 'created_at', $post->created_at);
  214. Log::info('post_create:' . $post->id . ',post_author:' . $post->uid . ',author_ip:' . getClientIp());
  215. return jsonSuccess([
  216. 'post_id' => $post->id,
  217. 'h5url' => config('customer.share_post_h5url') . "?post_id={$post->id}&invite_code={$userInfo['invite_code']}",
  218. 'bean' => $postData->available_bean,
  219. ]);
  220. } catch (QueryException $exception) {
  221. DB::rollBack();
  222. Log::debug('发布内容失败:' . $exception->getMessage());
  223. return jsonError('发布内容失败,请重试');
  224. }
  225. }
  226. /**
  227. * 评论&回复
  228. */
  229. public function comment($request)
  230. {
  231. //验证小号
  232. $userInfo = $this->getUserInfo();
  233. if (empty($userInfo)) {
  234. return jsonError('获取用户信息失败');
  235. }
  236. if (!$userInfo['sns_status']) {
  237. return jsonError('您已被禁言');
  238. }
  239. $oneHourTime = Carbon::now()->addHours(-1)->toDateTimeString();
  240. $oneHourCommentCount = $this->postComment->where('uid', $userInfo['uid'])->where('created_at', '>', $oneHourTime)->count();
  241. if ($oneHourCommentCount > 59) {
  242. return jsonError('回复了这么多,休息休息,喝口水吧!');
  243. }
  244. $detectionTextResult = $this->detectionService->checkText($request['content']);
  245. if ($detectionTextResult['code'] < 0) {
  246. return jsonError('内容违规,请修正哦');
  247. }
  248. $post = $this->post->find($request['post_id']);
  249. if (!$post) {
  250. return jsonError('获取内容信息失败');
  251. }
  252. $data = [
  253. 'uid' => $userInfo['uid'],
  254. 'post_id' => $request['post_id'],
  255. 'parent_id' => 0,
  256. 'username' => $userInfo['username'],
  257. 'reply_uid' => 0,
  258. 'reply_username' => '',
  259. 'avatar' => $userInfo['avatar'] ?? '',
  260. 'content' => $request['content'],
  261. 'is_delete' => 0,
  262. ];
  263. if (isset($request['parent_id']) && $request['parent_id'] != 0) {
  264. $comment = $this->postComment->find($request['parent_id']);
  265. if (!$comment || $comment->post_id != $post->id) {
  266. return jsonError('获取评论信息失败');
  267. }
  268. if ($comment->parent_id) {
  269. return jsonError('只能回复评论');
  270. }
  271. if ($comment->is_delete) {
  272. return jsonError('不能回复已删除评论');
  273. }
  274. $data['parent_id'] = $request['parent_id'];
  275. if (isset($request['reply_uid']) && isset($request['reply_username'])) {
  276. $data['reply_uid'] = $request['reply_uid'];
  277. $data['reply_username'] = $request['reply_username'];
  278. } else {
  279. $data['reply_uid'] = 0;
  280. $data['reply_username'] = '';
  281. }
  282. }
  283. DB::beginTransaction();
  284. try {
  285. $newComment = $this->postComment->create($data);
  286. if ($newComment->parent_id) {
  287. $this->postComment->where('id', $newComment->parent_id)->increment('reply_count');
  288. }
  289. DB::commit();
  290. if ($newComment->parent_id) {
  291. Redis::DEL('post_new_reply_' . $newComment->parent_id);
  292. } else {
  293. Redis::DEL('post_new_comment_' . $newComment->post_id);
  294. }
  295. return jsonSuccess(['id' => $newComment->id], '评论成功');
  296. } catch (QueryException $exception) {
  297. DB::rollBack();
  298. Log::debug('评论内容失败:' . $exception->getMessage());
  299. return jsonError('评论内容失败,请重试');
  300. }
  301. }
  302. /**
  303. * 删除评论
  304. */
  305. public function commentDelete($request)
  306. {
  307. $userInfo = $this->getUserInfo();
  308. if (empty($userInfo)) {
  309. return jsonError('获取用户信息失败');
  310. }
  311. $comment = $this->postComment->find($request['id']);
  312. if (!$comment) {
  313. return jsonError('获取评论信息失败');
  314. }
  315. if ($userInfo['uid'] != $comment->uid) {
  316. return jsonError('只能删除自己的评论');
  317. }
  318. if ($comment->is_delete == 1) {
  319. return jsonError('该评论已经删除');
  320. }
  321. DB::beginTransaction();
  322. try {
  323. $comment->is_delete = 1;
  324. $comment->save();
  325. DB::commit();
  326. if (!$comment->parent_id) {
  327. Redis::DEL('post_new_comment_' . $comment->post_id);
  328. } else {
  329. Redis::DEL('post_new_reply_' . $comment->id);
  330. }
  331. Redis::SADD('delete_post_comment_ids', $comment->id);
  332. return jsonSuccess('删除评论成功');
  333. } catch (QueryException $exception) {
  334. DB::rollBack();
  335. Log::debug('删除评论:' . $request['id'] . $exception->getMessage());
  336. return jsonError('删除评论失败');
  337. }
  338. }
  339. /**
  340. * 内容列表
  341. */
  342. public function lists($request)
  343. {
  344. Log::debug('内容列表' . json_encode($request));
  345. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  346. return $this->post
  347. ->where(function ($query) use ($request) {
  348. if (isset($request['keyword'])) {
  349. $query->where('title', 'like', "%{$request['keyword']}%")
  350. ->orWhere('content', 'like', "%{$request['keyword']}%");
  351. }
  352. })
  353. ->where(function ($query) use ($request) {
  354. if (isset($request['topic_ids']) && $request['topic_ids']) {
  355. $topicIds = json_decode($request['topic_ids'], true);
  356. foreach ($topicIds as $key => $id) {
  357. if ($key == 0) {
  358. $query = $query->whereRaw('FIND_IN_SET(' . $id . ', topic_ids)');
  359. } else {
  360. $query = $query->orWhereRaw('FIND_IN_SET(' . $id . ', topic_ids)');
  361. }
  362. }
  363. }
  364. })
  365. ->orderBy('weight', 'desc')
  366. ->orderBy('id', 'desc')
  367. ->paginate($perPage);
  368. }
  369. /**
  370. * 视频列表
  371. */
  372. public function video($request)
  373. {
  374. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  375. $where = [];
  376. $id = 0;
  377. if (isset($request['id'])) {
  378. $id = $request['id'];
  379. }
  380. $where[] = ['type', 'video'];
  381. if (isset($request['type']) && $request['type']) {
  382. if ($request['type'] == 'hot') {
  383. $ids = Redis::get('hotVideoIds');
  384. if (!$ids) {
  385. $ids = $this->hotVideoIds();
  386. if (!$ids) {
  387. $ids = '';
  388. }
  389. }
  390. Log::debug('热门视频ids' . $ids);
  391. return $this->post
  392. ->select('*', DB::raw("IF (id = {$id},1,0) as sort"))
  393. ->where($where)
  394. ->whereIn('id', explode(',', $ids))
  395. ->orderBy('sort', 'desc')
  396. ->orderByRaw(DB::raw("FIELD(id,{$ids})"))
  397. ->paginate($perPage);
  398. } elseif ($request['type'] == 'one') {
  399. $where[] = ['id', $id];
  400. return $this->post
  401. ->where($where)
  402. ->paginate($perPage);
  403. }
  404. }
  405. return $this->post
  406. ->select('*', DB::raw("IF (id = {$id},1,0) as sort"))
  407. ->where($where)
  408. ->where(function ($query) use ($request) {
  409. if (isset($request['topic_id']) && $request['topic_id']) {
  410. $query->whereRaw('FIND_IN_SET(' . $request['topic_id'] . ', topic_ids)');
  411. }
  412. })
  413. ->orderBy('sort', 'desc')
  414. ->orderBy('weight', 'desc')
  415. ->orderBy('id', 'desc')
  416. ->paginate($perPage);
  417. }
  418. /**
  419. * 个人中心内容列表
  420. */
  421. public function myPost($request, $uid)
  422. {
  423. $type = $request['type'];
  424. $perPage = isset($request['per_page']) ? $request['per_page'] : 18;
  425. $where = [];
  426. if ($type == 'create') {
  427. $where[] = ['post.uid', $uid];
  428. $post = $this->post;
  429. $order = 'post.id';
  430. } elseif ($type == 'collect') {
  431. $post = $this->post->withTrashed()->join('post_collect', 'post_collect.post_id', '=', 'post.id');
  432. $where[] = ['post_collect.uid', $uid];
  433. $order = 'post_collect.id';
  434. } else {
  435. $post = $this->post->withTrashed()->join('post_share', 'post_share.post_id', '=', 'post.id');
  436. $where[] = ['post_share.uid', $uid];
  437. $order = 'post_share.updated_at';
  438. }
  439. return $post
  440. ->select('post.*')
  441. ->where($where)
  442. ->orderBy($order, 'desc')
  443. ->paginate($perPage);
  444. }
  445. /**
  446. * 内容详情
  447. */
  448. public function detail($id)
  449. {
  450. return $this->post
  451. ->find($id);
  452. }
  453. /**
  454. * 内容是否存在
  455. */
  456. public function detailExists($id)
  457. {
  458. return $this->post->where('id', $id)->exists();
  459. }
  460. /**
  461. * 推荐内容列表
  462. */
  463. public function suggestPost($request, $uid)
  464. {
  465. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  466. if ($uid) {
  467. $blacklist = Redis::smembers('blacklist_' . $uid);
  468. } else {
  469. $blacklist = [];
  470. }
  471. $topicIds = [];
  472. if (isset($request['category_id']) && $request['category_id']) {
  473. $topicIds = $this->categoryTopic->where('category_id', $request['category_id'])->pluck('topic_id')->toArray();
  474. }
  475. return $this->post
  476. ->where(function ($query) use ($blacklist) {
  477. if ($blacklist) {
  478. $query->whereNotIn('uid', $blacklist);
  479. }
  480. })
  481. ->where(function ($query) use ($topicIds) {
  482. if ($topicIds) {
  483. foreach ($topicIds as $key => $id) {
  484. if ($key == 0) {
  485. $query = $query->whereRaw('FIND_IN_SET(' . $id . ', topic_ids)');
  486. } else {
  487. $query = $query->orWhereRaw('FIND_IN_SET(' . $id . ', topic_ids)');
  488. }
  489. }
  490. }
  491. })
  492. ->where('is_hide', 0)
  493. ->orderBy('is_suggest', 'desc')
  494. ->orderBy('weight', 'desc')
  495. ->orderBy('id', 'desc')
  496. ->paginate($perPage);
  497. }
  498. /**
  499. * 话题内容
  500. */
  501. public function topicPost($request)
  502. {
  503. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  504. return $this->post
  505. ->whereRaw('FIND_IN_SET(' . $request['id'] . ',topic_ids)')
  506. ->orderBy('id', 'desc')
  507. ->paginate($perPage);
  508. }
  509. /**
  510. * 评论列表
  511. */
  512. public function commentList($request)
  513. {
  514. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  515. return $this->postComment
  516. ->where('post_id', $request['post_id'])
  517. ->where('parent_id', 0)
  518. ->orderBy('id', 'desc')
  519. ->paginate($perPage);
  520. }
  521. /**
  522. * 回复列表
  523. */
  524. public function replyList($request)
  525. {
  526. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  527. return $this->postComment
  528. ->where('parent_id', $request['id'])
  529. ->orderBy('id', 'desc')
  530. ->paginate($perPage);
  531. }
  532. /**
  533. * 话题列表
  534. */
  535. public function topicList($request)
  536. {
  537. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  538. $where = [];
  539. $topic = $this->topic;
  540. if (isset($request['name'])) {
  541. $where[] = ['topic.name', 'like', "%{$request['name']}%"];
  542. }
  543. if (isset($request['category_id']) && $request['category_id']) {
  544. if ($request['category_id'] == -2) {
  545. $where[] = ['topic.is_hot', 1];
  546. } else {
  547. $topic = $topic->join('category_topic', 'category_topic.topic_id', '=', 'topic.id')->select('topic.*', 'category_topic.id as cid');
  548. $where[] = ['category_topic.category_id', $request['category_id']];
  549. return $topic
  550. ->where($where)
  551. ->orderBy('cid', 'asc')
  552. ->paginate($perPage);
  553. }
  554. }
  555. return $topic
  556. ->where($where)
  557. ->orderBy('id', 'desc')
  558. ->paginate($perPage);
  559. }
  560. /**
  561. * 我的话题列表
  562. */
  563. public function myTopicList($request)
  564. {
  565. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  566. $uid = 0;
  567. $user = $this->getUserInfo();
  568. if ($user) {
  569. $uid = $user['uid'];
  570. }
  571. return $this->memberFollowTopic
  572. ->where('uid', $uid)
  573. ->orderBy('id', 'desc')
  574. ->paginate($perPage);
  575. }
  576. /**
  577. * 更新帖子统计数量
  578. * @param $request
  579. * @return mixed
  580. */
  581. public function updatePostData($request)
  582. {
  583. $postId = isset($request['post_id']) ? $request['post_id'] : 0;
  584. if (empty($postId)) {
  585. Log::debug("非帖子类操作,不操作帖子统计数量" . json_encode($request));
  586. return true;
  587. }
  588. //帖子缓存信息key
  589. $postInfoKey = "post_info_" . $postId;
  590. $post = PostData::where('post_id', $postId)->first();
  591. if (!$post) return true;
  592. $row = false;
  593. if (isset($request['behavior_flag']) && $request['behavior_flag'] == 'read') {
  594. $post->pv += 1;
  595. $post->pv_real += 1;
  596. Redis::HINCRBY($postInfoKey, 'pv', 1);
  597. $topicIdStr = Redis::HGET($postInfoKey, 'topic_ids');
  598. if ($topicIdStr) {
  599. Log::debug('话题增加浏览量' . $topicIdStr);
  600. $topicIds = explode(',', $topicIdStr);
  601. foreach ($topicIds as $id) {
  602. $this->topic->where('id', $id)->increment('pv');
  603. }
  604. }
  605. $row = $post->save();
  606. Log::debug("帖子:" . $postId . "被阅读,pv +1");
  607. } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'unlike') {
  608. //用户不喜欢帖子key
  609. $postUnLikeKey = "post_unlike_" . $postId;
  610. $post->dislike_count += 1;
  611. PostDislike::create(['uid' => $request['target_id'], 'post_id' => $request['post_id']]);
  612. Redis::sadd($postUnLikeKey, $request['target_id']);
  613. Redis::HINCRBY($postInfoKey, 'dislike_count', 1);
  614. $row = $post->save();
  615. Log::debug("帖子:" . $postId . "被不喜欢,unlike +1");
  616. } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'like') {
  617. //用户点赞帖子
  618. $postLikeKey = "post_like_" . $postId;
  619. if ($request['behavior_value']) {
  620. $post->praise_count += 1;
  621. $post->praise_real_count += 1;
  622. PostLike::create(['uid' => $request['target_id'], 'post_id' => $request['post_id']]);
  623. Redis::sadd($postLikeKey, $request['target_id']);
  624. Redis::HINCRBY($postInfoKey, 'praise_count', 1);
  625. Log::debug("帖子:" . $postId . "被点赞,praise_count +1");
  626. } else {
  627. $post->praise_count -= 1;
  628. $post->praise_real_count -= 1;
  629. PostLike::where(['uid' => $request['target_id'], 'post_id' => $request['post_id']])->delete();
  630. Redis::srem($postLikeKey, $request['target_id']);
  631. Redis::HINCRBY($postInfoKey, 'praise_count', -1);
  632. Log::debug("帖子:" . $postId . "被取消点赞,praise_count -1");
  633. }
  634. $row = $post->save();
  635. } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'comment_like') {
  636. //用户点赞评论
  637. $comment = PostComment::where('id', $request['comment_id'])->first();
  638. $postLikeKey = "comment_like_" . $postId;
  639. if ($request['behavior_value']) {
  640. $comment->like_count += 1;
  641. PostCommentLike::create(['uid' => $request['target_id'], 'comment_id' => $request['comment_id']]);
  642. Redis::sadd($postLikeKey, $request['target_id'] . '_' . $request['comment_id']);
  643. Log::debug("评论:" . $request['comment_id'] . "被点赞,like_count +1");
  644. } else {
  645. $comment->like_count -= 1;
  646. PostCommentLike::where(['uid' => $request['target_id'], 'comment_id' => $request['comment_id']])->delete();
  647. Redis::srem($postLikeKey, $request['target_id'] . '_' . $request['comment_id']);
  648. Log::debug("评论:" . $request['comment_id'] . "被取消点赞,like_count -1");
  649. }
  650. $row = $comment->save();
  651. } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'forward') {
  652. $post->share_count += 1;
  653. $post->share_real_count += 1;
  654. Redis::HINCRBY($postInfoKey, 'share_count', 1);
  655. $shareRow = PostShare::where(['uid' => $request['target_id'], 'post_id' => $request['post_id']])->first();
  656. if ($shareRow) {
  657. PostShare::where(['uid' => $request['target_id'], 'post_id' => $request['post_id']])->update(['uid' => $request['target_id'], 'post_id' => $request['post_id']]);
  658. } else {
  659. PostShare::create(['uid' => $request['target_id'], 'post_id' => $request['post_id']]);
  660. }
  661. $row = $post->save();
  662. Log::debug("帖子:" . $postId . "被分享,share_count +1");
  663. } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'comment') {
  664. $post->comment_count += 1;
  665. Redis::HINCRBY($postInfoKey, 'comment_count', 1);
  666. $row = $post->save();
  667. Log::debug("帖子:" . $postId . "被评论,comment_count +1");
  668. } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'collect') {
  669. //用户收藏帖子
  670. $postCollectKey = "post_collect_" . $postId;
  671. if ($request['behavior_value']) {
  672. $post->collect_count += 1;
  673. $post->collect_real_count += 1;
  674. PostCollect::create(['uid' => $request['target_id'], 'post_id' => $request['post_id']]);
  675. Redis::sadd($postCollectKey, $request['target_id']);
  676. Redis::HINCRBY($postInfoKey, 'collect_count', 1);
  677. Log::debug("帖子:" . $postId . "被收藏,collect_count +1");
  678. } else {
  679. $post->collect_count -= 1;
  680. $post->collect_real_count -= 1;
  681. PostCollect::where(['uid' => $request['target_id'], 'post_id' => $request['post_id']])->delete();
  682. Redis::srem($postCollectKey, $request['target_id']);
  683. Redis::HINCRBY($postInfoKey, 'collect_count', -1);
  684. Log::debug("帖子:" . $postId . "被取消收藏,collect_count -1");
  685. }
  686. $row = $post->save();
  687. }
  688. $this->collectPostId($request['post_id']);
  689. return $row;
  690. }
  691. /**
  692. * 收集所有有操作的帖子,存入redis
  693. * 供后续计算帖子权重
  694. * @param $id
  695. */
  696. public function collectPostId($id)
  697. {
  698. $key = "community_calc_post_score";
  699. Redis::sadd($key, $id);
  700. Log::debug('存入帖子' . $id . '到权重列表');
  701. }
  702. /**
  703. * 话题详情
  704. */
  705. public function topicDetail($id)
  706. {
  707. return $this->topic
  708. ->find($id);
  709. }
  710. /**
  711. * 获取话题
  712. */
  713. public function getTopic($ids)
  714. {
  715. $topics = $this->topic
  716. ->whereIn('id', explode(',', $ids))
  717. ->orderByRaw(DB::raw("FIELD(id,{$ids})"))
  718. ->get();
  719. $data = [];
  720. foreach ($topics as $topic) {
  721. $data[] = [
  722. 'id' => $topic->id,
  723. 'name' => $topic->name,
  724. 'img' => $topic->img,
  725. 'follow_count' => getNumber($topic->use_count + $topic->base_count),
  726. ];
  727. }
  728. return $data;
  729. }
  730. /**
  731. * 获取内容视频组
  732. */
  733. public function getPostVideo($ids)
  734. {
  735. $posts = $this->post
  736. ->select('id', 'img', 'uid')
  737. ->whereIn('id', explode(',', $ids))
  738. ->where('type', 'video')
  739. ->get();
  740. foreach ($posts as &$post) {
  741. $user = $this->userInfo($post->uid);
  742. $post->username = $user['username'];
  743. $post->avatar = $user['avatar'];
  744. }
  745. return $posts;
  746. }
  747. //用户内容数,转发数,收藏数统计
  748. public function memberPostStatistics($uid)
  749. {
  750. $postCount = $this->post->where('uid', $uid)->count();
  751. $postCollectCount = $this->postCollect->where('uid', $uid)->count();
  752. $postShareCount = $this->postShare->where('uid', $uid)->count();
  753. $data = ['post_count' => $postCount, 'share_count' => $postShareCount, 'collect_count' => $postCollectCount];
  754. return jsonSuccess($data);
  755. }
  756. /**
  757. * 删除内容
  758. */
  759. public function delete($request)
  760. {
  761. //验证用户信息
  762. $userInfo = $this->getUserInfo();
  763. if (empty($userInfo)) {
  764. return jsonError('获取用户信息失败');
  765. }
  766. $post = $this->post->find($request['id']);
  767. if (!$post) {
  768. return jsonError('获取内容信息失败');
  769. }
  770. if ($post->uid != $userInfo['uid']) {
  771. return jsonError('只能删除自己发布的内容');
  772. }
  773. $logData = [
  774. 'uid' => $userInfo['uid'],
  775. 'operator_type' => 'user',
  776. 'post_id' => $request['id'],
  777. 'username' => $userInfo['username'],
  778. 'log_type' => 'delete',
  779. 'content' => json_encode(['delete' => $request['id']]),
  780. ];
  781. DB::beginTransaction();
  782. try {
  783. $post->delete();
  784. $this->postLog->create($logData);
  785. DB::commit();
  786. Redis::SADD('delete_post_ids', $request['id']);
  787. Log::debug('删除内容失败:' . $request['id']);
  788. return jsonSuccess('删除内容成功');
  789. } catch (QueryException $exception) {
  790. DB::rollBack();
  791. Log::debug('删除内容失败:' . $request['id'] . $exception->getMessage());
  792. return jsonError('删除内容失败,请重试');
  793. }
  794. }
  795. /**
  796. * 内容评论数
  797. */
  798. public function getCommentCount($id)
  799. {
  800. $commentCount = 0;
  801. $post = $this->post->find($id);
  802. if ($post) {
  803. $commentCount = $post->data->comment_count;
  804. }
  805. return $commentCount;
  806. }
  807. /**
  808. * 内容评论数
  809. */
  810. public function checkImage($img)
  811. {
  812. return $this->detectionService->checkImg($img);
  813. }
  814. }