PostRepository.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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. $data = [
  99. 'uid' => $userInfo['uid'],
  100. 'username' => $userInfo['username'],
  101. 'mobile' => $userInfo['mobile'],
  102. 'avatar' => $userInfo['avatar'] ?? '',
  103. 'type' => $request['type'],
  104. 'img' => $request['img'],
  105. 'video' => $request['video'] ?? '',
  106. 'video_id' => $request['video_id'] ?? '',
  107. 'topic_ids' => $topicIds,
  108. 'title' => $request['title'] ?? '',
  109. 'content' => $request['content'],
  110. 'location' => $request['location'] ?? '',
  111. 'is_suggest' => $request['is_suggest'],
  112. 'is_hide' => 0
  113. ];
  114. $date = date('Y-m-d H:i:s');
  115. $fresh = (Carbon::now()->timestamp) - (Carbon::parse("2019-05-01 00:00:00")->timestamp);
  116. $score = $fresh / 86400;
  117. DB::beginTransaction();
  118. try {
  119. $post = $this->post->create($data);
  120. $postData = $this->postData->create([
  121. 'post_id' => $post->id,
  122. 'pv' => 0,
  123. 'pv_real' => 0,
  124. 'dislike_count' => 0,
  125. 'praise_count' => 0,
  126. 'praise_real_count' => 0,
  127. 'share_count' => 0,
  128. 'share_real_count' => 0,
  129. 'comment_count' => 0,
  130. 'collect_count' => 0,
  131. 'collect_real_count' => 0,
  132. 'available_bean' => $this->availableBean(),
  133. 'will_collect_bean' => rand(100, 200),
  134. 'collect_bean' => 0,
  135. 'weight' => $score
  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. 'weight', $postData->weight);
  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. DB::commit();
  404. if(!$comment->parent_id){
  405. Redis::DEL('post_new_comment_'.$comment->post_id);
  406. }
  407. $virus = $this->behavior->where('behavior_identification', 'comment')->first();
  408. if ($virus) {
  409. if ($post->title) {
  410. $desc = $post->title;
  411. } else {
  412. $desc = subtext(strip_tags($post->content), 20);
  413. }
  414. $this->rabbitMqUtil->push('virus_add', [
  415. 'behavior_id' => $virus->virus_behavior_id,
  416. 'behavior_flag' => 'comment',
  417. 'post_id' => $post->id,
  418. 'post_type' => $post->type,
  419. 'post_author_uid' => $post->uid,
  420. 'post_desc' => $desc,
  421. 'post_cover' => $post->img,
  422. 'comment_id' => $comment->id,
  423. 'comment_content' => $comment->content,
  424. 'parent_comment_id' => $comment->parent_id,
  425. 'parent_comment_content' => $parentCommentContent,
  426. 'parent_comment_uid' => $parentCommentUid,
  427. 'parent_comment_time' => $parentCommentTime,
  428. 'reply_uid' => $comment->reply_uid,
  429. 'reply_username' => $comment->reply_username,
  430. 'target_id' => $comment->uid,
  431. 'action_id' => $comment->id,
  432. ]);
  433. }
  434. return Response::create();
  435. } catch (QueryException $exception) {
  436. DB::rollBack();
  437. Log::debug('评论内容:' . $request['post_id'] . $exception->getMessage());
  438. return Response::create([
  439. 'message' => '评论失败,请重试',
  440. 'error' => $exception->getMessage(),
  441. 'status_code' => 500
  442. ]);
  443. }
  444. }
  445. /**
  446. * 内容列表
  447. */
  448. public function lists($request)
  449. {
  450. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  451. $where = [];
  452. if (isset($request['is_suggest'])) {
  453. $where[] = ['is_suggest', $request['is_suggest']];
  454. }
  455. if (isset($request['type'])) {
  456. $where[] = ['type', $request['type']];
  457. }
  458. if (isset($request['uid'])) {
  459. $where[] = ['uid', $request['uid']];
  460. }
  461. $sort = 'post.id';
  462. if (isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])) {
  463. $sort = $request['sort'];
  464. }
  465. $post = $this->post;
  466. if (isset($request['waste']) && $request['waste'] == 1) {
  467. $post = $post->onlyTrashed();
  468. }
  469. return $post
  470. ->join('post_data', 'post_data.post_id', '=', 'post.id')
  471. ->select('post.*')
  472. ->where($where)
  473. ->where(function ($query) use ($request) {
  474. if (isset($request['keyword'])) {
  475. $query->where('uid', '=', $request['keyword'])
  476. ->orWhere('username', 'like', "%{$request['keyword']}%")
  477. ->orWhere('mobile', 'like', "%{$request['keyword']}%");
  478. }
  479. })
  480. ->where(function ($query) use ($request) {
  481. if (isset($request['content'])) {
  482. $query->where('title', 'like', "%{$request['content']}%")
  483. ->orWhere('content', 'like', "%{$request['content']}%");
  484. }
  485. })
  486. ->where(function ($query) use ($request) {
  487. if (isset($request['created_at'])) {
  488. $time = explode('_', $request['created_at']);
  489. $query->whereBetween('post.created_at', $time);
  490. }
  491. })
  492. ->where(function ($query) use ($request) {
  493. if (isset($request['category_ids']) || isset($request['topic_ids'])) {
  494. $ids = [];
  495. if (isset($request['category_ids'])) {
  496. $categoryIds = explode('_', $request['category_ids']);
  497. $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
  498. }
  499. if (isset($request['topic_ids'])) {
  500. $ids = array_merge($ids, explode('_', $request['topic_ids']));
  501. }
  502. foreach ($ids as $key => $id) {
  503. if ($key == 0) {
  504. $query = $query->whereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  505. } else {
  506. $query = $query->orWhereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  507. }
  508. }
  509. }
  510. })
  511. ->orderBy($sort, 'desc')
  512. ->paginate($perPage);
  513. }
  514. /**
  515. * 内容详情
  516. */
  517. public function detail($request)
  518. {
  519. return $this->post->withTrashed()->find($request['id']);
  520. }
  521. /**
  522. * 内容类型
  523. */
  524. public function getType($request)
  525. {
  526. $type = $this->post->where('id', $request['id'])->value('type');
  527. return Response::create([
  528. 'message' => '成功',
  529. 'status_code' => 200,
  530. 'data' => $type
  531. ]);
  532. }
  533. /**
  534. * 评论列表
  535. */
  536. public function commentList($request)
  537. {
  538. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  539. $where = [];
  540. if (isset($request['post_id'])) {
  541. $where[] = ['post_id', $request['post_id']];
  542. }
  543. if (isset($request['uid'])) {
  544. $where[] = ['uid', $request['uid']];
  545. }
  546. return $this->postComment
  547. ->where($where)
  548. ->orderBy('id', 'desc')
  549. ->paginate($perPage);
  550. }
  551. /**
  552. * 推荐内容
  553. */
  554. public function suggest($request)
  555. {
  556. $post = $this->post->where('id', $request['id'])->first();
  557. if (!$post) {
  558. return Response::create([
  559. 'message' => '获取内容信息失败',
  560. 'status_code' => 500
  561. ]);
  562. }
  563. if ($post->is_suggest == 1) {
  564. $post->is_suggest = 0;
  565. } else {
  566. $post->is_suggest = 1;
  567. }
  568. DB::beginTransaction();
  569. try {
  570. $post->save();
  571. DB::commit();
  572. return Response::create();
  573. } catch (QueryException $exception) {
  574. DB::rollBack();
  575. Log::debug('推荐内容:' . $request['id'] . $exception->getMessage());
  576. return Response::create([
  577. 'message' => '操作失败,请重试',
  578. 'error' => $exception->getMessage(),
  579. 'status_code' => 500
  580. ]);
  581. }
  582. }
  583. /**
  584. * 删除内容
  585. */
  586. public function delete($request)
  587. {
  588. $token = JWTAuth::decode(JWTAuth::getToken());
  589. if($token['type'] != 1){
  590. return Response::create([
  591. 'message' => '只有运营才能删除内容',
  592. 'status_code' => 500
  593. ]);
  594. }
  595. $post = $this->post->where('id', $request['id'])->first();
  596. if (!$post) {
  597. return Response::create([
  598. 'message' => '获取内容信息失败',
  599. 'status_code' => 500
  600. ]);
  601. }
  602. $uid = $post->uid;
  603. $title = $post->title;
  604. if(!$title){
  605. $title = subtext(strip_tags($post->content), 20);
  606. }
  607. $content = "经核实您的内容“{$title}”涉及违规,现已被删除,有任何问题请联系由你管理员";
  608. $logData = [
  609. 'uid' => $token['user']->id,
  610. 'operator_type' => 'admin',
  611. 'post_id' => $request['id'],
  612. 'username' => $token['user']->username,
  613. 'log_type' => 'delete',
  614. 'content' => json_encode(['delete' => $request['id']]),
  615. ];
  616. $date = Carbon::now()->toDateTimeString();
  617. DB::beginTransaction();
  618. try {
  619. $post->delete();
  620. $this->postLog->create($logData);
  621. DB::commit();
  622. Redis::SADD('delete_post_ids', $request['id']);
  623. $this->rabbitMqUtil->push('add_message_one', [
  624. 'uid' => $uid,
  625. 'message_rule_id' => 0,
  626. 'message_type' => 1,
  627. 'message_show_type' => 'post_delete',
  628. 'param' => [
  629. 'title' => '内容删除',
  630. 'content' => $content,
  631. 'cover' => '',
  632. 'activity_url' => 0,
  633. 'activity_time' => '',
  634. ],
  635. 'is_read' => 0,
  636. 'created_at' => $date,
  637. 'updated_at' => $date,
  638. ]);
  639. return Response::create();
  640. } catch (QueryException $exception) {
  641. DB::rollBack();
  642. Log::debug('删除内容:' . $request['id'] . $exception->getMessage());
  643. return Response::create([
  644. 'message' => '操作失败,请重试',
  645. 'error' => $exception->getMessage(),
  646. 'status_code' => 500
  647. ]);
  648. }
  649. }
  650. /**
  651. * 复原内容
  652. */
  653. public function restore($request)
  654. {
  655. $post = $this->post->withTrashed()->where('id', $request['id'])->first();
  656. if (!$post) {
  657. return Response::create([
  658. 'message' => '获取内容信息失败',
  659. 'status_code' => 500
  660. ]);
  661. }
  662. DB::beginTransaction();
  663. try {
  664. $post->restore();
  665. DB::commit();
  666. Redis::SREM('delete_post_ids', $request['id']);
  667. return Response::create();
  668. } catch (QueryException $exception) {
  669. DB::rollBack();
  670. Log::debug('复原内容:' . $request['id'] . $exception->getMessage());
  671. return Response::create([
  672. 'message' => '操作失败,请重试',
  673. 'error' => $exception->getMessage(),
  674. 'status_code' => 500
  675. ]);
  676. }
  677. }
  678. /**
  679. * 删除评论
  680. */
  681. public function commentDelete($request)
  682. {
  683. $comment = $this->postComment->find($request['id']);
  684. if (!$comment) {
  685. return Response::create([
  686. 'message' => '获取评论信息失败',
  687. 'status_code' => 500
  688. ]);
  689. }
  690. if ($comment->is_delete == 1) {
  691. return Response::create([
  692. 'message' => '该评论已经删除',
  693. 'status_code' => 500
  694. ]);
  695. }
  696. DB::beginTransaction();
  697. try {
  698. $comment->is_delete = 1;
  699. $comment->save();
  700. DB::commit();
  701. Redis::SADD('delete_post_comment_ids', $comment->id);
  702. if(!$comment->parent_id){
  703. Redis::DEL('post_new_comment_'.$comment->post_id);
  704. }
  705. return Response::create();
  706. } catch (QueryException $exception) {
  707. DB::rollBack();
  708. Log::debug('删除评论:' . $request['id'] . $exception->getMessage());
  709. return Response::create([
  710. 'message' => '操作失败,请重试',
  711. 'error' => $exception->getMessage(),
  712. 'status_code' => 500
  713. ]);
  714. }
  715. }
  716. /**
  717. * 隐藏内容
  718. */
  719. public function hide($request)
  720. {
  721. $post = $this->post->where('id', $request['id'])->first();
  722. if (!$post) {
  723. return Response::create([
  724. 'message' => '获取内容信息失败',
  725. 'status_code' => 500
  726. ]);
  727. }
  728. if ($post->is_hide == 1) {
  729. $post->is_hide = 0;
  730. } else {
  731. $post->is_hide = 1;
  732. }
  733. DB::beginTransaction();
  734. try {
  735. $post->save();
  736. DB::commit();
  737. return Response::create();
  738. } catch (QueryException $exception) {
  739. DB::rollBack();
  740. Log::debug('隐藏内容:' . $request['id'] . $exception->getMessage());
  741. return Response::create([
  742. 'message' => '操作失败,请重试',
  743. 'error' => $exception->getMessage(),
  744. 'status_code' => 500
  745. ]);
  746. }
  747. }
  748. /**
  749. * 日志列表
  750. */
  751. public function log($request)
  752. {
  753. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  754. $where = [];
  755. if (isset($request['log_type'])) {
  756. $where[] = ['log_type', $request['log_type']];
  757. }
  758. return $this->postLog
  759. ->where($where)
  760. ->where(function ($query) use ($request) {
  761. if (isset($request['created_at'])) {
  762. $time = explode('_', $request['created_at']);
  763. $query->whereBetween('created_at', $time);
  764. }
  765. })
  766. ->orderBy('id', 'desc')
  767. ->paginate($perPage);
  768. }
  769. public function download($filePath, $type, $request)
  770. {
  771. try {
  772. set_time_limit(0);
  773. if (!ini_get("auto_detect_line_endings")) {
  774. ini_set("auto_detect_line_endings", '1');
  775. }
  776. // 文件路径
  777. $writer = Writer::createFromPath(public_path($filePath), 'w+');
  778. // 设置标题
  779. if ($type == 'post') {
  780. $title = [
  781. '内容', date('Y年m月d日')
  782. ];
  783. } else {
  784. $title = [
  785. '回收站内容', date('Y年m月d日')
  786. ];
  787. }
  788. $title = eval('return ' . iconv('utf-8', 'gbk//IGNORE', var_export($title, true) . ';'));
  789. $writer->insertone($title);
  790. // 内容
  791. if ($type == 'post') {
  792. $header = [
  793. '内容ID', '发布时间', '用户昵称', '城市', '内容标签', '内容前20个字',
  794. '真实浏览量', '总浏览量', '真实点赞数', '总赞数', '真实分享数', '总分享数',
  795. '真实收藏数', '总收藏数', '评论数'
  796. ];
  797. } else {
  798. $header = [
  799. '内容ID', '发布时间', '用户昵称', '内容标签', '内容前20个字',
  800. '真实浏览量', '真实点赞数', '真实点赞数', '真实分享数', '真实收藏数', '评论数'
  801. ];
  802. }
  803. $header = eval('return ' . iconv('utf-8', 'gbk//IGNORE', var_export($header, true) . ';'));
  804. // $writer->setOutputBOM(Reader::BOM_UTF8);
  805. $writer->insertone($header);
  806. $where = [];
  807. if (isset($request['content'])) {
  808. $where[] = ['content', 'like', "%{$request['content']}%"];
  809. }
  810. if (isset($request['is_suggest'])) {
  811. $where[] = ['is_suggest', $request['is_suggest']];
  812. }
  813. if (isset($request['type'])) {
  814. $where[] = ['type', $request['type']];
  815. }
  816. $sort = 'post.id';
  817. if (isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])) {
  818. $sort = $request['sort'];
  819. }
  820. $post = $this->post;
  821. if ($type == 'post_waste') {
  822. $post = $post->onlyTrashed();
  823. }
  824. $post->join('post_data', 'post_data.post_id', '=', 'post.id')
  825. ->select('post.*')
  826. ->where($where)
  827. ->where(function ($query) use ($request) {
  828. if (isset($request['keyword'])) {
  829. $query->where('uid', '=', $request['keyword'])
  830. ->orWhere('username', 'like', "%{$request['keyword']}%")
  831. ->orWhere('mobile', 'like', "%{$request['keyword']}%");
  832. }
  833. })
  834. ->where(function ($query) use ($request) {
  835. if (isset($request['created_at'])) {
  836. $time = explode('_', $request['created_at']);
  837. $query->whereBetween('post.created_at', $time);
  838. }
  839. })
  840. ->where(function ($query) use ($request) {
  841. if (isset($request['category_ids']) || isset($request['topic_ids'])) {
  842. $ids = [];
  843. if (isset($request['category_ids'])) {
  844. $categoryIds = explode('_', $request['category_ids']);
  845. $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
  846. }
  847. if (isset($request['topic_ids'])) {
  848. $ids = array_merge($ids, explode('_', $request['topic_ids']));
  849. }
  850. Log::debug('话题ids:' . json_encode($ids));
  851. foreach ($ids as $key => $id) {
  852. if ($key == 0) {
  853. $query = $query->whereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  854. } else {
  855. $query = $query->orWhereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  856. }
  857. }
  858. }
  859. })
  860. ->orderBy($sort, 'desc')
  861. ->chunk(1, function ($posts) use ($writer, $type) {
  862. $data = [];
  863. foreach ($posts as $post) {
  864. if ($type == 'post') {
  865. $tmp = [
  866. $post->id,
  867. Carbon::parse($post->created_at)->toDateTimeString(),
  868. $post->username,
  869. $post->location,
  870. implode(' ', $post->topic()->toArray()),
  871. subtext(strip_tags($post->content), 20),
  872. $post->data->pv_real,
  873. $post->data->pv,
  874. $post->data->praise_real_count,
  875. $post->data->praise_count,
  876. $post->data->share_real_count,
  877. $post->data->share_count,
  878. $post->data->collect_real_count,
  879. $post->data->collect_count,
  880. $post->data->comment_count
  881. ];
  882. } else {
  883. $tmp = [
  884. $post->id,
  885. Carbon::parse($post->created_at)->toDateTimeString(),
  886. $post->username,
  887. Carbon::parse($post->created_at)->toDateTimeString(),
  888. subtext(strip_tags($post->content), 20),
  889. $post->data->pv_real,
  890. $post->data->praise_real_count,
  891. $post->data->share_real_count,
  892. $post->data->collect_real_count,
  893. $post->data->comment_count
  894. ];
  895. }
  896. foreach ($tmp as $key => $value) {
  897. $tmp[$key] = iconv('utf-8', 'gbk//IGNORE', $value);
  898. }
  899. $data[] = $tmp;
  900. }
  901. $writer->insertAll($data);
  902. });
  903. Log::info('内容导出成功!');
  904. } catch (QueryException $e) {
  905. Log::debug('内容导出失败!'.$e->getMessage());
  906. }
  907. }
  908. /**
  909. * 统计社区内容
  910. * @param $start
  911. * @param $end
  912. * @return array
  913. */
  914. public function statistics($start, $end)
  915. {
  916. $result = $this->postStatistics
  917. ->where('created_at', '>=', $start)
  918. ->where('created_at', '<=', $end)
  919. ->get()->toArray();
  920. $stimestamp = strtotime($start);
  921. $etimestamp = strtotime($end);
  922. $days = ($etimestamp - $stimestamp) / 86400;
  923. $date = array();
  924. for ($i = 0; $i < $days; $i++) {
  925. $date[] = date('Y-m-d', $stimestamp + (86400 * $i));
  926. }
  927. $totalRead = 0;
  928. $totalPost = 0;
  929. $totalShare = 0;
  930. $totalLike = 0;
  931. $totalCollect = 0;
  932. $totalComment = 0;
  933. $info = [];
  934. foreach ($date as $key => $value) {
  935. $info[$value] = [
  936. 'read' => 0,
  937. 'post' => 0,
  938. 'share' => 0,
  939. 'like' => 0,
  940. 'collect' => 0,
  941. 'comment' => 0,
  942. ];
  943. foreach ($result as $row) {
  944. if ($value == date('Y-m-d', strtotime($row['created_at']))) {
  945. $info[$value]['read'] = $row['read_count'];
  946. $info[$value]['post'] = $row['post_count'];
  947. $info[$value]['share'] = $row['share_count'];
  948. $info[$value]['like'] = $row['like_count'];
  949. $info[$value]['collect'] = $row['collect_count'];
  950. $info[$value]['comment'] = $row['comment_count'];
  951. $totalRead += $row['read_count'];
  952. $totalPost += $row['post_count'];
  953. $totalShare += $row['share_count'];
  954. $totalLike += $row['like_count'];
  955. $totalCollect += $row['collect_count'];
  956. $totalComment += $row['comment_count'];
  957. }
  958. }
  959. }
  960. $info['data']['total_read'] = $totalRead;
  961. $info['data']['total_post'] = $totalPost;
  962. $info['data']['total_share'] = $totalShare;
  963. $info['data']['total_like'] = $totalLike;
  964. $info['data']['total_collect'] = $totalCollect;
  965. $info['data']['total_comment'] = $totalComment;
  966. return $info;
  967. }
  968. }