PostRepositories.php 25 KB

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