PostRepository.php 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  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. return Response::create();
  196. } catch (QueryException $exception) {
  197. DB::rollBack();
  198. Log::debug('发布内容:' . $exception->getMessage());
  199. return Response::create([
  200. 'message' => '发布失败,请重试',
  201. 'error' => $exception->getMessage(),
  202. 'status_code' => 500
  203. ]);
  204. }
  205. }
  206. /**
  207. * 增加数据
  208. */
  209. public function addData($request)
  210. {
  211. $token = JWTAuth::decode(JWTAuth::getToken());
  212. if (!$token || $token['type'] != 1) {
  213. return Response::create([
  214. 'message' => '获取登陆信息失败',
  215. 'status_code' => 500
  216. ]);
  217. }
  218. $uid = $token['user']->id;
  219. $username = $token['user']->username;
  220. //验证小号数量
  221. $number = max([
  222. $request['add_pv'],
  223. $request['add_praise_count'],
  224. $request['add_collect_count'],
  225. $request['add_share_count']
  226. ]);
  227. $members = $this->getSystemMember($number);
  228. if (!$members || $members['status_code'] != 200) {
  229. return Response::create([
  230. 'message' => $members['message'],
  231. 'status_code' => 500
  232. ]);
  233. }
  234. $post = $this->post->find($request['post_id']);
  235. $postData = $this->postData->where('post_id', $request['post_id'])->first();
  236. if (!$postData || !$post) {
  237. return Response::create([
  238. 'message' => '获取内容失败',
  239. 'status_code' => 500
  240. ]);
  241. }
  242. if ($request['add_pv'] == 0 && $request['add_praise_count'] == 0 && $request['add_collect_count'] == 0 && $request['add_share_count'] == 0) {
  243. return Response::create([
  244. 'message' => '增加数据不能同时为0',
  245. 'status_code' => 500
  246. ]);
  247. }
  248. $content = [
  249. 'add_pv' => 0,
  250. 'add_praise_count' => 0,
  251. 'add_collect_count' => 0,
  252. 'add_share_count' => 0,
  253. ];
  254. if ($request['add_pv']) {
  255. $postData->pv += $request['add_pv'];
  256. $content['add_pv'] = $request['add_pv'];
  257. }
  258. if ($request['add_praise_count']) {
  259. $postData->praise_count += $request['add_praise_count'];
  260. $content['add_praise_count'] = $request['add_praise_count'];
  261. }
  262. if ($request['add_collect_count']) {
  263. $postData->collect_count += $request['add_collect_count'];
  264. $content['add_collect_count'] = $request['add_collect_count'];
  265. }
  266. if ($request['add_share_count']) {
  267. $postData->share_count += $request['add_share_count'];
  268. $content['add_share_count'] = $request['add_share_count'];
  269. }
  270. DB::beginTransaction();
  271. try {
  272. $postData->save();
  273. $this->postLog->create([
  274. 'post_id' => $request['post_id'],
  275. 'uid' => $uid,
  276. 'username' => $username,
  277. 'log_type' => 'add_data',
  278. 'content' => json_encode($content)
  279. ]);
  280. DB::commit();
  281. $virus = $this->behavior
  282. ->whereIn('behavior_identification', ['read', 'forward', 'like', 'collect'])
  283. ->pluck('virus_behavior_id', 'behavior_identification');
  284. if ($post->title) {
  285. $desc = $post->title;
  286. } else {
  287. $desc = subtext(strip_tags($post->content), 20);
  288. }
  289. $data = [
  290. 'behavior_value' => 1,
  291. 'post_id' => $post->id,
  292. 'post_type' => $post->type,
  293. 'post_author_uid' => $post->uid,
  294. 'post_desc' => $desc,
  295. 'post_cover' => $post->img,
  296. 'action_id' => $post->id,
  297. ];
  298. foreach ($members['data'] as $key => $member) {
  299. if (isset($virus['read']) && $request['add_pv'] > $key) {
  300. $newData = array_merge($data, [
  301. 'behavior_id' => $virus['read'],
  302. 'behavior_flag' => 'read',
  303. 'target_id' => $member['uid'],
  304. ]);
  305. $this->rabbitMqUtil->push('virus_add', $newData);
  306. }
  307. if (isset($virus['like']) && $request['add_praise_count'] > $key) {
  308. $newData = array_merge($data, [
  309. 'behavior_id' => $virus['like'],
  310. 'behavior_flag' => 'like',
  311. 'target_id' => $member['uid'],
  312. ]);
  313. $this->rabbitMqUtil->push('virus_add', $newData);
  314. }
  315. if (isset($virus['collect']) && $request['add_collect_count'] > $key) {
  316. $newData = array_merge($data, [
  317. 'behavior_id' => $virus['collect'],
  318. 'behavior_flag' => 'collect',
  319. 'target_id' => $member['uid'],
  320. ]);
  321. $this->rabbitMqUtil->push('virus_add', $newData);
  322. }
  323. if (isset($virus['forward']) && $request['add_share_count'] > $key) {
  324. $newData = array_merge($data, [
  325. 'behavior_id' => $virus['forward'],
  326. 'behavior_flag' => 'forward',
  327. 'target_id' => $member['uid'],
  328. ]);
  329. $this->rabbitMqUtil->push('virus_add', $newData);
  330. }
  331. }
  332. return Response::create();
  333. } catch (QueryException $exception) {
  334. DB::rollBack();
  335. Log::debug('内容增加数据:' . $request['post_id'] . $exception->getMessage());
  336. return Response::create([
  337. 'message' => '增加数据失败,请重试',
  338. 'error' => $exception->getMessage(),
  339. 'status_code' => 500
  340. ]);
  341. }
  342. }
  343. /**
  344. * 评论&回复
  345. */
  346. public function comment($request)
  347. {
  348. //验证小号
  349. $userInfo = $this->getUserInfo($request['uid']);
  350. Log::debug('评论&回复小号' . json_encode($userInfo));
  351. if (!$userInfo || $userInfo['type'] != 1) {
  352. return Response::create([
  353. 'message' => '所选小号信息有误',
  354. 'status_code' => 500
  355. ]);
  356. }
  357. $post = $this->post->find($request['post_id']);
  358. if (!$post) {
  359. return Response::create([
  360. 'message' => '获取内容失败',
  361. 'status_code' => 500
  362. ]);
  363. }
  364. $data = [
  365. 'uid' => $request['uid'],
  366. 'post_id' => $request['post_id'],
  367. 'parent_id' => 0,
  368. 'username' => $userInfo['username'],
  369. 'reply_uid' => 0,
  370. 'reply_username' => '',
  371. 'avatar' => $userInfo['avatar'] ?? '',
  372. 'content' => $request['content'],
  373. 'is_delete' => 0,
  374. ];
  375. $parentCommentContent = '';
  376. $parentCommentUid = 0;
  377. $parentCommentTime = '';
  378. if (isset($request['parent_id']) && $request['parent_id'] != 0) {
  379. $comment = $this->postComment->find($request['parent_id']);
  380. if (!$comment) {
  381. return Response::create([
  382. 'message' => '获取评论信息失败',
  383. 'status_code' => 500
  384. ]);
  385. }
  386. if ($comment->parent_id) {
  387. return Response::create([
  388. 'message' => '只能回复评论',
  389. 'status_code' => 500
  390. ]);
  391. }
  392. $data['parent_id'] = $request['parent_id'];
  393. $data['reply_uid'] = $comment->uid;
  394. $data['reply_username'] = $comment->username;
  395. $parentCommentContent = $comment->content;
  396. $parentCommentUid = $comment->uid;
  397. $parentCommentTime = Carbon::parse($comment->created_at)->toDateTimeString();
  398. }
  399. DB::beginTransaction();
  400. try {
  401. $comment = $this->postComment->create($data);
  402. DB::commit();
  403. if(!$comment->parent_id){
  404. Redis::DEL('post_new_comment_'.$comment->post_id);
  405. }
  406. $virus = $this->behavior->where('behavior_identification', 'comment')->first();
  407. if ($virus) {
  408. if ($post->title) {
  409. $desc = $post->title;
  410. } else {
  411. $desc = subtext(strip_tags($post->content), 20);
  412. }
  413. $this->rabbitMqUtil->push('virus_add', [
  414. 'behavior_id' => $virus->virus_behavior_id,
  415. 'behavior_flag' => 'comment',
  416. 'post_id' => $post->id,
  417. 'post_type' => $post->type,
  418. 'post_author_uid' => $post->uid,
  419. 'post_desc' => $desc,
  420. 'post_cover' => $post->img,
  421. 'comment_id' => $comment->id,
  422. 'comment_content' => $comment->content,
  423. 'parent_comment_id' => $comment->parent_id,
  424. 'parent_comment_content' => $parentCommentContent,
  425. 'parent_comment_uid' => $parentCommentUid,
  426. 'parent_comment_time' => $parentCommentTime,
  427. 'reply_uid' => $comment->reply_uid,
  428. 'reply_username' => $comment->reply_username,
  429. 'target_id' => $comment->uid,
  430. 'action_id' => $comment->id,
  431. ]);
  432. }
  433. return Response::create();
  434. } catch (QueryException $exception) {
  435. DB::rollBack();
  436. Log::debug('评论内容:' . $request['post_id'] . $exception->getMessage());
  437. return Response::create([
  438. 'message' => '评论失败,请重试',
  439. 'error' => $exception->getMessage(),
  440. 'status_code' => 500
  441. ]);
  442. }
  443. }
  444. /**
  445. * 内容列表
  446. */
  447. public function lists($request)
  448. {
  449. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  450. $where = [];
  451. if (isset($request['is_suggest'])) {
  452. $where[] = ['is_suggest', $request['is_suggest']];
  453. }
  454. if (isset($request['type'])) {
  455. $where[] = ['type', $request['type']];
  456. }
  457. if (isset($request['uid'])) {
  458. $where[] = ['uid', $request['uid']];
  459. }
  460. $sort = 'post.id';
  461. if (isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])) {
  462. $sort = $request['sort'];
  463. }
  464. $post = $this->post;
  465. if (isset($request['waste']) && $request['waste'] == 1) {
  466. $post = $post->onlyTrashed();
  467. }
  468. return $post
  469. ->join('post_data', 'post_data.post_id', '=', 'post.id')
  470. ->select('post.*')
  471. ->where($where)
  472. ->where(function ($query) use ($request) {
  473. if (isset($request['keyword'])) {
  474. $query->where('uid', '=', $request['keyword'])
  475. ->orWhere('username', 'like', "%{$request['keyword']}%")
  476. ->orWhere('mobile', 'like', "%{$request['keyword']}%");
  477. }
  478. })
  479. ->where(function ($query) use ($request) {
  480. if (isset($request['content'])) {
  481. $query->where('title', 'like', "%{$request['content']}%")
  482. ->orWhere('content', 'like', "%{$request['content']}%");
  483. }
  484. })
  485. ->where(function ($query) use ($request) {
  486. if (isset($request['created_at'])) {
  487. $time = explode('_', $request['created_at']);
  488. $query->whereBetween('post.created_at', $time);
  489. }
  490. })
  491. ->where(function ($query) use ($request) {
  492. if (isset($request['category_ids']) || isset($request['topic_ids'])) {
  493. $ids = [];
  494. if (isset($request['category_ids'])) {
  495. $categoryIds = explode('_', $request['category_ids']);
  496. $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
  497. }
  498. if (isset($request['topic_ids'])) {
  499. $ids = array_merge($ids, explode('_', $request['topic_ids']));
  500. }
  501. foreach ($ids as $key => $id) {
  502. if ($key == 0) {
  503. $query = $query->whereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  504. } else {
  505. $query = $query->orWhereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  506. }
  507. }
  508. }
  509. })
  510. ->orderBy($sort, 'desc')
  511. ->paginate($perPage);
  512. }
  513. /**
  514. * 内容详情
  515. */
  516. public function detail($request)
  517. {
  518. return $this->post->withTrashed()->find($request['id']);
  519. }
  520. /**
  521. * 内容类型
  522. */
  523. public function getType($request)
  524. {
  525. $type = $this->post->where('id', $request['id'])->value('type');
  526. return Response::create([
  527. 'message' => '成功',
  528. 'status_code' => 200,
  529. 'data' => $type
  530. ]);
  531. }
  532. /**
  533. * 评论列表
  534. */
  535. public function commentList($request)
  536. {
  537. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  538. $where = [];
  539. if (isset($request['post_id'])) {
  540. $where[] = ['post_id', $request['post_id']];
  541. }
  542. if (isset($request['uid'])) {
  543. $where[] = ['uid', $request['uid']];
  544. }
  545. return $this->postComment
  546. ->where($where)
  547. ->orderBy('id', 'desc')
  548. ->paginate($perPage);
  549. }
  550. /**
  551. * 推荐内容
  552. */
  553. public function suggest($request)
  554. {
  555. $post = $this->post->where('id', $request['id'])->first();
  556. if (!$post) {
  557. return Response::create([
  558. 'message' => '获取内容信息失败',
  559. 'status_code' => 500
  560. ]);
  561. }
  562. if ($post->is_suggest == 1) {
  563. $post->is_suggest = 0;
  564. } else {
  565. $post->is_suggest = 1;
  566. }
  567. DB::beginTransaction();
  568. try {
  569. $post->save();
  570. DB::commit();
  571. return Response::create();
  572. } catch (QueryException $exception) {
  573. DB::rollBack();
  574. Log::debug('推荐内容:' . $request['id'] . $exception->getMessage());
  575. return Response::create([
  576. 'message' => '操作失败,请重试',
  577. 'error' => $exception->getMessage(),
  578. 'status_code' => 500
  579. ]);
  580. }
  581. }
  582. /**
  583. * 删除内容
  584. */
  585. public function delete($request)
  586. {
  587. $token = JWTAuth::decode(JWTAuth::getToken());
  588. if($token['type'] != 1){
  589. return Response::create([
  590. 'message' => '只有运营才能删除内容',
  591. 'status_code' => 500
  592. ]);
  593. }
  594. $post = $this->post->where('id', $request['id'])->first();
  595. if (!$post) {
  596. return Response::create([
  597. 'message' => '获取内容信息失败',
  598. 'status_code' => 500
  599. ]);
  600. }
  601. $uid = $post->uid;
  602. $title = $post->title;
  603. if(!$title){
  604. $title = subtext(strip_tags($post->content), 20);
  605. }
  606. $content = "经核实您的内容“{$title}”涉及违规,现已被删除,有任何问题请联系由你管理员";
  607. $logData = [
  608. 'uid' => $token['user']->id,
  609. 'operator_type' => 'admin',
  610. 'post_id' => $request['id'],
  611. 'username' => $token['user']->username,
  612. 'log_type' => 'delete',
  613. 'content' => json_encode(['delete' => $request['id']]),
  614. ];
  615. $date = Carbon::now()->toDateTimeString();
  616. DB::beginTransaction();
  617. try {
  618. $post->delete();
  619. $this->postLog->create($logData);
  620. DB::commit();
  621. Redis::SADD('delete_post_ids', $request['id']);
  622. $this->rabbitMqUtil->push('add_message_one', [
  623. 'uid' => $uid,
  624. 'message_rule_id' => 0,
  625. 'message_type' => 1,
  626. 'message_show_type' => 'post_delete',
  627. 'param' => [
  628. 'title' => '内容删除',
  629. 'content' => $content,
  630. 'cover' => '',
  631. 'activity_url' => 0,
  632. 'activity_time' => '',
  633. ],
  634. 'is_read' => 0,
  635. 'created_at' => $date,
  636. 'updated_at' => $date,
  637. ]);
  638. return Response::create();
  639. } catch (QueryException $exception) {
  640. DB::rollBack();
  641. Log::debug('删除内容:' . $request['id'] . $exception->getMessage());
  642. return Response::create([
  643. 'message' => '操作失败,请重试',
  644. 'error' => $exception->getMessage(),
  645. 'status_code' => 500
  646. ]);
  647. }
  648. }
  649. /**
  650. * 复原内容
  651. */
  652. public function restore($request)
  653. {
  654. $post = $this->post->withTrashed()->where('id', $request['id'])->first();
  655. if (!$post) {
  656. return Response::create([
  657. 'message' => '获取内容信息失败',
  658. 'status_code' => 500
  659. ]);
  660. }
  661. DB::beginTransaction();
  662. try {
  663. $post->restore();
  664. DB::commit();
  665. Redis::SREM('delete_post_ids', $request['id']);
  666. return Response::create();
  667. } catch (QueryException $exception) {
  668. DB::rollBack();
  669. Log::debug('复原内容:' . $request['id'] . $exception->getMessage());
  670. return Response::create([
  671. 'message' => '操作失败,请重试',
  672. 'error' => $exception->getMessage(),
  673. 'status_code' => 500
  674. ]);
  675. }
  676. }
  677. /**
  678. * 删除评论
  679. */
  680. public function commentDelete($request)
  681. {
  682. $comment = $this->postComment->find($request['id']);
  683. if (!$comment) {
  684. return Response::create([
  685. 'message' => '获取评论信息失败',
  686. 'status_code' => 500
  687. ]);
  688. }
  689. if ($comment->is_delete == 1) {
  690. return Response::create([
  691. 'message' => '该评论已经删除',
  692. 'status_code' => 500
  693. ]);
  694. }
  695. DB::beginTransaction();
  696. try {
  697. $comment->is_delete = 1;
  698. $comment->save();
  699. DB::commit();
  700. Redis::SADD('delete_post_comment_ids', $comment->id);
  701. if(!$comment->parent_id){
  702. Redis::DEL('post_new_comment_'.$comment->post_id);
  703. }
  704. return Response::create();
  705. } catch (QueryException $exception) {
  706. DB::rollBack();
  707. Log::debug('删除评论:' . $request['id'] . $exception->getMessage());
  708. return Response::create([
  709. 'message' => '操作失败,请重试',
  710. 'error' => $exception->getMessage(),
  711. 'status_code' => 500
  712. ]);
  713. }
  714. }
  715. /**
  716. * 隐藏内容
  717. */
  718. public function hide($request)
  719. {
  720. $post = $this->post->where('id', $request['id'])->first();
  721. if (!$post) {
  722. return Response::create([
  723. 'message' => '获取内容信息失败',
  724. 'status_code' => 500
  725. ]);
  726. }
  727. if ($post->is_hide == 1) {
  728. $post->is_hide = 0;
  729. } else {
  730. $post->is_hide = 1;
  731. }
  732. DB::beginTransaction();
  733. try {
  734. $post->save();
  735. DB::commit();
  736. return Response::create();
  737. } catch (QueryException $exception) {
  738. DB::rollBack();
  739. Log::debug('隐藏内容:' . $request['id'] . $exception->getMessage());
  740. return Response::create([
  741. 'message' => '操作失败,请重试',
  742. 'error' => $exception->getMessage(),
  743. 'status_code' => 500
  744. ]);
  745. }
  746. }
  747. /**
  748. * 日志列表
  749. */
  750. public function log($request)
  751. {
  752. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  753. $where = [];
  754. if (isset($request['log_type'])) {
  755. $where[] = ['log_type', $request['log_type']];
  756. }
  757. return $this->postLog
  758. ->where($where)
  759. ->where(function ($query) use ($request) {
  760. if (isset($request['created_at'])) {
  761. $time = explode('_', $request['created_at']);
  762. $query->whereBetween('created_at', $time);
  763. }
  764. })
  765. ->orderBy('id', 'desc')
  766. ->paginate($perPage);
  767. }
  768. public function download($filePath, $type, $request)
  769. {
  770. try {
  771. set_time_limit(0);
  772. if (!ini_get("auto_detect_line_endings")) {
  773. ini_set("auto_detect_line_endings", '1');
  774. }
  775. // 文件路径
  776. $writer = Writer::createFromPath(public_path($filePath), 'w+');
  777. // 设置标题
  778. if ($type == 'post') {
  779. $title = [
  780. '内容', date('Y年m月d日')
  781. ];
  782. } else {
  783. $title = [
  784. '回收站内容', date('Y年m月d日')
  785. ];
  786. }
  787. $title = eval('return ' . iconv('utf-8', 'gbk//IGNORE', var_export($title, true) . ';'));
  788. $writer->insertone($title);
  789. // 内容
  790. if ($type == 'post') {
  791. $header = [
  792. '内容ID', '发布时间', '用户昵称', '城市', '内容标签', '内容前20个字',
  793. '真实浏览量', '总浏览量', '真实点赞数', '总赞数', '真实分享数', '总分享数',
  794. '真实收藏数', '总收藏数', '评论数'
  795. ];
  796. } else {
  797. $header = [
  798. '内容ID', '发布时间', '用户昵称', '内容标签', '内容前20个字',
  799. '真实浏览量', '真实点赞数', '真实点赞数', '真实分享数', '真实收藏数', '评论数'
  800. ];
  801. }
  802. $header = eval('return ' . iconv('utf-8', 'gbk//IGNORE', var_export($header, true) . ';'));
  803. // $writer->setOutputBOM(Reader::BOM_UTF8);
  804. $writer->insertone($header);
  805. $where = [];
  806. if (isset($request['content'])) {
  807. $where[] = ['content', 'like', "%{$request['content']}%"];
  808. }
  809. if (isset($request['is_suggest'])) {
  810. $where[] = ['is_suggest', $request['is_suggest']];
  811. }
  812. if (isset($request['type'])) {
  813. $where[] = ['type', $request['type']];
  814. }
  815. $sort = 'post.id';
  816. if (isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])) {
  817. $sort = $request['sort'];
  818. }
  819. $post = $this->post;
  820. if ($type == 'post_waste') {
  821. $post = $post->onlyTrashed();
  822. }
  823. $post->join('post_data', 'post_data.post_id', '=', 'post.id')
  824. ->select('post.*')
  825. ->where($where)
  826. ->where(function ($query) use ($request) {
  827. if (isset($request['keyword'])) {
  828. $query->where('uid', '=', $request['keyword'])
  829. ->orWhere('username', 'like', "%{$request['keyword']}%")
  830. ->orWhere('mobile', 'like', "%{$request['keyword']}%");
  831. }
  832. })
  833. ->where(function ($query) use ($request) {
  834. if (isset($request['created_at'])) {
  835. $time = explode('_', $request['created_at']);
  836. $query->whereBetween('post.created_at', $time);
  837. }
  838. })
  839. ->where(function ($query) use ($request) {
  840. if (isset($request['category_ids']) || isset($request['topic_ids'])) {
  841. $ids = [];
  842. if (isset($request['category_ids'])) {
  843. $categoryIds = explode('_', $request['category_ids']);
  844. $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
  845. }
  846. if (isset($request['topic_ids'])) {
  847. $ids = array_merge($ids, explode('_', $request['topic_ids']));
  848. }
  849. Log::debug('话题ids:' . json_encode($ids));
  850. foreach ($ids as $key => $id) {
  851. if ($key == 0) {
  852. $query = $query->whereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  853. } else {
  854. $query = $query->orWhereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  855. }
  856. }
  857. }
  858. })
  859. ->orderBy($sort, 'desc')
  860. ->chunk(1, function ($posts) use ($writer, $type) {
  861. $data = [];
  862. foreach ($posts as $post) {
  863. if ($type == 'post') {
  864. $tmp = [
  865. $post->id,
  866. Carbon::parse($post->created_at)->toDateTimeString(),
  867. $post->username,
  868. $post->location,
  869. implode(' ', $post->topic()->toArray()),
  870. subtext(strip_tags($post->content), 20),
  871. $post->data->pv_real,
  872. $post->data->pv,
  873. $post->data->praise_real_count,
  874. $post->data->praise_count,
  875. $post->data->share_real_count,
  876. $post->data->share_count,
  877. $post->data->collect_real_count,
  878. $post->data->collect_count,
  879. $post->data->comment_count
  880. ];
  881. } else {
  882. $tmp = [
  883. $post->id,
  884. Carbon::parse($post->created_at)->toDateTimeString(),
  885. $post->username,
  886. Carbon::parse($post->created_at)->toDateTimeString(),
  887. subtext(strip_tags($post->content), 20),
  888. $post->data->pv_real,
  889. $post->data->praise_real_count,
  890. $post->data->share_real_count,
  891. $post->data->collect_real_count,
  892. $post->data->comment_count
  893. ];
  894. }
  895. foreach ($tmp as $key => $value) {
  896. $tmp[$key] = iconv('utf-8', 'gbk//IGNORE', $value);
  897. }
  898. $data[] = $tmp;
  899. }
  900. $writer->insertAll($data);
  901. });
  902. Log::info('内容导出成功!');
  903. } catch (QueryException $e) {
  904. Log::debug('内容导出失败!'.$e->getMessage());
  905. }
  906. }
  907. /**
  908. * 统计社区内容
  909. * @param $start
  910. * @param $end
  911. * @return array
  912. */
  913. public function statistics($start, $end)
  914. {
  915. $result = $this->postStatistics
  916. ->where('created_at', '>=', $start)
  917. ->where('created_at', '<=', $end)
  918. ->get()->toArray();
  919. $stimestamp = strtotime($start);
  920. $etimestamp = strtotime($end);
  921. $days = ($etimestamp - $stimestamp) / 86400;
  922. $date = array();
  923. for ($i = 0; $i < $days; $i++) {
  924. $date[] = date('Y-m-d', $stimestamp + (86400 * $i));
  925. }
  926. $totalRead = 0;
  927. $totalPost = 0;
  928. $totalShare = 0;
  929. $totalLike = 0;
  930. $totalCollect = 0;
  931. $totalComment = 0;
  932. $info = [];
  933. foreach ($date as $key => $value) {
  934. $info[$value] = [
  935. 'read' => 0,
  936. 'post' => 0,
  937. 'share' => 0,
  938. 'like' => 0,
  939. 'collect' => 0,
  940. 'comment' => 0,
  941. ];
  942. foreach ($result as $row) {
  943. if ($value == date('Y-m-d', strtotime($row['created_at']))) {
  944. $info[$value]['read'] = $row['read_count'];
  945. $info[$value]['post'] = $row['post_count'];
  946. $info[$value]['share'] = $row['share_count'];
  947. $info[$value]['like'] = $row['like_count'];
  948. $info[$value]['collect'] = $row['collect_count'];
  949. $info[$value]['comment'] = $row['comment_count'];
  950. $totalRead += $row['read_count'];
  951. $totalPost += $row['post_count'];
  952. $totalShare += $row['share_count'];
  953. $totalLike += $row['like_count'];
  954. $totalCollect += $row['collect_count'];
  955. $totalComment += $row['comment_count'];
  956. }
  957. }
  958. }
  959. $info['data']['total_read'] = $totalRead;
  960. $info['data']['total_post'] = $totalPost;
  961. $info['data']['total_share'] = $totalShare;
  962. $info['data']['total_like'] = $totalLike;
  963. $info['data']['total_collect'] = $totalCollect;
  964. $info['data']['total_comment'] = $totalComment;
  965. return $info;
  966. }
  967. }