PostRepository.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  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('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. ->with('data')
  477. ->select('post.*')
  478. ->where($where)
  479. ->where(function ($query) use ($request) {
  480. if (isset($request['keyword'])) {
  481. $query->where('uid', '=', $request['keyword'])
  482. ->orWhere('username', 'like', "%{$request['keyword']}%")
  483. ->orWhere('mobile', 'like', "%{$request['keyword']}%");
  484. }
  485. })
  486. ->where(function ($query) use ($request) {
  487. if (isset($request['content'])) {
  488. $query->where('title', 'like', "%{$request['content']}%")
  489. ->orWhere('content', 'like', "%{$request['content']}%");
  490. }
  491. })
  492. ->where(function ($query) use ($request) {
  493. if (isset($request['created_at'])) {
  494. $time = explode('_', $request['created_at']);
  495. $query->whereBetween('post.created_at', $time);
  496. }
  497. })
  498. ->where(function ($query) use ($request) {
  499. if (isset($request['category_ids']) || isset($request['topic_ids'])) {
  500. $ids = [];
  501. if (isset($request['category_ids'])) {
  502. $categoryIds = explode('_', $request['category_ids']);
  503. $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
  504. }
  505. if (isset($request['topic_ids'])) {
  506. $ids = array_merge($ids, explode('_', $request['topic_ids']));
  507. }
  508. foreach ($ids as $key => $id) {
  509. if ($key == 0) {
  510. $query = $query->whereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  511. } else {
  512. $query = $query->orWhereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  513. }
  514. }
  515. }
  516. })
  517. ->orderBy($sort, 'desc')
  518. ->paginate($perPage);
  519. }
  520. /**
  521. * 内容详情
  522. */
  523. public function detail($request)
  524. {
  525. return $this->post->withTrashed()->find($request['id']);
  526. }
  527. /**
  528. * 内容类型
  529. */
  530. public function getType($request)
  531. {
  532. $type = $this->post->where('id', $request['id'])->value('type');
  533. return Response::create([
  534. 'message' => '成功',
  535. 'status_code' => 200,
  536. 'data' => $type
  537. ]);
  538. }
  539. /**
  540. * 评论列表
  541. */
  542. public function commentList($request)
  543. {
  544. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  545. $where = [];
  546. if (isset($request['post_id'])) {
  547. $where[] = ['post_id', $request['post_id']];
  548. }
  549. if (isset($request['uid'])) {
  550. $where[] = ['uid', $request['uid']];
  551. }
  552. return $this->postComment
  553. ->where($where)
  554. ->orderBy('id', 'desc')
  555. ->paginate($perPage);
  556. }
  557. /**
  558. * 推荐内容
  559. */
  560. public function suggest($request)
  561. {
  562. $post = $this->post->where('id', $request['id'])->first();
  563. if (!$post) {
  564. return Response::create([
  565. 'message' => '获取内容信息失败',
  566. 'status_code' => 500
  567. ]);
  568. }
  569. if ($post->is_suggest == 1) {
  570. $post->is_suggest = 0;
  571. } else {
  572. $post->is_suggest = 1;
  573. }
  574. DB::beginTransaction();
  575. try {
  576. $post->save();
  577. DB::commit();
  578. return Response::create();
  579. } catch (QueryException $exception) {
  580. DB::rollBack();
  581. Log::debug('推荐内容:' . $request['id'] . $exception->getMessage());
  582. return Response::create([
  583. 'message' => '操作失败,请重试',
  584. 'error' => $exception->getMessage(),
  585. 'status_code' => 500
  586. ]);
  587. }
  588. }
  589. /**
  590. * 删除内容
  591. */
  592. public function delete($request)
  593. {
  594. $token = JWTAuth::decode(JWTAuth::getToken());
  595. if($token['type'] != 1){
  596. return Response::create([
  597. 'message' => '只有运营才能删除内容',
  598. 'status_code' => 500
  599. ]);
  600. }
  601. $post = $this->post->where('id', $request['id'])->first();
  602. if (!$post) {
  603. return Response::create([
  604. 'message' => '获取内容信息失败',
  605. 'status_code' => 500
  606. ]);
  607. }
  608. $uid = $post->uid;
  609. $title = $post->title;
  610. if(!$title){
  611. $title = subtext(strip_tags($post->content), 20);
  612. }
  613. $content = "经核实您的内容“{$title}”涉及违规,现已被删除,有任何问题请联系由你管理员";
  614. $logData = [
  615. 'uid' => $token['user']->id,
  616. 'operator_type' => 'admin',
  617. 'post_id' => $request['id'],
  618. 'username' => $token['user']->username,
  619. 'log_type' => 'delete',
  620. 'content' => json_encode(['delete' => $request['id']]),
  621. ];
  622. $date = Carbon::now()->toDateTimeString();
  623. DB::beginTransaction();
  624. try {
  625. $post->delete();
  626. $this->postLog->create($logData);
  627. DB::commit();
  628. Redis::SADD('delete_post_ids', $request['id']);
  629. $this->rabbitMqUtil->push('add_message_one', [
  630. 'uid' => $uid,
  631. 'message_rule_id' => 0,
  632. 'message_type' => 1,
  633. 'message_show_type' => 'post_delete',
  634. 'param' => [
  635. 'title' => '内容删除',
  636. 'content' => $content,
  637. 'cover' => '',
  638. 'activity_url' => 0,
  639. 'activity_time' => '',
  640. ],
  641. 'is_read' => 0,
  642. 'created_at' => $date,
  643. 'updated_at' => $date,
  644. ]);
  645. return Response::create();
  646. } catch (QueryException $exception) {
  647. DB::rollBack();
  648. Log::debug('删除内容:' . $request['id'] . $exception->getMessage());
  649. return Response::create([
  650. 'message' => '操作失败,请重试',
  651. 'error' => $exception->getMessage(),
  652. 'status_code' => 500
  653. ]);
  654. }
  655. }
  656. /**
  657. * 复原内容
  658. */
  659. public function restore($request)
  660. {
  661. $post = $this->post->withTrashed()->where('id', $request['id'])->first();
  662. if (!$post) {
  663. return Response::create([
  664. 'message' => '获取内容信息失败',
  665. 'status_code' => 500
  666. ]);
  667. }
  668. DB::beginTransaction();
  669. try {
  670. $post->restore();
  671. DB::commit();
  672. Redis::SREM('delete_post_ids', $request['id']);
  673. return Response::create();
  674. } catch (QueryException $exception) {
  675. DB::rollBack();
  676. Log::debug('复原内容:' . $request['id'] . $exception->getMessage());
  677. return Response::create([
  678. 'message' => '操作失败,请重试',
  679. 'error' => $exception->getMessage(),
  680. 'status_code' => 500
  681. ]);
  682. }
  683. }
  684. /**
  685. * 删除评论
  686. */
  687. public function commentDelete($request)
  688. {
  689. $comment = $this->postComment->find($request['id']);
  690. if (!$comment) {
  691. return Response::create([
  692. 'message' => '获取评论信息失败',
  693. 'status_code' => 500
  694. ]);
  695. }
  696. if ($comment->is_delete == 1) {
  697. return Response::create([
  698. 'message' => '该评论已经删除',
  699. 'status_code' => 500
  700. ]);
  701. }
  702. DB::beginTransaction();
  703. try {
  704. $comment->is_delete = 1;
  705. $comment->save();
  706. DB::commit();
  707. Redis::SADD('delete_post_comment_ids', $comment->id);
  708. if(!$comment->parent_id){
  709. Redis::DEL('post_new_comment_'.$comment->post_id);
  710. }else{
  711. Redis::DEL('post_new_reply_'.$comment->id);
  712. }
  713. return Response::create();
  714. } catch (QueryException $exception) {
  715. DB::rollBack();
  716. Log::debug('删除评论:' . $request['id'] . $exception->getMessage());
  717. return Response::create([
  718. 'message' => '操作失败,请重试',
  719. 'error' => $exception->getMessage(),
  720. 'status_code' => 500
  721. ]);
  722. }
  723. }
  724. /**
  725. * 隐藏内容
  726. */
  727. public function hide($request)
  728. {
  729. $post = $this->post->where('id', $request['id'])->first();
  730. if (!$post) {
  731. return Response::create([
  732. 'message' => '获取内容信息失败',
  733. 'status_code' => 500
  734. ]);
  735. }
  736. if ($post->is_hide == 1) {
  737. $post->is_hide = 0;
  738. } else {
  739. $post->is_hide = 1;
  740. }
  741. DB::beginTransaction();
  742. try {
  743. $post->save();
  744. DB::commit();
  745. return Response::create();
  746. } catch (QueryException $exception) {
  747. DB::rollBack();
  748. Log::debug('隐藏内容:' . $request['id'] . $exception->getMessage());
  749. return Response::create([
  750. 'message' => '操作失败,请重试',
  751. 'error' => $exception->getMessage(),
  752. 'status_code' => 500
  753. ]);
  754. }
  755. }
  756. /**
  757. * 日志列表
  758. */
  759. public function log($request)
  760. {
  761. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  762. $where = [];
  763. if (isset($request['log_type'])) {
  764. $where[] = ['log_type', $request['log_type']];
  765. }
  766. return $this->postLog
  767. ->where($where)
  768. ->where(function ($query) use ($request) {
  769. if (isset($request['created_at'])) {
  770. $time = explode('_', $request['created_at']);
  771. $query->whereBetween('created_at', $time);
  772. }
  773. })
  774. ->orderBy('id', 'desc')
  775. ->paginate($perPage);
  776. }
  777. public function download($filePath, $type, $request)
  778. {
  779. try {
  780. set_time_limit(0);
  781. if (!ini_get("auto_detect_line_endings")) {
  782. ini_set("auto_detect_line_endings", '1');
  783. }
  784. // 文件路径
  785. $writer = Writer::createFromPath(public_path($filePath), 'w+');
  786. // 设置标题
  787. if ($type == 'post') {
  788. $title = [
  789. '内容', date('Y年m月d日')
  790. ];
  791. } else {
  792. $title = [
  793. '回收站内容', date('Y年m月d日')
  794. ];
  795. }
  796. $title = eval('return ' . iconv('utf-8', 'gbk//IGNORE', var_export($title, true) . ';'));
  797. $writer->insertone($title);
  798. // 内容
  799. if ($type == 'post') {
  800. $header = [
  801. '内容ID', '发布时间', '用户昵称', '城市', '内容标签', '内容前20个字',
  802. '真实浏览量', '总浏览量', '真实点赞数', '总赞数', '真实分享数', '总分享数',
  803. '真实收藏数', '总收藏数', '评论数'
  804. ];
  805. } else {
  806. $header = [
  807. '内容ID', '发布时间', '用户昵称', '内容标签', '内容前20个字',
  808. '真实浏览量', '真实点赞数', '真实点赞数', '真实分享数', '真实收藏数', '评论数'
  809. ];
  810. }
  811. $header = eval('return ' . iconv('utf-8', 'gbk//IGNORE', var_export($header, true) . ';'));
  812. // $writer->setOutputBOM(Reader::BOM_UTF8);
  813. $writer->insertone($header);
  814. $where = [];
  815. if (isset($request['content'])) {
  816. $where[] = ['content', 'like', "%{$request['content']}%"];
  817. }
  818. if (isset($request['is_suggest'])) {
  819. $where[] = ['is_suggest', $request['is_suggest']];
  820. }
  821. if (isset($request['type'])) {
  822. $where[] = ['type', $request['type']];
  823. }
  824. $sort = 'post.id';
  825. if (isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])) {
  826. $sort = $request['sort'];
  827. }
  828. $post = $this->post;
  829. if ($type == 'post_waste') {
  830. $post = $post->onlyTrashed();
  831. }
  832. $post->join('post_data', 'post_data.post_id', '=', 'post.id')
  833. ->select('post.*')
  834. ->where($where)
  835. ->where(function ($query) use ($request) {
  836. if (isset($request['keyword'])) {
  837. $query->where('uid', '=', $request['keyword'])
  838. ->orWhere('username', 'like', "%{$request['keyword']}%")
  839. ->orWhere('mobile', 'like', "%{$request['keyword']}%");
  840. }
  841. })
  842. ->where(function ($query) use ($request) {
  843. if (isset($request['created_at'])) {
  844. $time = explode('_', $request['created_at']);
  845. $query->whereBetween('post.created_at', $time);
  846. }
  847. })
  848. ->where(function ($query) use ($request) {
  849. if (isset($request['category_ids']) || isset($request['topic_ids'])) {
  850. $ids = [];
  851. if (isset($request['category_ids'])) {
  852. $categoryIds = explode('_', $request['category_ids']);
  853. $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
  854. }
  855. if (isset($request['topic_ids'])) {
  856. $ids = array_merge($ids, explode('_', $request['topic_ids']));
  857. }
  858. Log::debug('话题ids:' . json_encode($ids));
  859. foreach ($ids as $key => $id) {
  860. if ($key == 0) {
  861. $query = $query->whereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  862. } else {
  863. $query = $query->orWhereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  864. }
  865. }
  866. }
  867. })
  868. ->orderBy($sort, 'desc')
  869. ->chunk(1, function ($posts) use ($writer, $type) {
  870. $data = [];
  871. foreach ($posts as $post) {
  872. if ($type == 'post') {
  873. $tmp = [
  874. $post->id,
  875. Carbon::parse($post->created_at)->toDateTimeString(),
  876. $post->username,
  877. $post->location,
  878. implode(' ', $this->getTopic($post->topic_ids)),
  879. subtext(strip_tags($post->content), 20),
  880. $post->data->pv_real,
  881. $post->data->pv,
  882. $post->data->praise_real_count,
  883. $post->data->praise_count,
  884. $post->data->share_real_count,
  885. $post->data->share_count,
  886. $post->data->collect_real_count,
  887. $post->data->collect_count,
  888. $post->data->comment_count
  889. ];
  890. } else {
  891. $tmp = [
  892. $post->id,
  893. Carbon::parse($post->created_at)->toDateTimeString(),
  894. $post->username,
  895. Carbon::parse($post->created_at)->toDateTimeString(),
  896. subtext(strip_tags($post->content), 20),
  897. $post->data->pv_real,
  898. $post->data->praise_real_count,
  899. $post->data->share_real_count,
  900. $post->data->collect_real_count,
  901. $post->data->comment_count
  902. ];
  903. }
  904. foreach ($tmp as $key => $value) {
  905. $tmp[$key] = iconv('utf-8', 'gbk//IGNORE', $value);
  906. }
  907. $data[] = $tmp;
  908. }
  909. $writer->insertAll($data);
  910. });
  911. Log::info('内容导出成功!');
  912. } catch (QueryException $e) {
  913. Log::debug('内容导出失败!'.$e->getMessage());
  914. }
  915. }
  916. /**
  917. * 统计社区内容
  918. * @param $start
  919. * @param $end
  920. * @return array
  921. */
  922. public function statistics($start, $end)
  923. {
  924. $result = $this->postStatistics
  925. ->where('created_at', '>=', $start)
  926. ->where('created_at', '<=', $end)
  927. ->get()->toArray();
  928. $stimestamp = strtotime($start);
  929. $etimestamp = strtotime($end);
  930. $days = ($etimestamp - $stimestamp) / 86400;
  931. $date = array();
  932. for ($i = 0; $i < $days; $i++) {
  933. $date[] = date('Y-m-d', $stimestamp + (86400 * $i));
  934. }
  935. $totalRead = 0;
  936. $totalPost = 0;
  937. $totalShare = 0;
  938. $totalLike = 0;
  939. $totalCollect = 0;
  940. $totalComment = 0;
  941. $info = [];
  942. foreach ($date as $key => $value) {
  943. $info[$value] = [
  944. 'read' => 0,
  945. 'post' => 0,
  946. 'share' => 0,
  947. 'like' => 0,
  948. 'collect' => 0,
  949. 'comment' => 0,
  950. ];
  951. foreach ($result as $row) {
  952. if ($value == date('Y-m-d', strtotime($row['created_at']))) {
  953. $info[$value]['read'] = $row['read_count'];
  954. $info[$value]['post'] = $row['post_count'];
  955. $info[$value]['share'] = $row['share_count'];
  956. $info[$value]['like'] = $row['like_count'];
  957. $info[$value]['collect'] = $row['collect_count'];
  958. $info[$value]['comment'] = $row['comment_count'];
  959. $totalRead += $row['read_count'];
  960. $totalPost += $row['post_count'];
  961. $totalShare += $row['share_count'];
  962. $totalLike += $row['like_count'];
  963. $totalCollect += $row['collect_count'];
  964. $totalComment += $row['comment_count'];
  965. }
  966. }
  967. }
  968. $info['data']['total_read'] = $totalRead;
  969. $info['data']['total_post'] = $totalPost;
  970. $info['data']['total_share'] = $totalShare;
  971. $info['data']['total_like'] = $totalLike;
  972. $info['data']['total_collect'] = $totalCollect;
  973. $info['data']['total_comment'] = $totalComment;
  974. return $info;
  975. }
  976. /**
  977. * 编辑内容话题
  978. */
  979. public function updateTopic($request)
  980. {
  981. $token = JWTAuth::decode(JWTAuth::getToken());
  982. if($token['type'] != 1){
  983. return Response::create([
  984. 'message' => '只有运营才能删除内容',
  985. 'status_code' => 500
  986. ]);
  987. }
  988. $post = $this->post->where('id', $request['id'])->first();
  989. if (!$post) {
  990. return Response::create([
  991. 'message' => '获取内容信息失败',
  992. 'status_code' => 500
  993. ]);
  994. }
  995. //验证话题
  996. $topicIdsArray = $this->topic->whereIn('id', explode(',', $request['topic_ids']))->pluck('id')->toArray();
  997. $topicCount = count($topicIdsArray);
  998. if ($topicCount == 0 || $topicCount > 5) {
  999. return Response::create([
  1000. 'message' => '所选话题必须1-5个',
  1001. 'status_code' => 500
  1002. ]);
  1003. }
  1004. $topicIds = implode(',', $topicIdsArray);
  1005. $logData = [
  1006. 'uid' => $token['user']->id,
  1007. 'operator_type' => 'admin',
  1008. 'post_id' => $request['id'],
  1009. 'username' => $token['user']->username,
  1010. 'log_type' => 'update_topic',
  1011. 'content' => json_encode(['update_topic' => $request['id'].'-'.$topicIds]),
  1012. ];
  1013. DB::beginTransaction();
  1014. try {
  1015. $post->topic_ids = $topicIds;
  1016. $post->save();
  1017. $this->postLog->create($logData);
  1018. DB::commit();
  1019. Redis::HSET('post_info_'.$request['id'], 'topic_ids', $topicIds);
  1020. return Response::create();
  1021. } catch (QueryException $exception) {
  1022. DB::rollBack();
  1023. Log::debug('编辑内容话题:'.$post->id . $exception->getMessage());
  1024. return Response::create([
  1025. 'message' => '编辑失败,请重试',
  1026. 'error' => $exception->getMessage(),
  1027. 'status_code' => 500
  1028. ]);
  1029. }
  1030. }
  1031. }