PostRepository.php 35 KB

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