PostRepositories.php 29 KB

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