PostRepository.php 36 KB

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