PostRepository.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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\CategoryTopic;
  10. use App\Models\Post;
  11. use App\Models\PostComment;
  12. use App\Models\PostData;
  13. use App\Models\PostImgs;
  14. use App\Models\PostLog;
  15. use App\Models\Topic;
  16. use App\Service\RabbitMqUtil;
  17. use App\Traits\PostTrait;
  18. use App\Traits\UserTrait;
  19. use Illuminate\Database\QueryException;
  20. use Dingo\Api\Http\Response;
  21. use Illuminate\Support\Carbon;
  22. use Illuminate\Support\Facades\DB;
  23. use Illuminate\Support\Facades\Log;
  24. use Illuminate\Support\Facades\Redis;
  25. use Symfony\Component\HttpKernel\Exception\HttpException;
  26. use Tymon\JWTAuth\Facades\JWTAuth;
  27. use League\Csv\Writer;
  28. use League\Csv\CannotInsertRecord;
  29. class PostRepository
  30. {
  31. use PostTrait;
  32. use UserTrait;
  33. public function __construct(Post $post,
  34. PostData $postData,
  35. PostComment $postComment,
  36. PostImgs $postImgs,
  37. PostLog $postLog,
  38. RabbitMqUtil $rabbitMqUtil,
  39. CategoryTopic $categoryTopic,
  40. Topic $topic)
  41. {
  42. $this->post = $post;
  43. $this->postData = $postData;
  44. $this->postComment = $postComment;
  45. $this->postImgs = $postImgs;
  46. $this->postLog = $postLog;
  47. $this->rabbitMqUtil = $rabbitMqUtil;
  48. $this->categoryTopic = $categoryTopic;
  49. $this->topic = $topic;
  50. }
  51. /**
  52. * 发布内容
  53. */
  54. public function create($request)
  55. {
  56. //验证小号
  57. //todo 小号
  58. // $userInfo = $this->getUserInfo($request['uid']);
  59. // if(!$userInfo || $userInfo['type'] != 1){
  60. // return Response::create([
  61. // 'message' => '所选小号信息有误',
  62. // 'status_code' => 500
  63. // ]);
  64. // }
  65. //验证话题
  66. $topicIds = $this->topic->whereIn('id', explode(',', $request['topic_ids']))->pluck('id')->toArray();
  67. $topicCount = count($topicIds);
  68. if($topicCount == 0 || $topicCount > 5){
  69. return Response::create([
  70. 'message' => '所选话题必须1-5个',
  71. 'status_code' => 500
  72. ]);
  73. }
  74. $topicIds = implode(',', $topicIds);
  75. //验证内容字数
  76. $html = preg_replace("/<([a-zA-Z]+)[^>]*>/","<\\1>",$request['content']);
  77. if(mb_strlen($html,'UTF8') > 1000){
  78. return Response::create([
  79. 'message' => '所传内容不能超过1000字',
  80. 'status_code' => 500
  81. ]);
  82. }
  83. $data = [
  84. 'uid' => $request['uid'],
  85. 'username' => '暂无',
  86. 'mobile' => '13720758488',
  87. 'avatar' => 'https://wx.qlogo.cn/mmopen/vi_32/DYAIOgq83ep3asJn8emiat1MnPdviaPNroWY3f65y5ezkTAk2qtibv7ea9Ht9R2ahxr9bicY1DIj5vN5FibpDOwXegg/132',
  88. 'type' => $request['type'],
  89. 'img' => $request['img'],
  90. 'video' => $request['video']??'',
  91. 'topic_ids' => $topicIds,
  92. 'title' => $request['title']??'',
  93. 'content' => $request['content'],
  94. 'location' => $request['location']??'',
  95. 'is_suggest' => $request['is_suggest'],
  96. 'is_hide' => 0
  97. ];
  98. $date = date('Y-m-d H:i:s');
  99. DB::beginTransaction();
  100. try{
  101. $post = $this->post->create($data);
  102. $this->postData->create([
  103. 'post_id' => $post->id,
  104. 'pv' => 0,
  105. 'pv_real' => 0,
  106. 'dislike_count' => 0,
  107. 'praise_count' => 0,
  108. 'praise_real_count' => 0,
  109. 'share_count' => 0,
  110. 'share_real_count' => 0,
  111. 'comment_count' => 0,
  112. 'collect_count' => 0,
  113. 'collect_real_count' => 0,
  114. 'available_bean' => $this->availableBean(),
  115. 'will_collect_bean' => rand(100, 200),
  116. 'collect_bean' => 0,
  117. 'weight' => 0
  118. ]);
  119. if(!empty($request['imgs']) && $request['type'] == 'image'){
  120. $imgData = [];
  121. foreach($request['imgs'] as $img){
  122. $imgData[] = [
  123. 'post_id' => $post->id,
  124. 'img' => $img,
  125. 'created_at' => $date,
  126. 'updated_at' => $date
  127. ];
  128. }
  129. $this->postImgs->insert($imgData);
  130. }
  131. DB::commit();
  132. Redis::zadd('post_trigger_type', 0, $post->id);
  133. return Response::create();
  134. }catch (QueryException $exception){
  135. DB::rollBack();
  136. Log::debug('发布内容:'.$exception->getMessage());
  137. return Response::create([
  138. 'message' => '发布失败,请重试',
  139. 'error' => $exception->getMessage(),
  140. 'status_code' => 500
  141. ]);
  142. }
  143. }
  144. /**
  145. * 增加数据
  146. */
  147. public function addData($request)
  148. {
  149. $token = JWTAuth::decode(JWTAuth::getToken());
  150. if(!$token || $token['type'] != 1){
  151. return Response::create([
  152. 'message' => '获取登陆信息失败',
  153. 'status_code' => 500
  154. ]);
  155. }
  156. $uid = $token['user']->id;
  157. $username = $token['user']->username;
  158. //验证小号数量
  159. $postData = $this->postData->where('post_id', $request['post_id'])->first();
  160. if(!$postData){
  161. return Response::create([
  162. 'message' => '获取内容失败',
  163. 'status_code' => 500
  164. ]);
  165. }
  166. if($request['add_pv'] == 0 && $request['add_praise_count'] == 0 && $request['add_collect_count'] == 0 && $request['add_share_count'] == 0){
  167. return Response::create([
  168. 'message' => '增加数据不能同时为0',
  169. 'status_code' => 500
  170. ]);
  171. }
  172. $content = '';
  173. if($request['add_pv']){
  174. $postData->pv += $request['add_pv'];
  175. $content .= '浏览数增加'.$request['add_pv'].'、';
  176. }
  177. if($request['add_praise_count']){
  178. $postData->praise_count += $request['add_praise_count'];
  179. $content .= '点赞数增加'.$request['add_praise_count'].'、';
  180. }
  181. if($request['add_collect_count']){
  182. $postData->collect_count += $request['add_collect_count'];
  183. $content .= '收藏数增加'.$request['add_collect_count'].'、';
  184. }
  185. if($request['add_share_count']){
  186. $postData->share_count += $request['add_share_count'];
  187. $content .= '分享数增加'.$request['add_share_count'];
  188. }
  189. $content = rtrim($content, '、');
  190. DB::beginTransaction();
  191. try{
  192. $postData->save();
  193. $this->postLog->create([
  194. 'post_id' => $request['post_id'],
  195. 'uid' => $uid,
  196. 'username' => $username,
  197. 'log_type' => 'add_data',
  198. 'content' => $content
  199. ]);
  200. DB::commit();
  201. return Response::create();
  202. }catch (QueryException $exception){
  203. DB::rollBack();
  204. Log::debug('内容增加数据:'.$request['post_id'].$exception->getMessage());
  205. return Response::create([
  206. 'message' => '增加数据失败,请重试',
  207. 'error' => $exception->getMessage(),
  208. 'status_code' => 500
  209. ]);
  210. }
  211. }
  212. /**
  213. * 评论&回复
  214. */
  215. public function comment($request)
  216. {
  217. //验证小号
  218. $userInfo = $this->getUserInfo($request['uid']);
  219. Log::debug('评论&回复小号'.json_encode($userInfo));
  220. if(!$userInfo || $userInfo['type'] != 1){
  221. return Response::create([
  222. 'message' => '所选小号信息有误',
  223. 'status_code' => 500
  224. ]);
  225. }
  226. $post = $this->post->find($request['post_id']);
  227. if(!$post){
  228. return Response::create([
  229. 'message' => '获取内容失败',
  230. 'status_code' => 500
  231. ]);
  232. }
  233. $data = [
  234. 'uid' => $request['uid'],
  235. 'post_id' => $request['post_id'],
  236. 'parent_id' => 0,
  237. 'username' => $userInfo['username'],
  238. 'reply_uid' => 0,
  239. 'reply_username' => '',
  240. 'avatar' => $userInfo['avatar'],
  241. 'content' => $request['content'],
  242. 'is_delete' => 0,
  243. ];
  244. if(isset($request['parent_id']) && $request['parent_id'] != 0){
  245. $comment = $this->postComment->find($request['parent_id']);
  246. if(!$comment){
  247. return Response::create([
  248. 'message' => '获取评论信息失败',
  249. 'status_code' => 500
  250. ]);
  251. }
  252. if($comment->parent_id){
  253. return Response::create([
  254. 'message' => '只能回复评论',
  255. 'status_code' => 500
  256. ]);
  257. }
  258. $data['parent_id'] = $request['parent_id'];
  259. $data['reply_uid'] = $comment->uid;
  260. $data['reply_username'] = $comment->username;
  261. $this->rabbitMqUtil->push('add_message', [
  262. 'uid' => $comment->uid,
  263. 'message_show_type' => 'post_reply_main',
  264. 'param' => [
  265. 'uid' => $userInfo['uid'],
  266. 'username' => $userInfo['username'],
  267. 'post_id' => $post->id,
  268. 'cover' => $post->img,
  269. 'content' => subtext($request['content'], 20),
  270. ]
  271. ]);
  272. }else{
  273. $this->rabbitMqUtil->push('add_message', [
  274. 'uid' => $post->uid,
  275. 'message_show_type' => 'post_comment',
  276. 'param' => [
  277. 'uid' => $userInfo['uid'],
  278. 'username' => $userInfo['username'],
  279. 'post_id' => $post->id,
  280. 'cover' => $post->img,
  281. 'content' => subtext($request['content'], 20),
  282. ]
  283. ]);
  284. }
  285. DB::beginTransaction();
  286. try{
  287. $this->postComment->create($data);
  288. $post->data->comment_count += 1;
  289. $post->data->save();
  290. DB::commit();
  291. return Response::create();
  292. }catch (QueryException $exception){
  293. DB::rollBack();
  294. Log::debug('评论内容:'.$request['post_id'].$exception->getMessage());
  295. return Response::create([
  296. 'message' => '评论失败,请重试',
  297. 'error' => $exception->getMessage(),
  298. 'status_code' => 500
  299. ]);
  300. }
  301. }
  302. /**
  303. * 内容列表
  304. */
  305. public function lists($request)
  306. {
  307. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  308. $where = [];
  309. if(isset($request['content'])){
  310. $where[] = ['content', 'like', "%{$request['content']}%"];
  311. }
  312. if(isset($request['is_suggest'])){
  313. $where[] = ['is_suggest', $request['is_suggest']];
  314. }
  315. if(isset($request['type'])){
  316. $where[] = ['type', $request['type']];
  317. }
  318. if(isset($request['uid'])){
  319. $where[] = ['uid', $request['uid']];
  320. }
  321. $sort = 'post.id';
  322. if(isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])){
  323. $sort = $request['sort'];
  324. }
  325. $post = $this->post;
  326. if(isset($request['waste']) && $request['waste'] == 1){
  327. $post = $post->onlyTrashed();
  328. }
  329. return $post
  330. ->join('post_data', 'post_data.post_id', '=', 'post.id')
  331. ->select('post.*')
  332. ->where($where)
  333. ->where(function($query) use ($request){
  334. if(isset($request['keyword'])){
  335. $query->where('uid', '=', $request['keyword'])
  336. ->orWhere('username', 'like', "%{$request['keyword']}%")
  337. ->orWhere('mobile', 'like', "%{$request['keyword']}%");
  338. }
  339. })
  340. ->where(function($query) use ($request){
  341. if(isset($request['created_at'])){
  342. $time = explode('_', $request['created_at']);
  343. $query->whereBetween('post.created_at', $time);
  344. }
  345. })
  346. ->where(function ($query) use ($request){
  347. if(isset($request['category_ids']) || isset($request['topic_ids'])){
  348. $ids = [];
  349. if (isset($request['category_ids'])) {
  350. $categoryIds = explode('_', $request['category_ids']);
  351. $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
  352. }
  353. if (isset($request['topic_ids'])) {
  354. $ids = array_merge($ids, explode('_', $request['topic_ids']));
  355. }
  356. foreach ($ids as $key=>$id) {
  357. if ($key==0) {
  358. $query = $query->whereRaw('FIND_IN_SET('.$id.',topic_ids)');
  359. } else {
  360. $query = $query->orWhereRaw('FIND_IN_SET('.$id.',topic_ids)');
  361. }
  362. }
  363. }
  364. })
  365. ->orderBy($sort,'desc')
  366. ->paginate($perPage);
  367. }
  368. /**
  369. * 内容详情
  370. */
  371. public function detail($request)
  372. {
  373. return $this->post->withTrashed()->find($request['id']);
  374. }
  375. /**
  376. * 评论列表
  377. */
  378. public function commentList($request)
  379. {
  380. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  381. $where = [];
  382. if(isset($request['post_id'])){
  383. $where[] = ['post_id', $request['post_id']];
  384. }
  385. if(isset($request['uid'])){
  386. $where[] = ['uid', $request['uid']];
  387. }
  388. return $this->postComment
  389. ->where($where)
  390. ->orderBy('id','desc')
  391. ->paginate($perPage);
  392. }
  393. /**
  394. * 推荐内容
  395. */
  396. public function suggest($request)
  397. {
  398. $post = $this->post->where('id', $request['id'])->first();
  399. if(!$post){
  400. return Response::create([
  401. 'message' => '获取内容信息失败',
  402. 'status_code' => 500
  403. ]);
  404. }
  405. if($post->is_suggest == 1){
  406. $post->is_suggest = 0;
  407. }else{
  408. $post->is_suggest = 1;
  409. }
  410. DB::beginTransaction();
  411. try{
  412. $post->save();
  413. DB::commit();
  414. return Response::create();
  415. }catch (QueryException $exception){
  416. DB::rollBack();
  417. Log::debug('推荐内容:'.$request['id'].$exception->getMessage());
  418. return Response::create([
  419. 'message' => '操作失败,请重试',
  420. 'error' => $exception->getMessage(),
  421. 'status_code' => 500
  422. ]);
  423. }
  424. }
  425. /**
  426. * 删除内容
  427. */
  428. public function delete($request)
  429. {
  430. $post = $this->post->where('id', $request['id'])->first();
  431. if(!$post){
  432. return Response::create([
  433. 'message' => '获取内容信息失败',
  434. 'status_code' => 500
  435. ]);
  436. }
  437. DB::beginTransaction();
  438. try{
  439. $post->delete();
  440. DB::commit();
  441. return Response::create();
  442. }catch (QueryException $exception){
  443. DB::rollBack();
  444. Log::debug('删除内容:'.$request['id'].$exception->getMessage());
  445. return Response::create([
  446. 'message' => '操作失败,请重试',
  447. 'error' => $exception->getMessage(),
  448. 'status_code' => 500
  449. ]);
  450. }
  451. }
  452. /**
  453. * 复原内容
  454. */
  455. public function restore($request)
  456. {
  457. $post = $this->post->withTrashed()->where('id', $request['id'])->first();
  458. if(!$post){
  459. return Response::create([
  460. 'message' => '获取内容信息失败',
  461. 'status_code' => 500
  462. ]);
  463. }
  464. DB::beginTransaction();
  465. try{
  466. $post->restore();
  467. DB::commit();
  468. return Response::create();
  469. }catch (QueryException $exception){
  470. DB::rollBack();
  471. Log::debug('复原内容:'.$request['id'].$exception->getMessage());
  472. return Response::create([
  473. 'message' => '操作失败,请重试',
  474. 'error' => $exception->getMessage(),
  475. 'status_code' => 500
  476. ]);
  477. }
  478. }
  479. /**
  480. * 删除评论
  481. */
  482. public function commentDelete($request)
  483. {
  484. $comment = $this->postComment->find($request['id']);
  485. if(!$comment){
  486. return Response::create([
  487. 'message' => '获取评论信息失败',
  488. 'status_code' => 500
  489. ]);
  490. }
  491. if($comment->is_delete == 1){
  492. return Response::create([
  493. 'message' => '该评论已经删除',
  494. 'status_code' => 500
  495. ]);
  496. }
  497. DB::beginTransaction();
  498. try{
  499. $comment->is_delete = 1;
  500. $comment->save();
  501. DB::commit();
  502. return Response::create();
  503. }catch (QueryException $exception){
  504. DB::rollBack();
  505. Log::debug('删除评论:'.$request['id'].$exception->getMessage());
  506. return Response::create([
  507. 'message' => '操作失败,请重试',
  508. 'error' => $exception->getMessage(),
  509. 'status_code' => 500
  510. ]);
  511. }
  512. }
  513. /**
  514. * 隐藏内容
  515. */
  516. public function hide($request)
  517. {
  518. $post = $this->post->where('id', $request['id'])->first();
  519. if(!$post){
  520. return Response::create([
  521. 'message' => '获取内容信息失败',
  522. 'status_code' => 500
  523. ]);
  524. }
  525. if($post->is_hide == 1){
  526. $post->is_hide = 0;
  527. }else{
  528. $post->is_hide = 1;
  529. }
  530. DB::beginTransaction();
  531. try{
  532. $post->save();
  533. DB::commit();
  534. return Response::create();
  535. }catch (QueryException $exception){
  536. DB::rollBack();
  537. Log::debug('隐藏内容:'.$request['id'].$exception->getMessage());
  538. return Response::create([
  539. 'message' => '操作失败,请重试',
  540. 'error' => $exception->getMessage(),
  541. 'status_code' => 500
  542. ]);
  543. }
  544. }
  545. /**
  546. * 日志列表
  547. */
  548. public function log($request)
  549. {
  550. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  551. $where = [];
  552. if(isset($request['log_type'])){
  553. $where[] = ['log_type', $request['log_type']];
  554. }
  555. return $this->postLog
  556. ->where($where)
  557. ->where(function($query) use ($request){
  558. if(isset($request['created_at'])){
  559. $time = explode('_', $request['created_at']);
  560. $query->whereBetween('created_at', $time);
  561. }
  562. })
  563. ->orderBy('id','desc')
  564. ->paginate($perPage);
  565. }
  566. public function download($filePath, $type, $request)
  567. {
  568. try {
  569. set_time_limit(0);
  570. if (!ini_get("auto_detect_line_endings")) {
  571. ini_set("auto_detect_line_endings", '1');
  572. }
  573. // 文件路径
  574. $writer = Writer::createFromPath(public_path($filePath), 'w+');
  575. // 设置标题
  576. if($type == 'post'){
  577. $title = [
  578. '内容', date('Y年m月d日')
  579. ];
  580. }else{
  581. $title = [
  582. '回收站内容', date('Y年m月d日')
  583. ];
  584. }
  585. $title = eval('return ' . iconv('utf-8', 'gbk//IGNORE', var_export($title, true) . ';'));
  586. $writer->insertone($title);
  587. // 内容
  588. if($type == 'post') {
  589. $header = [
  590. '内容ID', '发布时间', '用户昵称', '城市', '内容标签', '内容前20个字',
  591. '真实浏览量', '总浏览量', '真实点赞数', '总赞数', '真实分享数', '总分享数',
  592. '真实收藏数', '总收藏数', '评论数'
  593. ];
  594. } else {
  595. $header = [
  596. '内容ID', '发布时间', '用户昵称', '内容标签', '内容前20个字',
  597. '真实浏览量', '真实点赞数', '真实点赞数', '真实分享数', '真实收藏数', '评论数'
  598. ];
  599. }
  600. $header = eval('return ' . iconv('utf-8', 'gbk//IGNORE', var_export($header, true) . ';'));
  601. // $writer->setOutputBOM(Reader::BOM_UTF8);
  602. $writer->insertone($header);
  603. $where = [];
  604. if(isset($request['content'])){
  605. $where[] = ['content', 'like', "%{$request['content']}%"];
  606. }
  607. if(isset($request['is_suggest'])){
  608. $where[] = ['is_suggest', $request['is_suggest']];
  609. }
  610. if(isset($request['type'])){
  611. $where[] = ['type', $request['type']];
  612. }
  613. $sort = 'post.id';
  614. if(isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])){
  615. $sort = $request['sort'];
  616. }
  617. $post = $this->post;
  618. if($type == 'post_waste'){
  619. $post = $post->onlyTrashed();
  620. }
  621. $post->join('post_data', 'post_data.post_id', '=', 'post.id')
  622. ->select('post.*')
  623. ->where($where)
  624. ->where(function($query) use ($request){
  625. if(isset($request['keyword'])){
  626. $query->where('uid', '=', $request['keyword'])
  627. ->orWhere('username', 'like', "%{$request['keyword']}%")
  628. ->orWhere('mobile', 'like', "%{$request['keyword']}%");
  629. }
  630. })
  631. ->where(function($query) use ($request){
  632. if(isset($request['created_at'])){
  633. $time = explode('_', $request['created_at']);
  634. $query->whereBetween('post.created_at', $time);
  635. }
  636. })
  637. ->where(function ($query) use ($request){
  638. if(isset($request['category_ids']) || isset($request['topic_ids'])){
  639. $ids = [];
  640. if (isset($request['category_ids'])) {
  641. $categoryIds = explode('_', $request['category_ids']);
  642. $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
  643. }
  644. if (isset($request['topic_ids'])) {
  645. $ids = array_merge($ids, explode('_', $request['topic_ids']));
  646. }
  647. Log::debug('话题ids:'.json_encode($ids));
  648. foreach ($ids as $key=>$id) {
  649. if ($key==0) {
  650. $query = $query->whereRaw('FIND_IN_SET('.$id.',topic_ids)');
  651. } else {
  652. $query = $query->orWhereRaw('FIND_IN_SET('.$id.',topic_ids)');
  653. }
  654. }
  655. }
  656. })
  657. ->orderBy($sort,'desc')
  658. ->chunk(1, function ($posts) use ($writer, $type){
  659. $data = [];
  660. foreach ($posts as $post) {
  661. if($type == 'post'){
  662. $tmp = [
  663. $post->id,
  664. Carbon::parse($post->created_at)->toDateTimeString(),
  665. $post->username,
  666. $post->location,
  667. implode(' ', $post->topic()->toArray()),
  668. subtext($post->content, 20),
  669. $post->data->pv_real,
  670. $post->data->pv,
  671. $post->data->praise_real_count,
  672. $post->data->praise_count,
  673. $post->data->share_real_count,
  674. $post->data->share_count,
  675. $post->data->collect_real_count,
  676. $post->data->collect_count,
  677. $post->data->comment_count
  678. ];
  679. }else{
  680. $tmp = [
  681. $post->id,
  682. Carbon::parse($post->created_at)->toDateTimeString(),
  683. $post->username,
  684. Carbon::parse($post->created_at)->toDateTimeString(),
  685. subtext($post->content, 20),
  686. $post->data->pv_real,
  687. $post->data->praise_real_count,
  688. $post->data->share_real_count,
  689. $post->data->collect_real_count,
  690. $post->data->comment_count
  691. ];
  692. }
  693. foreach ($tmp as $key => $value) {
  694. $tmp[$key] = iconv('utf-8', 'gbk//IGNORE', $value);
  695. }
  696. $data[] = $tmp;
  697. }
  698. $writer->insertAll($data);
  699. });
  700. Log::channel('download')->info('内容导出成功!');
  701. } catch (CannotInsertRecord $e) {
  702. $e->getRecord();
  703. Log::channel('download')->info('内容导出失败!');
  704. }
  705. }
  706. }