PostRepositories.php 28 KB

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