PostRepositories.php 30 KB

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