PostRepository.php 38 KB

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