PostRepository.php 26 KB

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