PostRepository.php 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/5
  6. * Time: 16:03
  7. */
  8. namespace App\Repositories\Post;
  9. use App\Models\Behavior;
  10. use App\Models\CategoryTopic;
  11. use App\Models\Post;
  12. use App\Models\PostComment;
  13. use App\Models\PostData;
  14. use App\Models\PostImgs;
  15. use App\Models\PostLog;
  16. use App\Models\PostStatistics;
  17. use App\Models\Topic;
  18. use App\Service\RabbitMqUtil;
  19. use App\Traits\PostTrait;
  20. use App\Traits\UserTrait;
  21. use Illuminate\Database\QueryException;
  22. use Dingo\Api\Http\Response;
  23. use Illuminate\Support\Carbon;
  24. use Illuminate\Support\Facades\DB;
  25. use Illuminate\Support\Facades\Log;
  26. use Illuminate\Support\Facades\Redis;
  27. use Symfony\Component\HttpKernel\Exception\HttpException;
  28. use Tymon\JWTAuth\Facades\JWTAuth;
  29. use League\Csv\Writer;
  30. use League\Csv\CannotInsertRecord;
  31. class PostRepository
  32. {
  33. use PostTrait;
  34. use UserTrait;
  35. public function __construct(Post $post,
  36. PostData $postData,
  37. PostComment $postComment,
  38. PostImgs $postImgs,
  39. PostLog $postLog,
  40. RabbitMqUtil $rabbitMqUtil,
  41. Behavior $behavior,
  42. CategoryTopic $categoryTopic,
  43. PostStatistics $postStatistics,
  44. Topic $topic)
  45. {
  46. $this->post = $post;
  47. $this->postData = $postData;
  48. $this->postComment = $postComment;
  49. $this->postImgs = $postImgs;
  50. $this->postLog = $postLog;
  51. $this->rabbitMqUtil = $rabbitMqUtil;
  52. $this->behavior = $behavior;
  53. $this->categoryTopic = $categoryTopic;
  54. $this->topic = $topic;
  55. $this->postStatistics = $postStatistics;
  56. }
  57. /**
  58. * 发布内容
  59. */
  60. public function create($request)
  61. {
  62. //富文本标题
  63. if($request['type'] == 'html' && (!isset($request['title']) || !$request['title'])){
  64. return Response::create([
  65. 'message' => '当类型是富文本时,标题不能为空',
  66. 'status_code' => 500
  67. ]);
  68. }
  69. //验证小号
  70. $userInfo = $this->getUserInfo($request['uid']);
  71. Log::debug('发布内容小号信息:' . json_encode($userInfo));
  72. if (!$userInfo || $userInfo['type'] != 1) {
  73. return Response::create([
  74. 'message' => '所选小号信息有误',
  75. 'status_code' => 500
  76. ]);
  77. }
  78. //验证话题
  79. $topicIdsArray = $this->topic->whereIn('id', explode(',', $request['topic_ids']))->pluck('id')->toArray();
  80. $topicCount = count($topicIdsArray);
  81. if ($topicCount == 0 || $topicCount > 5) {
  82. return Response::create([
  83. 'message' => '所选话题必须1-5个',
  84. 'status_code' => 500
  85. ]);
  86. }
  87. $topicIds = implode(',', $topicIdsArray);
  88. //验证内容字数
  89. if($request['type'] != 'html'){
  90. $html = strip_tags($request['content']);
  91. if (mb_strlen($html, 'UTF8') > 1000) {
  92. return Response::create([
  93. 'message' => '所传内容不能超过1000字',
  94. 'status_code' => 500
  95. ]);
  96. }
  97. }
  98. $fresh = (Carbon::now()->timestamp) - (Carbon::parse("2019-05-01 00:00:00")->timestamp);
  99. $score = $fresh / 86400;
  100. $data = [
  101. 'uid' => $userInfo['uid'],
  102. 'username' => $userInfo['username'],
  103. 'mobile' => $userInfo['mobile'],
  104. 'avatar' => $userInfo['avatar'] ?? '',
  105. 'type' => $request['type'],
  106. 'img' => $request['img'],
  107. 'video' => $request['video'] ?? '',
  108. 'video_id' => $request['video_id'] ?? '',
  109. 'topic_ids' => $topicIds,
  110. 'title' => $request['title'] ?? '',
  111. 'content' => $request['content'],
  112. 'location' => $request['location'] ?? '',
  113. 'is_suggest' => $request['is_suggest'],
  114. 'is_hide' => 0,
  115. 'weight' => $score
  116. ];
  117. $date = date('Y-m-d H:i:s');
  118. DB::beginTransaction();
  119. try {
  120. $post = $this->post->create($data);
  121. $postData = $this->postData->create([
  122. 'post_id' => $post->id,
  123. 'pv' => 0,
  124. 'pv_real' => 0,
  125. 'dislike_count' => 0,
  126. 'praise_count' => 0,
  127. 'praise_real_count' => 0,
  128. 'share_count' => 0,
  129. 'share_real_count' => 0,
  130. 'comment_count' => 0,
  131. 'collect_count' => 0,
  132. 'collect_real_count' => 0,
  133. 'available_bean' => $this->availableBean(),
  134. 'will_collect_bean' => rand(100, 200),
  135. 'collect_bean' => 0
  136. ]);
  137. $imgs = [];
  138. if (!empty($request['imgs']) && $request['type'] == 'image') {
  139. $imgData = [];
  140. foreach ($request['imgs'] as $img) {
  141. $imgs[] = $img;
  142. $imgData[] = [
  143. 'post_id' => $post->id,
  144. 'img' => $img,
  145. 'created_at' => $date,
  146. 'updated_at' => $date
  147. ];
  148. }
  149. $this->postImgs->insert($imgData);
  150. }
  151. DB::commit();
  152. Redis::zadd('post_trigger_type', 0, $post->id);
  153. foreach ($topicIdsArray as $id) {
  154. Redis::zincrby('topic.user_uid' . $request['uid'], 1, $id);
  155. }
  156. $virus = $this->behavior->where('behavior_identification', 'publish')->first();
  157. if ($virus) {
  158. if ($post->title) {
  159. $desc = $post->title;
  160. } else {
  161. $desc = subtext(strip_tags($post->content), 20);
  162. }
  163. $this->rabbitMqUtil->push('virus_add', [
  164. 'behavior_id' => $virus->virus_behavior_id,
  165. 'behavior_flag' => 'publish',
  166. 'post_id' => $post->id,
  167. 'post_type' => $post->type,
  168. 'post_desc' => $desc,
  169. 'post_cover' => $post->img,
  170. 'target_id' => $post->uid,
  171. 'action_id' => $post->id,
  172. ]);
  173. }
  174. Redis::HSET('post_info_'.$post->id,
  175. 'id', $post->id,
  176. 'uid', $post->uid,
  177. 'type', $post->type,
  178. 'img', $post->img,
  179. 'imgs', json_encode($imgs),
  180. 'video', $post->video,
  181. 'topic_ids', $post->topic_ids,
  182. 'title', $post->title,
  183. 'content', $post->content,
  184. 'location', $post->location,
  185. 'pv', $postData->pv,
  186. 'dislike_count', $postData->dislike_count,
  187. 'praise_count', $postData->praise_count,
  188. 'share_count', $postData->share_count,
  189. 'comment_count', $postData->comment_count,
  190. 'collect_count', $postData->collect_count,
  191. 'available_bean', $postData->available_bean,
  192. 'will_collect_bean', $postData->will_collect_bean,
  193. 'create_bean', $postData->create_bean,
  194. 'collect_bean', $postData->collect_bean,
  195. 'created_at', $post->created_at);
  196. return Response::create();
  197. } catch (QueryException $exception) {
  198. DB::rollBack();
  199. Log::debug('发布内容:' . $exception->getMessage());
  200. return Response::create([
  201. 'message' => '发布失败,请重试',
  202. 'error' => $exception->getMessage(),
  203. 'status_code' => 500
  204. ]);
  205. }
  206. }
  207. /**
  208. * 增加数据
  209. */
  210. public function addData($request)
  211. {
  212. $token = JWTAuth::decode(JWTAuth::getToken());
  213. if (!$token || $token['type'] != 1) {
  214. return Response::create([
  215. 'message' => '获取登陆信息失败',
  216. 'status_code' => 500
  217. ]);
  218. }
  219. $uid = $token['user']->id;
  220. $username = $token['user']->username;
  221. //验证小号数量
  222. $number = max([
  223. $request['add_pv'],
  224. $request['add_praise_count'],
  225. $request['add_collect_count'],
  226. $request['add_share_count']
  227. ]);
  228. $members = $this->getSystemMember($number);
  229. if (!$members || $members['status_code'] != 200) {
  230. return Response::create([
  231. 'message' => $members['message'],
  232. 'status_code' => 500
  233. ]);
  234. }
  235. $post = $this->post->find($request['post_id']);
  236. $postData = $this->postData->where('post_id', $request['post_id'])->first();
  237. if (!$postData || !$post) {
  238. return Response::create([
  239. 'message' => '获取内容失败',
  240. 'status_code' => 500
  241. ]);
  242. }
  243. if ($request['add_pv'] == 0 && $request['add_praise_count'] == 0 && $request['add_collect_count'] == 0 && $request['add_share_count'] == 0) {
  244. return Response::create([
  245. 'message' => '增加数据不能同时为0',
  246. 'status_code' => 500
  247. ]);
  248. }
  249. $content = [
  250. 'add_pv' => 0,
  251. 'add_praise_count' => 0,
  252. 'add_collect_count' => 0,
  253. 'add_share_count' => 0,
  254. ];
  255. if ($request['add_pv']) {
  256. $postData->pv += $request['add_pv'];
  257. $content['add_pv'] = $request['add_pv'];
  258. }
  259. if ($request['add_praise_count']) {
  260. $postData->praise_count += $request['add_praise_count'];
  261. $content['add_praise_count'] = $request['add_praise_count'];
  262. }
  263. if ($request['add_collect_count']) {
  264. $postData->collect_count += $request['add_collect_count'];
  265. $content['add_collect_count'] = $request['add_collect_count'];
  266. }
  267. if ($request['add_share_count']) {
  268. $postData->share_count += $request['add_share_count'];
  269. $content['add_share_count'] = $request['add_share_count'];
  270. }
  271. DB::beginTransaction();
  272. try {
  273. $postData->save();
  274. $this->postLog->create([
  275. 'post_id' => $request['post_id'],
  276. 'uid' => $uid,
  277. 'username' => $username,
  278. 'log_type' => 'add_data',
  279. 'content' => json_encode($content)
  280. ]);
  281. DB::commit();
  282. $virus = $this->behavior
  283. ->whereIn('behavior_identification', ['read', 'forward', 'like', 'collect'])
  284. ->pluck('virus_behavior_id', 'behavior_identification');
  285. if ($post->title) {
  286. $desc = $post->title;
  287. } else {
  288. $desc = subtext(strip_tags($post->content), 20);
  289. }
  290. $data = [
  291. 'behavior_value' => 1,
  292. 'post_id' => $post->id,
  293. 'post_type' => $post->type,
  294. 'post_author_uid' => $post->uid,
  295. 'post_desc' => $desc,
  296. 'post_cover' => $post->img,
  297. 'action_id' => $post->id,
  298. ];
  299. foreach ($members['data'] as $key => $member) {
  300. if (isset($virus['read']) && $request['add_pv'] > $key) {
  301. $newData = array_merge($data, [
  302. 'behavior_id' => $virus['read'],
  303. 'behavior_flag' => 'read',
  304. 'target_id' => $member['uid'],
  305. ]);
  306. $this->rabbitMqUtil->push('virus_add', $newData);
  307. }
  308. if (isset($virus['like']) && $request['add_praise_count'] > $key) {
  309. $newData = array_merge($data, [
  310. 'behavior_id' => $virus['like'],
  311. 'behavior_flag' => 'like',
  312. 'target_id' => $member['uid'],
  313. ]);
  314. $this->rabbitMqUtil->push('virus_add', $newData);
  315. }
  316. if (isset($virus['collect']) && $request['add_collect_count'] > $key) {
  317. $newData = array_merge($data, [
  318. 'behavior_id' => $virus['collect'],
  319. 'behavior_flag' => 'collect',
  320. 'target_id' => $member['uid'],
  321. ]);
  322. $this->rabbitMqUtil->push('virus_add', $newData);
  323. }
  324. if (isset($virus['forward']) && $request['add_share_count'] > $key) {
  325. $newData = array_merge($data, [
  326. 'behavior_id' => $virus['forward'],
  327. 'behavior_flag' => 'forward',
  328. 'target_id' => $member['uid'],
  329. ]);
  330. $this->rabbitMqUtil->push('virus_add', $newData);
  331. }
  332. }
  333. return Response::create();
  334. } catch (QueryException $exception) {
  335. DB::rollBack();
  336. Log::debug('内容增加数据:' . $request['post_id'] . $exception->getMessage());
  337. return Response::create([
  338. 'message' => '增加数据失败,请重试',
  339. 'error' => $exception->getMessage(),
  340. 'status_code' => 500
  341. ]);
  342. }
  343. }
  344. /**
  345. * 评论&回复
  346. */
  347. public function comment($request)
  348. {
  349. //验证小号
  350. $userInfo = $this->getUserInfo($request['uid']);
  351. Log::debug('评论&回复小号' . json_encode($userInfo));
  352. if (!$userInfo || $userInfo['type'] != 1) {
  353. return Response::create([
  354. 'message' => '所选小号信息有误',
  355. 'status_code' => 500
  356. ]);
  357. }
  358. $post = $this->post->find($request['post_id']);
  359. if (!$post) {
  360. return Response::create([
  361. 'message' => '获取内容失败',
  362. 'status_code' => 500
  363. ]);
  364. }
  365. $data = [
  366. 'uid' => $request['uid'],
  367. 'post_id' => $request['post_id'],
  368. 'parent_id' => 0,
  369. 'username' => $userInfo['username'],
  370. 'reply_uid' => 0,
  371. 'reply_username' => '',
  372. 'avatar' => $userInfo['avatar'] ?? '',
  373. 'content' => $request['content'],
  374. 'is_delete' => 0,
  375. ];
  376. $parentCommentContent = '';
  377. $parentCommentUid = 0;
  378. $parentCommentTime = '';
  379. if (isset($request['parent_id']) && $request['parent_id'] != 0) {
  380. $comment = $this->postComment->find($request['parent_id']);
  381. if (!$comment) {
  382. return Response::create([
  383. 'message' => '获取评论信息失败',
  384. 'status_code' => 500
  385. ]);
  386. }
  387. if ($comment->parent_id) {
  388. return Response::create([
  389. 'message' => '只能回复评论',
  390. 'status_code' => 500
  391. ]);
  392. }
  393. $data['parent_id'] = $request['parent_id'];
  394. $data['reply_uid'] = $comment->uid;
  395. $data['reply_username'] = $comment->username;
  396. $parentCommentContent = $comment->content;
  397. $parentCommentUid = $comment->uid;
  398. $parentCommentTime = Carbon::parse($comment->created_at)->toDateTimeString();
  399. }
  400. DB::beginTransaction();
  401. try {
  402. $comment = $this->postComment->create($data);
  403. if($comment->parent_id){
  404. $this->postComment->where('parent_id', $comment->parent_id)->increment('reply_count');
  405. }
  406. DB::commit();
  407. if(!$comment->parent_id){
  408. Redis::DEL('post_new_comment_'.$comment->post_id);
  409. }else{
  410. Redis::DEL('post_new_reply_'.$comment->id);
  411. }
  412. $virus = $this->behavior->where('behavior_identification', 'comment')->first();
  413. if ($virus) {
  414. if ($post->title) {
  415. $desc = $post->title;
  416. } else {
  417. $desc = subtext(strip_tags($post->content), 20);
  418. }
  419. $this->rabbitMqUtil->push('virus_add', [
  420. 'behavior_id' => $virus->virus_behavior_id,
  421. 'behavior_flag' => 'comment',
  422. 'post_id' => $post->id,
  423. 'post_type' => $post->type,
  424. 'post_author_uid' => $post->uid,
  425. 'post_desc' => $desc,
  426. 'post_cover' => $post->img,
  427. 'comment_id' => $comment->id,
  428. 'comment_content' => $comment->content,
  429. 'parent_comment_id' => $comment->parent_id,
  430. 'parent_comment_content' => $parentCommentContent,
  431. 'parent_comment_uid' => $parentCommentUid,
  432. 'parent_comment_time' => $parentCommentTime,
  433. 'reply_uid' => $comment->reply_uid,
  434. 'reply_username' => $comment->reply_username,
  435. 'target_id' => $comment->uid,
  436. 'action_id' => $comment->id,
  437. ]);
  438. }
  439. return Response::create();
  440. } catch (QueryException $exception) {
  441. DB::rollBack();
  442. Log::debug('评论内容:' . $request['post_id'] . $exception->getMessage());
  443. return Response::create([
  444. 'message' => '评论失败,请重试',
  445. 'error' => $exception->getMessage(),
  446. 'status_code' => 500
  447. ]);
  448. }
  449. }
  450. /**
  451. * 内容列表
  452. */
  453. public function lists($request)
  454. {
  455. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  456. $where = [];
  457. if (isset($request['is_suggest'])) {
  458. $where[] = ['is_suggest', $request['is_suggest']];
  459. }
  460. if (isset($request['type'])) {
  461. $where[] = ['type', $request['type']];
  462. }
  463. if (isset($request['uid'])) {
  464. $where[] = ['uid', $request['uid']];
  465. }
  466. $sort = 'post.id';
  467. if (isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])) {
  468. $sort = $request['sort'];
  469. }
  470. $post = $this->post;
  471. if (isset($request['waste']) && $request['waste'] == 1) {
  472. $post = $post->onlyTrashed();
  473. }
  474. return $post
  475. ->join('post_data', 'post_data.post_id', '=', 'post.id')
  476. ->select('post.*')
  477. ->where($where)
  478. ->where(function ($query) use ($request) {
  479. if (isset($request['keyword'])) {
  480. $query->where('uid', '=', $request['keyword'])
  481. ->orWhere('username', 'like', "%{$request['keyword']}%")
  482. ->orWhere('mobile', 'like', "%{$request['keyword']}%");
  483. }
  484. })
  485. ->where(function ($query) use ($request) {
  486. if (isset($request['content'])) {
  487. $query->where('title', 'like', "%{$request['content']}%")
  488. ->orWhere('content', 'like', "%{$request['content']}%");
  489. }
  490. })
  491. ->where(function ($query) use ($request) {
  492. if (isset($request['created_at'])) {
  493. $time = explode('_', $request['created_at']);
  494. $query->whereBetween('post.created_at', $time);
  495. }
  496. })
  497. ->where(function ($query) use ($request) {
  498. if (isset($request['category_ids']) || isset($request['topic_ids'])) {
  499. $ids = [];
  500. if (isset($request['category_ids'])) {
  501. $categoryIds = explode('_', $request['category_ids']);
  502. $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
  503. }
  504. if (isset($request['topic_ids'])) {
  505. $ids = array_merge($ids, explode('_', $request['topic_ids']));
  506. }
  507. foreach ($ids as $key => $id) {
  508. if ($key == 0) {
  509. $query = $query->whereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  510. } else {
  511. $query = $query->orWhereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  512. }
  513. }
  514. }
  515. })
  516. ->orderBy($sort, 'desc')
  517. ->paginate($perPage);
  518. }
  519. /**
  520. * 内容详情
  521. */
  522. public function detail($request)
  523. {
  524. return $this->post->withTrashed()->find($request['id']);
  525. }
  526. /**
  527. * 内容类型
  528. */
  529. public function getType($request)
  530. {
  531. $type = $this->post->where('id', $request['id'])->value('type');
  532. return Response::create([
  533. 'message' => '成功',
  534. 'status_code' => 200,
  535. 'data' => $type
  536. ]);
  537. }
  538. /**
  539. * 评论列表
  540. */
  541. public function commentList($request)
  542. {
  543. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  544. $where = [];
  545. if (isset($request['post_id'])) {
  546. $where[] = ['post_id', $request['post_id']];
  547. }
  548. if (isset($request['uid'])) {
  549. $where[] = ['uid', $request['uid']];
  550. }
  551. return $this->postComment
  552. ->where($where)
  553. ->orderBy('id', 'desc')
  554. ->paginate($perPage);
  555. }
  556. /**
  557. * 推荐内容
  558. */
  559. public function suggest($request)
  560. {
  561. $post = $this->post->where('id', $request['id'])->first();
  562. if (!$post) {
  563. return Response::create([
  564. 'message' => '获取内容信息失败',
  565. 'status_code' => 500
  566. ]);
  567. }
  568. if ($post->is_suggest == 1) {
  569. $post->is_suggest = 0;
  570. } else {
  571. $post->is_suggest = 1;
  572. }
  573. DB::beginTransaction();
  574. try {
  575. $post->save();
  576. DB::commit();
  577. return Response::create();
  578. } catch (QueryException $exception) {
  579. DB::rollBack();
  580. Log::debug('推荐内容:' . $request['id'] . $exception->getMessage());
  581. return Response::create([
  582. 'message' => '操作失败,请重试',
  583. 'error' => $exception->getMessage(),
  584. 'status_code' => 500
  585. ]);
  586. }
  587. }
  588. /**
  589. * 删除内容
  590. */
  591. public function delete($request)
  592. {
  593. $token = JWTAuth::decode(JWTAuth::getToken());
  594. if($token['type'] != 1){
  595. return Response::create([
  596. 'message' => '只有运营才能删除内容',
  597. 'status_code' => 500
  598. ]);
  599. }
  600. $post = $this->post->where('id', $request['id'])->first();
  601. if (!$post) {
  602. return Response::create([
  603. 'message' => '获取内容信息失败',
  604. 'status_code' => 500
  605. ]);
  606. }
  607. $uid = $post->uid;
  608. $title = $post->title;
  609. if(!$title){
  610. $title = subtext(strip_tags($post->content), 20);
  611. }
  612. $content = "经核实您的内容“{$title}”涉及违规,现已被删除,有任何问题请联系由你管理员";
  613. $logData = [
  614. 'uid' => $token['user']->id,
  615. 'operator_type' => 'admin',
  616. 'post_id' => $request['id'],
  617. 'username' => $token['user']->username,
  618. 'log_type' => 'delete',
  619. 'content' => json_encode(['delete' => $request['id']]),
  620. ];
  621. $date = Carbon::now()->toDateTimeString();
  622. DB::beginTransaction();
  623. try {
  624. $post->delete();
  625. $this->postLog->create($logData);
  626. DB::commit();
  627. Redis::SADD('delete_post_ids', $request['id']);
  628. $this->rabbitMqUtil->push('add_message_one', [
  629. 'uid' => $uid,
  630. 'message_rule_id' => 0,
  631. 'message_type' => 1,
  632. 'message_show_type' => 'post_delete',
  633. 'param' => [
  634. 'title' => '内容删除',
  635. 'content' => $content,
  636. 'cover' => '',
  637. 'activity_url' => 0,
  638. 'activity_time' => '',
  639. ],
  640. 'is_read' => 0,
  641. 'created_at' => $date,
  642. 'updated_at' => $date,
  643. ]);
  644. return Response::create();
  645. } catch (QueryException $exception) {
  646. DB::rollBack();
  647. Log::debug('删除内容:' . $request['id'] . $exception->getMessage());
  648. return Response::create([
  649. 'message' => '操作失败,请重试',
  650. 'error' => $exception->getMessage(),
  651. 'status_code' => 500
  652. ]);
  653. }
  654. }
  655. /**
  656. * 复原内容
  657. */
  658. public function restore($request)
  659. {
  660. $post = $this->post->withTrashed()->where('id', $request['id'])->first();
  661. if (!$post) {
  662. return Response::create([
  663. 'message' => '获取内容信息失败',
  664. 'status_code' => 500
  665. ]);
  666. }
  667. DB::beginTransaction();
  668. try {
  669. $post->restore();
  670. DB::commit();
  671. Redis::SREM('delete_post_ids', $request['id']);
  672. return Response::create();
  673. } catch (QueryException $exception) {
  674. DB::rollBack();
  675. Log::debug('复原内容:' . $request['id'] . $exception->getMessage());
  676. return Response::create([
  677. 'message' => '操作失败,请重试',
  678. 'error' => $exception->getMessage(),
  679. 'status_code' => 500
  680. ]);
  681. }
  682. }
  683. /**
  684. * 删除评论
  685. */
  686. public function commentDelete($request)
  687. {
  688. $comment = $this->postComment->find($request['id']);
  689. if (!$comment) {
  690. return Response::create([
  691. 'message' => '获取评论信息失败',
  692. 'status_code' => 500
  693. ]);
  694. }
  695. if ($comment->is_delete == 1) {
  696. return Response::create([
  697. 'message' => '该评论已经删除',
  698. 'status_code' => 500
  699. ]);
  700. }
  701. DB::beginTransaction();
  702. try {
  703. $comment->is_delete = 1;
  704. $comment->save();
  705. DB::commit();
  706. Redis::SADD('delete_post_comment_ids', $comment->id);
  707. if(!$comment->parent_id){
  708. Redis::DEL('post_new_comment_'.$comment->post_id);
  709. }else{
  710. Redis::DEL('post_new_reply_'.$comment->id);
  711. }
  712. return Response::create();
  713. } catch (QueryException $exception) {
  714. DB::rollBack();
  715. Log::debug('删除评论:' . $request['id'] . $exception->getMessage());
  716. return Response::create([
  717. 'message' => '操作失败,请重试',
  718. 'error' => $exception->getMessage(),
  719. 'status_code' => 500
  720. ]);
  721. }
  722. }
  723. /**
  724. * 隐藏内容
  725. */
  726. public function hide($request)
  727. {
  728. $post = $this->post->where('id', $request['id'])->first();
  729. if (!$post) {
  730. return Response::create([
  731. 'message' => '获取内容信息失败',
  732. 'status_code' => 500
  733. ]);
  734. }
  735. if ($post->is_hide == 1) {
  736. $post->is_hide = 0;
  737. } else {
  738. $post->is_hide = 1;
  739. }
  740. DB::beginTransaction();
  741. try {
  742. $post->save();
  743. DB::commit();
  744. return Response::create();
  745. } catch (QueryException $exception) {
  746. DB::rollBack();
  747. Log::debug('隐藏内容:' . $request['id'] . $exception->getMessage());
  748. return Response::create([
  749. 'message' => '操作失败,请重试',
  750. 'error' => $exception->getMessage(),
  751. 'status_code' => 500
  752. ]);
  753. }
  754. }
  755. /**
  756. * 日志列表
  757. */
  758. public function log($request)
  759. {
  760. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  761. $where = [];
  762. if (isset($request['log_type'])) {
  763. $where[] = ['log_type', $request['log_type']];
  764. }
  765. return $this->postLog
  766. ->where($where)
  767. ->where(function ($query) use ($request) {
  768. if (isset($request['created_at'])) {
  769. $time = explode('_', $request['created_at']);
  770. $query->whereBetween('created_at', $time);
  771. }
  772. })
  773. ->orderBy('id', 'desc')
  774. ->paginate($perPage);
  775. }
  776. public function download($filePath, $type, $request)
  777. {
  778. try {
  779. set_time_limit(0);
  780. if (!ini_get("auto_detect_line_endings")) {
  781. ini_set("auto_detect_line_endings", '1');
  782. }
  783. // 文件路径
  784. $writer = Writer::createFromPath(public_path($filePath), 'w+');
  785. // 设置标题
  786. if ($type == 'post') {
  787. $title = [
  788. '内容', date('Y年m月d日')
  789. ];
  790. } else {
  791. $title = [
  792. '回收站内容', date('Y年m月d日')
  793. ];
  794. }
  795. $title = eval('return ' . iconv('utf-8', 'gbk//IGNORE', var_export($title, true) . ';'));
  796. $writer->insertone($title);
  797. // 内容
  798. if ($type == 'post') {
  799. $header = [
  800. '内容ID', '发布时间', '用户昵称', '城市', '内容标签', '内容前20个字',
  801. '真实浏览量', '总浏览量', '真实点赞数', '总赞数', '真实分享数', '总分享数',
  802. '真实收藏数', '总收藏数', '评论数'
  803. ];
  804. } else {
  805. $header = [
  806. '内容ID', '发布时间', '用户昵称', '内容标签', '内容前20个字',
  807. '真实浏览量', '真实点赞数', '真实点赞数', '真实分享数', '真实收藏数', '评论数'
  808. ];
  809. }
  810. $header = eval('return ' . iconv('utf-8', 'gbk//IGNORE', var_export($header, true) . ';'));
  811. // $writer->setOutputBOM(Reader::BOM_UTF8);
  812. $writer->insertone($header);
  813. $where = [];
  814. if (isset($request['content'])) {
  815. $where[] = ['content', 'like', "%{$request['content']}%"];
  816. }
  817. if (isset($request['is_suggest'])) {
  818. $where[] = ['is_suggest', $request['is_suggest']];
  819. }
  820. if (isset($request['type'])) {
  821. $where[] = ['type', $request['type']];
  822. }
  823. $sort = 'post.id';
  824. if (isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])) {
  825. $sort = $request['sort'];
  826. }
  827. $post = $this->post;
  828. if ($type == 'post_waste') {
  829. $post = $post->onlyTrashed();
  830. }
  831. $post->join('post_data', 'post_data.post_id', '=', 'post.id')
  832. ->select('post.*')
  833. ->where($where)
  834. ->where(function ($query) use ($request) {
  835. if (isset($request['keyword'])) {
  836. $query->where('uid', '=', $request['keyword'])
  837. ->orWhere('username', 'like', "%{$request['keyword']}%")
  838. ->orWhere('mobile', 'like', "%{$request['keyword']}%");
  839. }
  840. })
  841. ->where(function ($query) use ($request) {
  842. if (isset($request['created_at'])) {
  843. $time = explode('_', $request['created_at']);
  844. $query->whereBetween('post.created_at', $time);
  845. }
  846. })
  847. ->where(function ($query) use ($request) {
  848. if (isset($request['category_ids']) || isset($request['topic_ids'])) {
  849. $ids = [];
  850. if (isset($request['category_ids'])) {
  851. $categoryIds = explode('_', $request['category_ids']);
  852. $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
  853. }
  854. if (isset($request['topic_ids'])) {
  855. $ids = array_merge($ids, explode('_', $request['topic_ids']));
  856. }
  857. Log::debug('话题ids:' . json_encode($ids));
  858. foreach ($ids as $key => $id) {
  859. if ($key == 0) {
  860. $query = $query->whereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  861. } else {
  862. $query = $query->orWhereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  863. }
  864. }
  865. }
  866. })
  867. ->orderBy($sort, 'desc')
  868. ->chunk(1, function ($posts) use ($writer, $type) {
  869. $data = [];
  870. foreach ($posts as $post) {
  871. if ($type == 'post') {
  872. $tmp = [
  873. $post->id,
  874. Carbon::parse($post->created_at)->toDateTimeString(),
  875. $post->username,
  876. $post->location,
  877. implode(' ', $post->topic()->toArray()),
  878. subtext(strip_tags($post->content), 20),
  879. $post->data->pv_real,
  880. $post->data->pv,
  881. $post->data->praise_real_count,
  882. $post->data->praise_count,
  883. $post->data->share_real_count,
  884. $post->data->share_count,
  885. $post->data->collect_real_count,
  886. $post->data->collect_count,
  887. $post->data->comment_count
  888. ];
  889. } else {
  890. $tmp = [
  891. $post->id,
  892. Carbon::parse($post->created_at)->toDateTimeString(),
  893. $post->username,
  894. Carbon::parse($post->created_at)->toDateTimeString(),
  895. subtext(strip_tags($post->content), 20),
  896. $post->data->pv_real,
  897. $post->data->praise_real_count,
  898. $post->data->share_real_count,
  899. $post->data->collect_real_count,
  900. $post->data->comment_count
  901. ];
  902. }
  903. foreach ($tmp as $key => $value) {
  904. $tmp[$key] = iconv('utf-8', 'gbk//IGNORE', $value);
  905. }
  906. $data[] = $tmp;
  907. }
  908. $writer->insertAll($data);
  909. });
  910. Log::info('内容导出成功!');
  911. } catch (QueryException $e) {
  912. Log::debug('内容导出失败!'.$e->getMessage());
  913. }
  914. }
  915. /**
  916. * 统计社区内容
  917. * @param $start
  918. * @param $end
  919. * @return array
  920. */
  921. public function statistics($start, $end)
  922. {
  923. $result = $this->postStatistics
  924. ->where('created_at', '>=', $start)
  925. ->where('created_at', '<=', $end)
  926. ->get()->toArray();
  927. $stimestamp = strtotime($start);
  928. $etimestamp = strtotime($end);
  929. $days = ($etimestamp - $stimestamp) / 86400;
  930. $date = array();
  931. for ($i = 0; $i < $days; $i++) {
  932. $date[] = date('Y-m-d', $stimestamp + (86400 * $i));
  933. }
  934. $totalRead = 0;
  935. $totalPost = 0;
  936. $totalShare = 0;
  937. $totalLike = 0;
  938. $totalCollect = 0;
  939. $totalComment = 0;
  940. $info = [];
  941. foreach ($date as $key => $value) {
  942. $info[$value] = [
  943. 'read' => 0,
  944. 'post' => 0,
  945. 'share' => 0,
  946. 'like' => 0,
  947. 'collect' => 0,
  948. 'comment' => 0,
  949. ];
  950. foreach ($result as $row) {
  951. if ($value == date('Y-m-d', strtotime($row['created_at']))) {
  952. $info[$value]['read'] = $row['read_count'];
  953. $info[$value]['post'] = $row['post_count'];
  954. $info[$value]['share'] = $row['share_count'];
  955. $info[$value]['like'] = $row['like_count'];
  956. $info[$value]['collect'] = $row['collect_count'];
  957. $info[$value]['comment'] = $row['comment_count'];
  958. $totalRead += $row['read_count'];
  959. $totalPost += $row['post_count'];
  960. $totalShare += $row['share_count'];
  961. $totalLike += $row['like_count'];
  962. $totalCollect += $row['collect_count'];
  963. $totalComment += $row['comment_count'];
  964. }
  965. }
  966. }
  967. $info['data']['total_read'] = $totalRead;
  968. $info['data']['total_post'] = $totalPost;
  969. $info['data']['total_share'] = $totalShare;
  970. $info['data']['total_like'] = $totalLike;
  971. $info['data']['total_collect'] = $totalCollect;
  972. $info['data']['total_comment'] = $totalComment;
  973. return $info;
  974. }
  975. }