PostRepositories.php 24 KB

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