PostRepository.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  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['content'])) {
  419. $where[] = ['content', 'like', "%{$request['content']}%"];
  420. }
  421. if (isset($request['is_suggest'])) {
  422. $where[] = ['is_suggest', $request['is_suggest']];
  423. }
  424. if (isset($request['type'])) {
  425. $where[] = ['type', $request['type']];
  426. }
  427. if (isset($request['uid'])) {
  428. $where[] = ['uid', $request['uid']];
  429. }
  430. $sort = 'post.id';
  431. if (isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])) {
  432. $sort = $request['sort'];
  433. }
  434. $post = $this->post;
  435. if (isset($request['waste']) && $request['waste'] == 1) {
  436. $post = $post->onlyTrashed();
  437. }
  438. return $post
  439. ->join('post_data', 'post_data.post_id', '=', 'post.id')
  440. ->select('post.*')
  441. ->where($where)
  442. ->where(function ($query) use ($request) {
  443. if (isset($request['keyword'])) {
  444. $query->where('uid', '=', $request['keyword'])
  445. ->orWhere('username', 'like', "%{$request['keyword']}%")
  446. ->orWhere('mobile', 'like', "%{$request['keyword']}%");
  447. }
  448. })
  449. ->where(function ($query) use ($request) {
  450. if (isset($request['created_at'])) {
  451. $time = explode('_', $request['created_at']);
  452. $query->whereBetween('post.created_at', $time);
  453. }
  454. })
  455. ->where(function ($query) use ($request) {
  456. if (isset($request['category_ids']) || isset($request['topic_ids'])) {
  457. $ids = [];
  458. if (isset($request['category_ids'])) {
  459. $categoryIds = explode('_', $request['category_ids']);
  460. $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
  461. }
  462. if (isset($request['topic_ids'])) {
  463. $ids = array_merge($ids, explode('_', $request['topic_ids']));
  464. }
  465. foreach ($ids as $key => $id) {
  466. if ($key == 0) {
  467. $query = $query->whereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  468. } else {
  469. $query = $query->orWhereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  470. }
  471. }
  472. }
  473. })
  474. ->orderBy($sort, 'desc')
  475. ->paginate($perPage);
  476. }
  477. /**
  478. * 内容详情
  479. */
  480. public function detail($request)
  481. {
  482. return $this->post->withTrashed()->find($request['id']);
  483. }
  484. /**
  485. * 评论列表
  486. */
  487. public function commentList($request)
  488. {
  489. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  490. $where = [];
  491. if (isset($request['post_id'])) {
  492. $where[] = ['post_id', $request['post_id']];
  493. }
  494. if (isset($request['uid'])) {
  495. $where[] = ['uid', $request['uid']];
  496. }
  497. return $this->postComment
  498. ->where($where)
  499. ->orderBy('id', 'desc')
  500. ->paginate($perPage);
  501. }
  502. /**
  503. * 推荐内容
  504. */
  505. public function suggest($request)
  506. {
  507. $post = $this->post->where('id', $request['id'])->first();
  508. if (!$post) {
  509. return Response::create([
  510. 'message' => '获取内容信息失败',
  511. 'status_code' => 500
  512. ]);
  513. }
  514. if ($post->is_suggest == 1) {
  515. $post->is_suggest = 0;
  516. } else {
  517. $post->is_suggest = 1;
  518. }
  519. DB::beginTransaction();
  520. try {
  521. $post->save();
  522. DB::commit();
  523. return Response::create();
  524. } catch (QueryException $exception) {
  525. DB::rollBack();
  526. Log::debug('推荐内容:' . $request['id'] . $exception->getMessage());
  527. return Response::create([
  528. 'message' => '操作失败,请重试',
  529. 'error' => $exception->getMessage(),
  530. 'status_code' => 500
  531. ]);
  532. }
  533. }
  534. /**
  535. * 删除内容
  536. */
  537. public function delete($request)
  538. {
  539. $post = $this->post->where('id', $request['id'])->first();
  540. if (!$post) {
  541. return Response::create([
  542. 'message' => '获取内容信息失败',
  543. 'status_code' => 500
  544. ]);
  545. }
  546. $uid = $post->uid;
  547. $title = $post->title;
  548. if(!$title){
  549. $title = subtext(strip_tags($post->content), 20);
  550. }
  551. $content = "经核实您的内容“{$title}”涉及违规,现已被删除,有任何问题请联系彩虹管理员";
  552. $date = Carbon::now()->toDateTimeString();
  553. DB::beginTransaction();
  554. try {
  555. $post->delete();
  556. DB::commit();
  557. $this->rabbitMqUtil->push('add_message_one', [
  558. 'uid' => $uid,
  559. 'message_rule_id' => 0,
  560. 'message_type' => 1,
  561. 'message_show_type' => 13,
  562. 'param' => json_encode([
  563. 'title' => '内容删除',
  564. 'content' => $content,
  565. 'cover' => '',
  566. 'activity_url' => 0,
  567. 'activity_time' => '',
  568. ]),
  569. 'is_read' => 0,
  570. 'created_at' => $date,
  571. 'updated_at' => $date,
  572. ]);
  573. return Response::create();
  574. } catch (QueryException $exception) {
  575. DB::rollBack();
  576. Log::debug('删除内容:' . $request['id'] . $exception->getMessage());
  577. return Response::create([
  578. 'message' => '操作失败,请重试',
  579. 'error' => $exception->getMessage(),
  580. 'status_code' => 500
  581. ]);
  582. }
  583. }
  584. /**
  585. * 复原内容
  586. */
  587. public function restore($request)
  588. {
  589. $post = $this->post->withTrashed()->where('id', $request['id'])->first();
  590. if (!$post) {
  591. return Response::create([
  592. 'message' => '获取内容信息失败',
  593. 'status_code' => 500
  594. ]);
  595. }
  596. DB::beginTransaction();
  597. try {
  598. $post->restore();
  599. DB::commit();
  600. return Response::create();
  601. } catch (QueryException $exception) {
  602. DB::rollBack();
  603. Log::debug('复原内容:' . $request['id'] . $exception->getMessage());
  604. return Response::create([
  605. 'message' => '操作失败,请重试',
  606. 'error' => $exception->getMessage(),
  607. 'status_code' => 500
  608. ]);
  609. }
  610. }
  611. /**
  612. * 删除评论
  613. */
  614. public function commentDelete($request)
  615. {
  616. $comment = $this->postComment->find($request['id']);
  617. if (!$comment) {
  618. return Response::create([
  619. 'message' => '获取评论信息失败',
  620. 'status_code' => 500
  621. ]);
  622. }
  623. if ($comment->is_delete == 1) {
  624. return Response::create([
  625. 'message' => '该评论已经删除',
  626. 'status_code' => 500
  627. ]);
  628. }
  629. DB::beginTransaction();
  630. try {
  631. $comment->is_delete = 1;
  632. $comment->save();
  633. DB::commit();
  634. return Response::create();
  635. } catch (QueryException $exception) {
  636. DB::rollBack();
  637. Log::debug('删除评论:' . $request['id'] . $exception->getMessage());
  638. return Response::create([
  639. 'message' => '操作失败,请重试',
  640. 'error' => $exception->getMessage(),
  641. 'status_code' => 500
  642. ]);
  643. }
  644. }
  645. /**
  646. * 隐藏内容
  647. */
  648. public function hide($request)
  649. {
  650. $post = $this->post->where('id', $request['id'])->first();
  651. if (!$post) {
  652. return Response::create([
  653. 'message' => '获取内容信息失败',
  654. 'status_code' => 500
  655. ]);
  656. }
  657. if ($post->is_hide == 1) {
  658. $post->is_hide = 0;
  659. } else {
  660. $post->is_hide = 1;
  661. }
  662. DB::beginTransaction();
  663. try {
  664. $post->save();
  665. DB::commit();
  666. return Response::create();
  667. } catch (QueryException $exception) {
  668. DB::rollBack();
  669. Log::debug('隐藏内容:' . $request['id'] . $exception->getMessage());
  670. return Response::create([
  671. 'message' => '操作失败,请重试',
  672. 'error' => $exception->getMessage(),
  673. 'status_code' => 500
  674. ]);
  675. }
  676. }
  677. /**
  678. * 日志列表
  679. */
  680. public function log($request)
  681. {
  682. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  683. $where = [];
  684. if (isset($request['log_type'])) {
  685. $where[] = ['log_type', $request['log_type']];
  686. }
  687. return $this->postLog
  688. ->where($where)
  689. ->where(function ($query) use ($request) {
  690. if (isset($request['created_at'])) {
  691. $time = explode('_', $request['created_at']);
  692. $query->whereBetween('created_at', $time);
  693. }
  694. })
  695. ->orderBy('id', 'desc')
  696. ->paginate($perPage);
  697. }
  698. public function download($filePath, $type, $request)
  699. {
  700. try {
  701. set_time_limit(0);
  702. if (!ini_get("auto_detect_line_endings")) {
  703. ini_set("auto_detect_line_endings", '1');
  704. }
  705. // 文件路径
  706. $writer = Writer::createFromPath(public_path($filePath), 'w+');
  707. // 设置标题
  708. if ($type == 'post') {
  709. $title = [
  710. '内容', date('Y年m月d日')
  711. ];
  712. } else {
  713. $title = [
  714. '回收站内容', date('Y年m月d日')
  715. ];
  716. }
  717. $title = eval('return ' . iconv('utf-8', 'gbk//IGNORE', var_export($title, true) . ';'));
  718. $writer->insertone($title);
  719. // 内容
  720. if ($type == 'post') {
  721. $header = [
  722. '内容ID', '发布时间', '用户昵称', '城市', '内容标签', '内容前20个字',
  723. '真实浏览量', '总浏览量', '真实点赞数', '总赞数', '真实分享数', '总分享数',
  724. '真实收藏数', '总收藏数', '评论数'
  725. ];
  726. } else {
  727. $header = [
  728. '内容ID', '发布时间', '用户昵称', '内容标签', '内容前20个字',
  729. '真实浏览量', '真实点赞数', '真实点赞数', '真实分享数', '真实收藏数', '评论数'
  730. ];
  731. }
  732. $header = eval('return ' . iconv('utf-8', 'gbk//IGNORE', var_export($header, true) . ';'));
  733. // $writer->setOutputBOM(Reader::BOM_UTF8);
  734. $writer->insertone($header);
  735. $where = [];
  736. if (isset($request['content'])) {
  737. $where[] = ['content', 'like', "%{$request['content']}%"];
  738. }
  739. if (isset($request['is_suggest'])) {
  740. $where[] = ['is_suggest', $request['is_suggest']];
  741. }
  742. if (isset($request['type'])) {
  743. $where[] = ['type', $request['type']];
  744. }
  745. $sort = 'post.id';
  746. if (isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])) {
  747. $sort = $request['sort'];
  748. }
  749. $post = $this->post;
  750. if ($type == 'post_waste') {
  751. $post = $post->onlyTrashed();
  752. }
  753. $post->join('post_data', 'post_data.post_id', '=', 'post.id')
  754. ->select('post.*')
  755. ->where($where)
  756. ->where(function ($query) use ($request) {
  757. if (isset($request['keyword'])) {
  758. $query->where('uid', '=', $request['keyword'])
  759. ->orWhere('username', 'like', "%{$request['keyword']}%")
  760. ->orWhere('mobile', 'like', "%{$request['keyword']}%");
  761. }
  762. })
  763. ->where(function ($query) use ($request) {
  764. if (isset($request['created_at'])) {
  765. $time = explode('_', $request['created_at']);
  766. $query->whereBetween('post.created_at', $time);
  767. }
  768. })
  769. ->where(function ($query) use ($request) {
  770. if (isset($request['category_ids']) || isset($request['topic_ids'])) {
  771. $ids = [];
  772. if (isset($request['category_ids'])) {
  773. $categoryIds = explode('_', $request['category_ids']);
  774. $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
  775. }
  776. if (isset($request['topic_ids'])) {
  777. $ids = array_merge($ids, explode('_', $request['topic_ids']));
  778. }
  779. Log::debug('话题ids:' . json_encode($ids));
  780. foreach ($ids as $key => $id) {
  781. if ($key == 0) {
  782. $query = $query->whereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  783. } else {
  784. $query = $query->orWhereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  785. }
  786. }
  787. }
  788. })
  789. ->orderBy($sort, 'desc')
  790. ->chunk(1, function ($posts) use ($writer, $type) {
  791. $data = [];
  792. foreach ($posts as $post) {
  793. if ($type == 'post') {
  794. $tmp = [
  795. $post->id,
  796. Carbon::parse($post->created_at)->toDateTimeString(),
  797. $post->username,
  798. $post->location,
  799. implode(' ', $post->topic()->toArray()),
  800. subtext(strip_tags($post->content), 20),
  801. $post->data->pv_real,
  802. $post->data->pv,
  803. $post->data->praise_real_count,
  804. $post->data->praise_count,
  805. $post->data->share_real_count,
  806. $post->data->share_count,
  807. $post->data->collect_real_count,
  808. $post->data->collect_count,
  809. $post->data->comment_count
  810. ];
  811. } else {
  812. $tmp = [
  813. $post->id,
  814. Carbon::parse($post->created_at)->toDateTimeString(),
  815. $post->username,
  816. Carbon::parse($post->created_at)->toDateTimeString(),
  817. subtext(strip_tags($post->content), 20),
  818. $post->data->pv_real,
  819. $post->data->praise_real_count,
  820. $post->data->share_real_count,
  821. $post->data->collect_real_count,
  822. $post->data->comment_count
  823. ];
  824. }
  825. foreach ($tmp as $key => $value) {
  826. $tmp[$key] = iconv('utf-8', 'gbk//IGNORE', $value);
  827. }
  828. $data[] = $tmp;
  829. }
  830. $writer->insertAll($data);
  831. });
  832. Log::info('内容导出成功!');
  833. } catch (QueryException $e) {
  834. Log::debug('内容导出失败!'.$e->getMessage());
  835. }
  836. }
  837. /**
  838. * 统计社区内容
  839. * @param $start
  840. * @param $end
  841. * @return array
  842. */
  843. public function statistics($start, $end)
  844. {
  845. $result = $this->postStatistics
  846. ->where('created_at', '>=', $start)
  847. ->where('created_at', '<=', $end)
  848. ->get()->toArray();
  849. $stimestamp = strtotime($start);
  850. $etimestamp = strtotime($end);
  851. $days = ($etimestamp - $stimestamp) / 86400;
  852. $date = array();
  853. for ($i = 0; $i < $days; $i++) {
  854. $date[] = date('Y-m-d', $stimestamp + (86400 * $i));
  855. }
  856. $totalRead = 0;
  857. $totalPost = 0;
  858. $totalShare = 0;
  859. $totalLike = 0;
  860. $totalCollect = 0;
  861. $totalComment = 0;
  862. $info = [];
  863. foreach ($date as $key => $value) {
  864. $info[$value] = [
  865. 'read' => 0,
  866. 'post' => 0,
  867. 'share' => 0,
  868. 'like' => 0,
  869. 'collect' => 0,
  870. 'comment' => 0,
  871. ];
  872. foreach ($result as $row) {
  873. if ($value == date('Y-m-d', strtotime($row['created_at']))) {
  874. $info[$value]['read'] = $row['read_count'];
  875. $info[$value]['post'] = $row['post_count'];
  876. $info[$value]['share'] = $row['share_count'];
  877. $info[$value]['like'] = $row['like_count'];
  878. $info[$value]['collect'] = $row['collect_count'];
  879. $info[$value]['comment'] = $row['comment_count'];
  880. $totalRead += $row['read_count'];
  881. $totalPost += $row['post_count'];
  882. $totalShare += $row['share_count'];
  883. $totalLike += $row['like_count'];
  884. $totalCollect += $row['collect_count'];
  885. $totalComment += $row['comment_count'];
  886. }
  887. }
  888. }
  889. $info['data']['total_read'] = $totalRead;
  890. $info['data']['total_post'] = $totalPost;
  891. $info['data']['total_share'] = $totalShare;
  892. $info['data']['total_like'] = $totalLike;
  893. $info['data']['total_collect'] = $totalCollect;
  894. $info['data']['total_comment'] = $totalComment;
  895. return $info;
  896. }
  897. }