PostRepositories.php 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: edz
  5. * Date: 2019-06-10
  6. * Time: 17:53
  7. */
  8. namespace App\Repositories;
  9. use App\Models\Behavior;
  10. use App\Models\CategoryTopic;
  11. use App\Models\MemberFollowTopic;
  12. use App\Models\Post;
  13. use App\Models\PostCollect;
  14. use App\Models\PostComment;
  15. use App\Models\PostCommentLike;
  16. use App\Models\PostData;
  17. use App\Models\PostDislike;
  18. use App\Models\PostImgs;
  19. use App\Models\PostLike;
  20. use App\Models\PostLog;
  21. use App\Models\PostShare;
  22. use App\Models\Topic;
  23. use App\Service\AliYunVodService;
  24. use App\Service\DetectionService;
  25. use App\Service\RabbitMqUtil;
  26. use App\Traits\CmsTrait;
  27. use App\Traits\PostTrait;
  28. use App\Traits\UserTrait;
  29. use Illuminate\Database\QueryException;
  30. use Illuminate\Support\Carbon;
  31. use Illuminate\Support\Facades\Log;
  32. use Illuminate\Support\Facades\Redis;
  33. use Illuminate\Support\Facades\DB;
  34. use Tymon\JWTAuth\Facades\JWTAuth;
  35. class PostRepositories
  36. {
  37. use UserTrait;
  38. use PostTrait;
  39. use CmsTrait;
  40. public function __construct(Post $post,
  41. PostData $postData,
  42. PostImgs $postImgs,
  43. PostComment $postComment,
  44. PostCollect $postCollect,
  45. PostShare $postShare,
  46. PostLog $postLog,
  47. DetectionService $detectionService,
  48. AliYunVodService $aliYunVodService,
  49. MemberFollowTopic $memberFollowTopic,
  50. CategoryTopic $categoryTopic,
  51. Topic $topic)
  52. {
  53. $this->post = $post;
  54. $this->postData = $postData;
  55. $this->postImgs = $postImgs;
  56. $this->postComment = $postComment;
  57. $this->postCollect = $postCollect;
  58. $this->postShare = $postShare;
  59. $this->postLog = $postLog;
  60. $this->detectionService = $detectionService;
  61. $this->topic = $topic;
  62. $this->memberFollowTopic = $memberFollowTopic;
  63. $this->categoryTopic = $categoryTopic;
  64. $this->aliYunVodService = $aliYunVodService;
  65. }
  66. /**
  67. * 发布内容
  68. */
  69. public function create($request)
  70. {
  71. //验证小号
  72. $userInfo = $this->getUserInfo();
  73. if (empty($userInfo)) {
  74. return jsonError('获取用户信息失败');
  75. }
  76. if (!$userInfo['sns_status']) {
  77. return jsonError('您已被禁言');
  78. }
  79. $isValid = 0;
  80. if ($userInfo['strength']) {
  81. $isValid = 1;
  82. }
  83. $oneHourTime = Carbon::now()->addHours(-1)->toDateTimeString();
  84. $oneHourPostCount = $this->post->where('uid', $userInfo['uid'])->where('created_at', '>', $oneHourTime)->count();
  85. if ($oneHourPostCount > 5) {
  86. return jsonError('创作欲望太强啦,休息一下,看看其他用户的内容吧!');
  87. }
  88. $detectionText = strip_tags($request['title']) . ',' . strip_tags($request['content']);
  89. $detectionTextResult = $this->detectionService->checkText($detectionText);
  90. if ($detectionTextResult['code'] < 0) {
  91. return jsonError('内容违规,请修正哦');
  92. }
  93. $topicIds = json_decode($request['topic_ids'], true);
  94. $topicCount = count($topicIds);
  95. if ($topicCount == 0 || $topicCount > 2) {
  96. return jsonError('所选话题必须1-2个');
  97. }
  98. //验证话题
  99. $hasTopicCount = $this->topic->whereIn('id', $topicIds)->count();
  100. if ($topicCount != $hasTopicCount) {
  101. Log::error('所选话题非法' . $request['topic_ids']);
  102. return jsonError('所选话题非法');
  103. }
  104. $imgs = [];
  105. if ($request['type'] == 'image') {
  106. $imgs = json_decode($request['imgs'], true);
  107. $imgCount = count($imgs);
  108. if ($imgCount == 0 || $imgCount > 9) {
  109. return jsonError('所传图集必须1-9个');
  110. }
  111. }
  112. $allImg = array_merge($imgs, [$request['img']]);
  113. foreach ($allImg as &$img) {
  114. $img = $img . '&x-oss-process=image/resize,p_50/quality,Q_50';
  115. }
  116. $detectionImageResult = $this->detectionService->checkImg($allImg);
  117. if ($detectionImageResult['code'] < 0) {
  118. Log::debug('图片违规,请修正哦' . json_encode($detectionImageResult));
  119. return jsonError('图片违规,请修正哦');
  120. }
  121. //如果为html时候,则imgs只是去把富文本的图片拿去鉴黄,但不做其他处理
  122. if ($request['type'] == 'html') {
  123. $imgs = [];
  124. }
  125. $videoUrl = "";
  126. $videoId = "";
  127. if (isset($request['video']) && $request['video']) {
  128. $videoId = $request['video'];
  129. for ($i = 0; $i < 3; $i++) {
  130. $videoUrl = $this->aliYunVodService->getPlayUrlByVideoId($request['video']);
  131. Log::debug('video-url:' . $videoUrl);
  132. if ($videoUrl) {
  133. break;
  134. }
  135. }
  136. if (empty($videoUrl)) {
  137. return jsonError('发布失败,请重试');
  138. }
  139. }
  140. $fresh = (Carbon::now()->timestamp) - (Carbon::parse("2019-05-01 00:00:00")->timestamp);
  141. $score = $fresh / 43200;
  142. $data = [
  143. 'uid' => $userInfo['uid'],
  144. 'username' => $userInfo['username'],
  145. 'mobile' => $userInfo['mobile'],
  146. 'avatar' => $userInfo['avatar'] ?? '',
  147. 'type' => $request['type'],
  148. 'img' => $request['img'],
  149. 'video' => $videoUrl,
  150. 'video_id' => $videoId,
  151. 'topic_ids' => implode(',', $topicIds),
  152. 'title' => isset($request['title']) ? strip_tags($request['title']) : '',
  153. 'content' => $request['content'],
  154. 'lat' => isset($request['lat']) ? number_format($request['lat'],8) : 0,
  155. 'lng' => isset($request['lng']) ? number_format($request['lng'],8) : 0,
  156. 'location' => isset($request['location']) ? strip_tags($request['location']) : '',
  157. 'is_suggest' => 0,
  158. 'is_hide' => 0,
  159. 'weight' => $score
  160. ];
  161. $date = date('Y-m-d H:i:s');
  162. DB::beginTransaction();
  163. try {
  164. $post = $this->post->create($data);
  165. $postData = $this->postData->create([
  166. 'post_id' => $post->id,
  167. 'pv' => 0,
  168. 'pv_real' => 0,
  169. 'dislike_count' => 0,
  170. 'praise_count' => 0,
  171. 'praise_real_count' => 0,
  172. 'share_count' => 0,
  173. 'share_real_count' => 0,
  174. 'comment_count' => 0,
  175. 'collect_count' => 0,
  176. 'collect_real_count' => 0,
  177. 'available_bean' => $this->availableBean(),
  178. 'will_collect_bean' => rand(100, 200),
  179. 'collect_bean' => 0
  180. ]);
  181. if ($imgs) {
  182. $imgData = [];
  183. foreach ($imgs as $img) {
  184. $imgData[] = [
  185. 'post_id' => $post->id,
  186. 'img' => $img,
  187. 'created_at' => $date,
  188. 'updated_at' => $date
  189. ];
  190. }
  191. $this->postImgs->insert($imgData);
  192. }
  193. DB::commit();
  194. Redis::zadd('post_trigger_type', $isValid, $post->id);
  195. foreach ($topicIds as $id) {
  196. Redis::zincrby('topic.user_uid' . $userInfo['uid'], 1, $id);
  197. Redis::zincrby('topic.just_use_count', 1, $id);
  198. }
  199. Redis::HSET('post_info_' . $post->id,
  200. 'id', $post->id,
  201. 'uid', $post->uid,
  202. 'type', $post->type,
  203. 'img', $post->img,
  204. 'imgs', json_encode($imgs),
  205. 'video', $post->video,
  206. 'topic_ids', $post->topic_ids,
  207. 'is_fine', $post->is_fine,
  208. 'title', $post->title,
  209. 'content', $post->content,
  210. 'location', $post->location,
  211. 'pv', $postData->pv,
  212. 'dislike_count', $postData->dislike_count,
  213. 'praise_count', $postData->praise_count,
  214. 'share_count', $postData->share_count,
  215. 'comment_count', $postData->comment_count,
  216. 'collect_count', $postData->collect_count,
  217. 'available_bean', $postData->available_bean,
  218. 'will_collect_bean', $postData->will_collect_bean,
  219. 'create_bean', $postData->create_bean,
  220. 'collect_bean', $postData->collect_bean,
  221. 'created_at', $post->created_at);
  222. Log::info('post_create:' . $post->id . ',post_author:' . $post->uid . ',author_ip:' . getClientIp());
  223. $behavior = Behavior::where('behavior_identification', 'publish')->first();
  224. if ('html' == $post->type && $behavior) {
  225. $data = [
  226. "behavior_id" => $behavior['virus_behavior_id'],
  227. "behavior_flag" => "publish",
  228. "post_id" => $post->id,
  229. "post_type" => $post->type,
  230. "post_desc" => $post->title?$post->title:subtext($post->conetnt,100),
  231. "post_cover" => $post->img,
  232. "target_id" => $post->uid,
  233. "action_id" => $post->id, //传帖子ID
  234. ];
  235. Log::debug('h5邀请用户注册' . json_encode($data));
  236. //扔进virus队列
  237. $rabbit = new RabbitMqUtil();
  238. $rabbit->push('virus_add', $data);
  239. }
  240. return jsonSuccess([
  241. 'post_id' => $post->id,
  242. 'h5url' => config('customer.share_post_h5url') . "?post_id={$post->id}&invite_code={$userInfo['invite_code']}",
  243. 'bean' => $postData->available_bean,
  244. ]);
  245. } catch (QueryException $exception) {
  246. DB::rollBack();
  247. Log::debug('发布内容失败:' . $exception->getMessage());
  248. return jsonError('发布内容失败,请重试');
  249. }
  250. }
  251. /**
  252. * 评论&回复
  253. */
  254. public function comment($request)
  255. {
  256. //验证小号
  257. $userInfo = $this->getUserInfo();
  258. if (empty($userInfo)) {
  259. return jsonError('获取用户信息失败');
  260. }
  261. if (!$userInfo['sns_status']) {
  262. return jsonError('您已被禁言');
  263. }
  264. $oneHourTime = Carbon::now()->addHours(-1)->toDateTimeString();
  265. $oneHourCommentCount = $this->postComment->where('uid', $userInfo['uid'])->where('created_at', '>', $oneHourTime)->count();
  266. if ($oneHourCommentCount > 59) {
  267. return jsonError('回复了这么多,休息休息,喝口水吧!');
  268. }
  269. $detectionTextResult = $this->detectionService->checkText($request['content']);
  270. if ($detectionTextResult['code'] < 0) {
  271. return jsonError('内容违规,请修正哦');
  272. }
  273. $post = $this->post->find($request['post_id']);
  274. if (!$post) {
  275. return jsonError('获取内容信息失败');
  276. }
  277. $data = [
  278. 'uid' => $userInfo['uid'],
  279. 'post_id' => $request['post_id'],
  280. 'parent_id' => 0,
  281. 'username' => $userInfo['username'],
  282. 'reply_uid' => 0,
  283. 'reply_username' => '',
  284. 'avatar' => $userInfo['avatar'] ?? '',
  285. 'content' => $request['content'],
  286. 'is_delete' => 0,
  287. ];
  288. if (isset($request['parent_id']) && $request['parent_id'] != 0) {
  289. $comment = $this->postComment->find($request['parent_id']);
  290. if (!$comment || $comment->post_id != $post->id) {
  291. return jsonError('获取评论信息失败');
  292. }
  293. if ($comment->parent_id) {
  294. return jsonError('只能回复评论');
  295. }
  296. if ($comment->is_delete) {
  297. return jsonError('不能回复已删除评论');
  298. }
  299. $data['parent_id'] = $request['parent_id'];
  300. if (isset($request['reply_uid']) && isset($request['reply_username'])) {
  301. $data['reply_uid'] = $request['reply_uid'];
  302. $data['reply_username'] = $request['reply_username'];
  303. } else {
  304. $data['reply_uid'] = 0;
  305. $data['reply_username'] = '';
  306. }
  307. }
  308. DB::beginTransaction();
  309. try {
  310. $newComment = $this->postComment->create($data);
  311. if ($newComment->parent_id) {
  312. $this->postComment->where('id', $newComment->parent_id)->increment('reply_count');
  313. }
  314. DB::commit();
  315. if ($newComment->parent_id) {
  316. Redis::DEL('post_new_reply_' . $newComment->parent_id);
  317. } else {
  318. Redis::DEL('post_new_comment_' . $newComment->post_id);
  319. }
  320. return jsonSuccess(['id' => $newComment->id], '评论成功');
  321. } catch (QueryException $exception) {
  322. DB::rollBack();
  323. Log::debug('评论内容失败:' . $exception->getMessage());
  324. return jsonError('评论内容失败,请重试');
  325. }
  326. }
  327. /**
  328. * 删除评论
  329. */
  330. public function commentDelete($request)
  331. {
  332. $userInfo = $this->getUserInfo();
  333. if (empty($userInfo)) {
  334. return jsonError('获取用户信息失败');
  335. }
  336. $comment = $this->postComment->find($request['id']);
  337. if (!$comment) {
  338. return jsonError('获取评论信息失败');
  339. }
  340. if ($userInfo['uid'] != $comment->uid) {
  341. return jsonError('只能删除自己的评论');
  342. }
  343. if ($comment->is_delete == 1) {
  344. return jsonError('该评论已经删除');
  345. }
  346. DB::beginTransaction();
  347. try {
  348. $comment->is_delete = 1;
  349. $comment->save();
  350. DB::commit();
  351. if (!$comment->parent_id) {
  352. Redis::DEL('post_new_comment_' . $comment->post_id);
  353. } else {
  354. Redis::DEL('post_new_reply_' . $comment->id);
  355. }
  356. Redis::SADD('delete_post_comment_ids', $comment->id);
  357. return jsonSuccess('删除评论成功');
  358. } catch (QueryException $exception) {
  359. DB::rollBack();
  360. Log::debug('删除评论:' . $request['id'] . $exception->getMessage());
  361. return jsonError('删除评论失败');
  362. }
  363. }
  364. /**
  365. * 内容列表
  366. */
  367. public function lists($request)
  368. {
  369. Log::debug('内容列表' . json_encode($request));
  370. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  371. return $this->post
  372. ->where(function ($query) use ($request) {
  373. if (isset($request['keyword'])) {
  374. $query->where('title', 'like', "%{$request['keyword']}%")
  375. ->orWhere('content', 'like', "%{$request['keyword']}%");
  376. }
  377. })
  378. ->where(function ($query) use ($request) {
  379. if (isset($request['topic_ids']) && $request['topic_ids']) {
  380. $topicIds = json_decode($request['topic_ids'], true);
  381. foreach ($topicIds as $key => $id) {
  382. if ($key == 0) {
  383. $query = $query->whereRaw('FIND_IN_SET(' . $id . ', topic_ids)');
  384. } else {
  385. $query = $query->orWhereRaw('FIND_IN_SET(' . $id . ', topic_ids)');
  386. }
  387. }
  388. }
  389. })
  390. ->orderBy('weight', 'desc')
  391. ->orderBy('id', 'desc')
  392. ->paginate($perPage);
  393. }
  394. /**
  395. * 视频列表
  396. */
  397. public function video($request)
  398. {
  399. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  400. $where = [];
  401. $id = 0;
  402. if (isset($request['id'])) {
  403. $id = $request['id'];
  404. }
  405. $where[] = ['type', 'video'];
  406. if (isset($request['type']) && $request['type']) {
  407. if ($request['type'] == 'hot') {
  408. $ids = Redis::get('hotVideoIds');
  409. if (!$ids) {
  410. $ids = $this->hotVideoIds();
  411. if (!$ids) {
  412. $ids = '';
  413. }
  414. }
  415. Log::debug('热门视频ids' . $ids);
  416. return $this->post
  417. ->select('*', DB::raw("IF (id = {$id},1,0) as sort"))
  418. ->where($where)
  419. ->whereIn('id', explode(',', $ids))
  420. ->orderBy('sort', 'desc')
  421. ->orderByRaw(DB::raw("FIELD(id,{$ids})"))
  422. ->paginate($perPage);
  423. } elseif ($request['type'] == 'one') {
  424. $where[] = ['id', $id];
  425. return $this->post
  426. ->where($where)
  427. ->paginate($perPage);
  428. }
  429. }
  430. return $this->post
  431. ->select('*', DB::raw("IF (id = {$id},1,0) as sort"))
  432. ->where($where)
  433. ->where(function ($query) use ($request) {
  434. if (isset($request['topic_id']) && $request['topic_id']) {
  435. $query->whereRaw('FIND_IN_SET(' . $request['topic_id'] . ', topic_ids)');
  436. }
  437. })
  438. ->orderBy('sort', 'desc')
  439. ->orderBy('weight', 'desc')
  440. ->orderBy('id', 'desc')
  441. ->paginate($perPage);
  442. }
  443. /**
  444. * 个人中心内容列表
  445. */
  446. public function myPost($request, $uid)
  447. {
  448. $type = $request['type'];
  449. $perPage = isset($request['per_page']) ? $request['per_page'] : 18;
  450. $where = [];
  451. if ($type == 'create') {
  452. $where[] = ['post.uid', $uid];
  453. $post = $this->post;
  454. $order = 'post.id';
  455. } elseif ($type == 'collect') {
  456. $post = $this->post->withTrashed()->join('post_collect', 'post_collect.post_id', '=', 'post.id');
  457. $where[] = ['post_collect.uid', $uid];
  458. $order = 'post_collect.id';
  459. } else {
  460. $post = $this->post->withTrashed()->join('post_share', 'post_share.post_id', '=', 'post.id');
  461. $where[] = ['post_share.uid', $uid];
  462. $order = 'post_share.updated_at';
  463. }
  464. return $post
  465. ->select('post.*')
  466. ->where($where)
  467. ->orderBy($order, 'desc')
  468. ->paginate($perPage);
  469. }
  470. /**
  471. * 内容详情
  472. */
  473. public function detail($id)
  474. {
  475. return $this->post
  476. ->find($id);
  477. }
  478. /**
  479. * 内容是否存在
  480. */
  481. public function detailExists($id)
  482. {
  483. return $this->post->where('id', $id)->exists();
  484. }
  485. /**
  486. * 推荐内容列表
  487. */
  488. public function suggestPost($request, $uid)
  489. {
  490. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  491. if ($uid) {
  492. $blacklist = Redis::smembers('blacklist_' . $uid);
  493. } else {
  494. $blacklist = [];
  495. }
  496. $topicIds = [];
  497. if (isset($request['category_id']) && $request['category_id']) {
  498. $topicIds = $this->categoryTopic->where('category_id', $request['category_id'])->pluck('topic_id')->toArray();
  499. }
  500. return $this->post
  501. ->where(function ($query) use ($blacklist) {
  502. if ($blacklist) {
  503. $query->whereNotIn('uid', $blacklist);
  504. }
  505. })
  506. ->where(function ($query) use ($topicIds) {
  507. if ($topicIds) {
  508. foreach ($topicIds as $key => $id) {
  509. if ($key == 0) {
  510. $query = $query->whereRaw('FIND_IN_SET(' . $id . ', topic_ids)');
  511. } else {
  512. $query = $query->orWhereRaw('FIND_IN_SET(' . $id . ', topic_ids)');
  513. }
  514. }
  515. }
  516. })
  517. ->where('is_hide', 0)
  518. ->orderBy('is_suggest', 'desc')
  519. ->orderBy('weight', 'desc')
  520. ->orderBy('id', 'desc')
  521. ->paginate($perPage);
  522. }
  523. /**
  524. * 话题内容
  525. */
  526. public function topicPost($request)
  527. {
  528. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  529. return $this->post
  530. ->where('is_hide', 0)
  531. ->whereRaw('FIND_IN_SET(' . $request['id'] . ',topic_ids)')
  532. ->orderBy('id', 'desc')
  533. ->paginate($perPage);
  534. }
  535. /**
  536. * 评论列表
  537. */
  538. public function commentList($request)
  539. {
  540. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  541. return $this->postComment
  542. ->where('post_id', $request['post_id'])
  543. ->where('parent_id', 0)
  544. ->orderBy('like_count', 'desc')
  545. ->orderBy('id', 'desc')
  546. ->paginate($perPage);
  547. }
  548. /**
  549. * 回复列表
  550. */
  551. public function replyList($request)
  552. {
  553. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  554. return $this->postComment
  555. ->where('parent_id', $request['id'])
  556. ->orderBy('id', 'desc')
  557. ->paginate($perPage);
  558. }
  559. /**
  560. * 根据评论id获取内容id
  561. */
  562. public function getPostId($commentId)
  563. {
  564. return $this->postComment
  565. ->where('id', $commentId)
  566. ->value('post_id');
  567. }
  568. /**
  569. * 话题列表
  570. */
  571. public function topicList($request)
  572. {
  573. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  574. $where = [];
  575. $topic = $this->topic;
  576. if (isset($request['name'])) {
  577. $where[] = ['topic.name', 'like', "%{$request['name']}%"];
  578. }
  579. if (isset($request['category_id']) && $request['category_id']) {
  580. if ($request['category_id'] == -2) {
  581. $where[] = ['topic.is_hot', 1];
  582. } else {
  583. $topic = $topic->join('category_topic', 'category_topic.topic_id', '=', 'topic.id')->select('topic.*', 'category_topic.id as cid');
  584. $where[] = ['category_topic.category_id', $request['category_id']];
  585. $where[] = ['topic.is_open', 1];
  586. return $topic
  587. ->where($where)
  588. ->orderBy('cid', 'asc')
  589. ->paginate($perPage);
  590. }
  591. }
  592. return $topic
  593. ->where($where)
  594. ->orderBy('id', 'desc')
  595. ->paginate($perPage);
  596. }
  597. /**
  598. * 我的话题列表
  599. */
  600. public function myTopicList($request)
  601. {
  602. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  603. $uid = 0;
  604. $user = $this->getUserInfo();
  605. if ($user) {
  606. $uid = $user['uid'];
  607. }
  608. return $this->memberFollowTopic
  609. ->where('uid', $uid)
  610. ->orderBy('id', 'desc')
  611. ->paginate($perPage);
  612. }
  613. /**
  614. * 更新帖子统计数量
  615. * @param $request
  616. * @return mixed
  617. */
  618. public function updatePostData($request)
  619. {
  620. $postId = isset($request['post_id']) ? $request['post_id'] : 0;
  621. if (empty($postId)) {
  622. Log::debug("非帖子类操作,不操作帖子统计数量" . json_encode($request));
  623. return true;
  624. }
  625. //帖子缓存信息key
  626. $postInfoKey = "post_info_" . $postId;
  627. $post = PostData::where('post_id', $postId)->first();
  628. if (!$post) return true;
  629. $row = false;
  630. if (isset($request['behavior_flag']) && $request['behavior_flag'] == 'read') {
  631. $post->pv += 1;
  632. $post->pv_real += 1;
  633. Redis::HINCRBY($postInfoKey, 'pv', 1);
  634. $topicIdStr = Redis::HGET($postInfoKey, 'topic_ids');
  635. if ($topicIdStr) {
  636. Log::debug('话题增加浏览量' . $topicIdStr);
  637. $topicIds = explode(',', $topicIdStr);
  638. foreach ($topicIds as $id) {
  639. $this->topic->where('id', $id)->increment('pv');
  640. }
  641. }
  642. $row = $post->save();
  643. Log::debug("帖子:" . $postId . "被阅读,pv +1");
  644. } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'unlike') {
  645. //用户不喜欢帖子key
  646. $postUnLikeKey = "post_unlike_" . $postId;
  647. $post->dislike_count += 1;
  648. PostDislike::create(['uid' => $request['target_id'], 'post_id' => $request['post_id']]);
  649. Redis::sadd($postUnLikeKey, $request['target_id']);
  650. Redis::HINCRBY($postInfoKey, 'dislike_count', 1);
  651. $row = $post->save();
  652. Log::debug("帖子:" . $postId . "被不喜欢,unlike +1");
  653. } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'like') {
  654. //用户点赞帖子
  655. $postLikeKey = "post_like_" . $postId;
  656. if ($request['behavior_value']) {
  657. $post->praise_count += 1;
  658. $post->praise_real_count += 1;
  659. PostLike::create(['uid' => $request['target_id'], 'post_id' => $request['post_id']]);
  660. Redis::sadd($postLikeKey, $request['target_id']);
  661. Redis::HINCRBY($postInfoKey, 'praise_count', 1);
  662. Log::debug("帖子:" . $postId . "被点赞,praise_count +1");
  663. } else {
  664. $post->praise_count -= 1;
  665. $post->praise_real_count -= 1;
  666. PostLike::where(['uid' => $request['target_id'], 'post_id' => $request['post_id']])->delete();
  667. Redis::srem($postLikeKey, $request['target_id']);
  668. Redis::HINCRBY($postInfoKey, 'praise_count', -1);
  669. Log::debug("帖子:" . $postId . "被取消点赞,praise_count -1");
  670. }
  671. $row = $post->save();
  672. } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'comment_like') {
  673. //用户点赞评论
  674. $comment = PostComment::where('id', $request['comment_id'])->first();
  675. $postLikeKey = "comment_like_" . $postId;
  676. if ($request['behavior_value']) {
  677. $comment->like_count += 1;
  678. PostCommentLike::create(['uid' => $request['target_id'], 'comment_id' => $request['comment_id']]);
  679. Redis::zadd($postLikeKey, $request['comment_id'], $request['target_id'] . '_' . $request['comment_id']);
  680. Log::debug("评论:" . $request['comment_id'] . "被点赞,like_count +1");
  681. } else {
  682. $comment->like_count -= 1;
  683. PostCommentLike::where(['uid' => $request['target_id'], 'comment_id' => $request['comment_id']])->delete();
  684. Redis::zrem($postLikeKey, $request['target_id'] . '_' . $request['comment_id']);
  685. Log::debug("评论:" . $request['comment_id'] . "被取消点赞,like_count -1");
  686. }
  687. $row = $comment->save();
  688. } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'forward') {
  689. $post->share_count += 1;
  690. $post->share_real_count += 1;
  691. Redis::HINCRBY($postInfoKey, 'share_count', 1);
  692. $shareRow = PostShare::where(['uid' => $request['target_id'], 'post_id' => $request['post_id']])->first();
  693. if ($shareRow) {
  694. PostShare::where(['uid' => $request['target_id'], 'post_id' => $request['post_id']])->update(['uid' => $request['target_id'], 'post_id' => $request['post_id']]);
  695. } else {
  696. PostShare::create(['uid' => $request['target_id'], 'post_id' => $request['post_id']]);
  697. }
  698. $row = $post->save();
  699. Log::debug("帖子:" . $postId . "被分享,share_count +1");
  700. } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'comment') {
  701. $post->comment_count += 1;
  702. Redis::HINCRBY($postInfoKey, 'comment_count', 1);
  703. $row = $post->save();
  704. Log::debug("帖子:" . $postId . "被评论,comment_count +1");
  705. } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'collect') {
  706. //用户收藏帖子
  707. $postCollectKey = "post_collect_" . $postId;
  708. if ($request['behavior_value']) {
  709. $post->collect_count += 1;
  710. $post->collect_real_count += 1;
  711. PostCollect::create(['uid' => $request['target_id'], 'post_id' => $request['post_id']]);
  712. Redis::sadd($postCollectKey, $request['target_id']);
  713. Redis::HINCRBY($postInfoKey, 'collect_count', 1);
  714. Log::debug("帖子:" . $postId . "被收藏,collect_count +1");
  715. } else {
  716. $post->collect_count -= 1;
  717. $post->collect_real_count -= 1;
  718. PostCollect::where(['uid' => $request['target_id'], 'post_id' => $request['post_id']])->delete();
  719. Redis::srem($postCollectKey, $request['target_id']);
  720. Redis::HINCRBY($postInfoKey, 'collect_count', -1);
  721. Log::debug("帖子:" . $postId . "被取消收藏,collect_count -1");
  722. }
  723. $row = $post->save();
  724. }
  725. $this->collectPostId($request['post_id']);
  726. return $row;
  727. }
  728. /**
  729. * 收集所有有操作的帖子,存入redis
  730. * 供后续计算帖子权重
  731. * @param $id
  732. */
  733. public function collectPostId($id)
  734. {
  735. $key = "community_calc_post_score";
  736. Redis::sadd($key, $id);
  737. Log::debug('存入帖子' . $id . '到权重列表');
  738. }
  739. /**
  740. * 话题详情
  741. */
  742. public function topicDetail($id)
  743. {
  744. return $this->topic
  745. ->find($id);
  746. }
  747. /**
  748. * 获取话题
  749. */
  750. public function getTopic($ids)
  751. {
  752. $topics = $this->topic
  753. ->whereIn('id', explode(',', $ids))
  754. ->orderByRaw(DB::raw("FIELD(id,{$ids})"))
  755. ->get();
  756. $data = [];
  757. foreach ($topics as $topic) {
  758. $data[] = [
  759. 'id' => $topic->id,
  760. 'name' => $topic->name,
  761. 'img' => $topic->img,
  762. 'follow_count' => getNumber($topic->use_count + $topic->base_count),
  763. ];
  764. }
  765. return $data;
  766. }
  767. /**
  768. * 获取内容视频组
  769. */
  770. public function getPostVideo($ids)
  771. {
  772. $posts = $this->post
  773. ->select('id', 'img', 'uid')
  774. ->whereIn('id', explode(',', $ids))
  775. ->where('type', 'video')
  776. ->get();
  777. foreach ($posts as &$post) {
  778. $user = $this->userInfo($post->uid);
  779. $post->username = $user['username'];
  780. $post->avatar = $user['avatar'];
  781. }
  782. return $posts;
  783. }
  784. //用户内容数,转发数,收藏数统计
  785. public function memberPostStatistics($uid)
  786. {
  787. $postCount = $this->post->where('uid', $uid)->count();
  788. $postCollectCount = $this->postCollect->where('uid', $uid)->count();
  789. $postShareCount = $this->postShare->where('uid', $uid)->count();
  790. $data = ['post_count' => $postCount, 'share_count' => $postShareCount, 'collect_count' => $postCollectCount];
  791. return jsonSuccess($data);
  792. }
  793. /**
  794. * 删除内容
  795. */
  796. public function delete($request)
  797. {
  798. //验证用户信息
  799. $userInfo = $this->getUserInfo();
  800. if (empty($userInfo)) {
  801. return jsonError('获取用户信息失败');
  802. }
  803. $post = $this->post->find($request['id']);
  804. if (!$post) {
  805. return jsonError('获取内容信息失败');
  806. }
  807. if ($post->uid != $userInfo['uid']) {
  808. return jsonError('只能删除自己发布的内容');
  809. }
  810. $logData = [
  811. 'uid' => $userInfo['uid'],
  812. 'operator_type' => 'user',
  813. 'post_id' => $request['id'],
  814. 'username' => $userInfo['username'],
  815. 'log_type' => 'delete',
  816. 'content' => json_encode(['delete' => $request['id']]),
  817. ];
  818. DB::beginTransaction();
  819. try {
  820. $post->delete();
  821. $this->postLog->create($logData);
  822. DB::commit();
  823. Redis::SADD('delete_post_ids', $request['id']);
  824. Log::debug('删除内容失败:' . $request['id']);
  825. return jsonSuccess('删除内容成功');
  826. } catch (QueryException $exception) {
  827. DB::rollBack();
  828. Log::debug('删除内容失败:' . $request['id'] . $exception->getMessage());
  829. return jsonError('删除内容失败,请重试');
  830. }
  831. }
  832. /**
  833. * 内容评论数
  834. */
  835. public function getCommentCount($id)
  836. {
  837. $commentCount = 0;
  838. $post = $this->post->find($id);
  839. if ($post) {
  840. $commentCount = $post->data->comment_count;
  841. }
  842. return $commentCount;
  843. }
  844. /**
  845. * 内容评论数
  846. */
  847. public function checkImage($img)
  848. {
  849. return $this->detectionService->checkImg($img);
  850. }
  851. /**
  852. * 发布内容--程序对接使用
  853. */
  854. public function createPost($request)
  855. {
  856. //验证小号
  857. $isValid = 1;
  858. $detectionText = strip_tags($request['title']) . ',' . strip_tags($request['content']);
  859. $detectionTextResult = $this->detectionService->checkText($detectionText);
  860. if ($detectionTextResult['code'] < 0) {
  861. return jsonError('内容违规,请修正哦');
  862. }
  863. $topicIds = json_decode($request['topic_ids'], true);
  864. $topicCount = count($topicIds);
  865. if ($topicCount == 0 || $topicCount > 2) {
  866. return jsonError('所选话题必须1-2个');
  867. }
  868. //验证话题
  869. $hasTopicCount = $this->topic->whereIn('id', $topicIds)->count();
  870. if ($topicCount != $hasTopicCount) {
  871. Log::error('所选话题非法' . $request['topic_ids']);
  872. return jsonError('所选话题非法');
  873. }
  874. $imgs = [];
  875. if ($request['type'] == 'image') {
  876. $imgs = json_decode($request['imgs'], true);
  877. $imgCount = count($imgs);
  878. if ($imgCount == 0 || $imgCount > 9) {
  879. return jsonError('所传图集必须1-9个');
  880. }
  881. }
  882. $allImg = array_merge($imgs, [$request['img']]);
  883. foreach ($allImg as &$img) {
  884. $img = $img . '&x-oss-process=image/resize,p_50/quality,Q_50';
  885. }
  886. $detectionImageResult = $this->detectionService->checkImg($allImg);
  887. if ($detectionImageResult['code'] < 0) {
  888. Log::debug('图片违规,请修正哦' . json_encode($detectionImageResult));
  889. return jsonError('图片违规,请修正哦');
  890. }
  891. //如果为html时候,则imgs只是去把富文本的图片拿去鉴黄,但不做其他处理
  892. if ($request['type'] == 'html') {
  893. $imgs = [];
  894. }
  895. $videoUrl = "";
  896. $videoId = "";
  897. if (isset($request['video']) && $request['video']) {
  898. $videoId = $request['video'];
  899. for ($i = 0; $i < 3; $i++) {
  900. $videoUrl = $this->aliYunVodService->getPlayUrlByVideoId($request['video']);
  901. Log::debug('video-url:' . $videoUrl);
  902. if ($videoUrl) {
  903. break;
  904. }
  905. }
  906. if (empty($videoUrl)) {
  907. return jsonError('发布失败,请重试');
  908. }
  909. }
  910. $fresh = (Carbon::now()->timestamp) - (Carbon::parse("2019-05-01 00:00:00")->timestamp);
  911. $score = $fresh / 43200;
  912. $data = [
  913. 'uid' => $request['uid'],
  914. 'username' => $request['username'],
  915. 'mobile' => $request['mobile'],
  916. 'avatar' => $request['avatar'] ?? '',
  917. 'type' => $request['type'],
  918. 'img' => $request['img'],
  919. 'video' => $videoUrl,
  920. 'video_id' => $videoId,
  921. 'topic_ids' => implode(',', $topicIds),
  922. 'title' => isset($request['title']) ? strip_tags($request['title']) : '',
  923. 'content' => $request['content'],
  924. 'lat' => isset($request['lat']) ? number_format($request['lat'],8) : 0,
  925. 'lng' => isset($request['lng']) ? number_format($request['lng'],8) : 0,
  926. 'location' => isset($request['location']) ? strip_tags($request['location']) : '',
  927. 'is_suggest' => 0,
  928. 'is_hide' => 0,
  929. 'weight' => $score
  930. ];
  931. $date = date('Y-m-d H:i:s');
  932. DB::beginTransaction();
  933. try {
  934. $post = $this->post->create($data);
  935. $postData = $this->postData->create([
  936. 'post_id' => $post->id,
  937. 'pv' => 0,
  938. 'pv_real' => 0,
  939. 'dislike_count' => 0,
  940. 'praise_count' => 0,
  941. 'praise_real_count' => 0,
  942. 'share_count' => 0,
  943. 'share_real_count' => 0,
  944. 'comment_count' => 0,
  945. 'collect_count' => 0,
  946. 'collect_real_count' => 0,
  947. 'available_bean' => $this->availableBean(),
  948. 'will_collect_bean' => rand(100, 200),
  949. 'collect_bean' => 0
  950. ]);
  951. if ($imgs) {
  952. $imgData = [];
  953. foreach ($imgs as $img) {
  954. $imgData[] = [
  955. 'post_id' => $post->id,
  956. 'img' => $img,
  957. 'created_at' => $date,
  958. 'updated_at' => $date
  959. ];
  960. }
  961. $this->postImgs->insert($imgData);
  962. }
  963. DB::commit();
  964. Redis::zadd('post_trigger_type', $isValid, $post->id);
  965. foreach ($topicIds as $id) {
  966. Redis::zincrby('topic.user_uid' . $request['uid'], 1, $id);
  967. Redis::zincrby('topic.just_use_count', 1, $id);
  968. }
  969. Redis::HSET('post_info_' . $post->id,
  970. 'id', $post->id,
  971. 'uid', $post->uid,
  972. 'type', $post->type,
  973. 'img', $post->img,
  974. 'imgs', json_encode($imgs),
  975. 'video', $post->video,
  976. 'topic_ids', $post->topic_ids,
  977. 'is_fine', $post->is_fine,
  978. 'title', $post->title,
  979. 'content', $post->content,
  980. 'location', $post->location,
  981. 'pv', $postData->pv,
  982. 'dislike_count', $postData->dislike_count,
  983. 'praise_count', $postData->praise_count,
  984. 'share_count', $postData->share_count,
  985. 'comment_count', $postData->comment_count,
  986. 'collect_count', $postData->collect_count,
  987. 'available_bean', $postData->available_bean,
  988. 'will_collect_bean', $postData->will_collect_bean,
  989. 'create_bean', $postData->create_bean,
  990. 'collect_bean', $postData->collect_bean,
  991. 'created_at', $post->created_at);
  992. Log::info('post_create:' . $post->id . ',post_author:' . $post->uid . ',author_ip:' . getClientIp());
  993. $behavior = Behavior::where('behavior_identification', 'publish')->first();
  994. if ($behavior) {
  995. $data = [
  996. "behavior_id" => $behavior['virus_behavior_id'],
  997. "behavior_flag" => "publish",
  998. "post_id" => $post->id,
  999. "post_type" => $post->type,
  1000. "post_desc" => $post->title?$post->title:subtext($post->conetnt,100),
  1001. "post_cover" => $post->img,
  1002. "target_id" => $post->uid,
  1003. "action_id" => $post->id, //传帖子ID
  1004. ];
  1005. Log::debug('h5邀请用户注册' . json_encode($data));
  1006. //扔进virus队列
  1007. $rabbit = new RabbitMqUtil();
  1008. $rabbit->push('virus_add', $data);
  1009. }
  1010. return jsonSuccess([
  1011. 'post_id' => $post->id,
  1012. 'h5url' => config('customer.share_post_h5url') . "?post_id={$post->id}&invite_code={$request['invite_code']}",
  1013. 'bean' => $postData->available_bean,
  1014. ]);
  1015. } catch (QueryException $exception) {
  1016. DB::rollBack();
  1017. Log::debug('发布内容失败:' . $exception->getMessage());
  1018. return jsonError('发布内容失败,请重试');
  1019. }
  1020. }
  1021. }