PostRepositories.php 33 KB

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