PostRepositories.php 33 KB

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