PostRepositories.php 28 KB

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