PostRepository.php 26 KB

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