PostRepository.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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. 'post_id' => $post->id,
  258. 'content' => subtext($request['content'], 20),
  259. ]
  260. ]);
  261. }else{
  262. $this->rabbitMqUtil->push('add_message', [
  263. 'uid' => $post->uid,
  264. 'message_show_type' => 'post_comment',
  265. 'param' => [
  266. 'uid' => $userInfo['uid'],
  267. 'username' => $userInfo['username'],
  268. 'post_id' => $post->id,
  269. 'content' => subtext($request['content'], 20),
  270. ]
  271. ]);
  272. }
  273. DB::beginTransaction();
  274. try{
  275. $this->postComment->create($data);
  276. $post->data->comment_count += 1;
  277. $post->data->save();
  278. DB::commit();
  279. return Response::create();
  280. }catch (QueryException $exception){
  281. DB::rollBack();
  282. Log::debug('评论内容:'.$request['post_id'].$exception->getMessage());
  283. return Response::create([
  284. 'message' => '评论失败,请重试',
  285. 'error' => $exception->getMessage(),
  286. 'status_code' => 500
  287. ]);
  288. }
  289. }
  290. /**
  291. * 内容列表
  292. */
  293. public function lists($request)
  294. {
  295. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  296. $where = [];
  297. if(isset($request['content'])){
  298. $where[] = ['content', 'like', "%{$request['content']}%"];
  299. }
  300. if(isset($request['is_suggest'])){
  301. $where[] = ['is_suggest', $request['is_suggest']];
  302. }
  303. if(isset($request['type'])){
  304. $where[] = ['type', $request['type']];
  305. }
  306. if(isset($request['uid'])){
  307. $where[] = ['uid', $request['uid']];
  308. }
  309. $sort = 'post.id';
  310. if(isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])){
  311. $sort = $request['sort'];
  312. }
  313. $post = $this->post;
  314. if(isset($request['waste']) && $request['waste'] == 1){
  315. $post = $post->onlyTrashed();
  316. }
  317. return $post
  318. ->join('post_data', 'post_data.post_id', '=', 'post.id')
  319. ->select('post.*')
  320. ->where($where)
  321. ->where(function($query) use ($request){
  322. if(isset($request['keyword'])){
  323. $query->where('uid', '=', $request['keyword'])
  324. ->orWhere('username', 'like', "%{$request['keyword']}%")
  325. ->orWhere('mobile', 'like', "%{$request['keyword']}%");
  326. }
  327. })
  328. ->where(function($query) use ($request){
  329. if(isset($request['created_at'])){
  330. $time = explode('_', $request['created_at']);
  331. $query->whereBetween('post.created_at', $time);
  332. }
  333. })
  334. ->where(function ($query) use ($request){
  335. if(isset($request['category_ids']) || isset($request['topic_ids'])){
  336. $ids = [];
  337. if (isset($request['category_ids'])) {
  338. $categoryIds = explode('_', $request['category_ids']);
  339. $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
  340. }
  341. if (isset($request['topic_ids'])) {
  342. $ids = array_merge($ids, explode('_', $request['topic_ids']));
  343. }
  344. foreach ($ids as $key=>$id) {
  345. if ($key==0) {
  346. $query = $query->whereRaw('FIND_IN_SET('.$id.',topic_ids)');
  347. } else {
  348. $query = $query->orWhereRaw('FIND_IN_SET('.$id.',topic_ids)');
  349. }
  350. }
  351. }
  352. })
  353. ->orderBy($sort,'desc')
  354. ->paginate($perPage);
  355. }
  356. /**
  357. * 内容详情
  358. */
  359. public function detail($request)
  360. {
  361. return $this->post->withTrashed()->find($request['id']);
  362. }
  363. /**
  364. * 评论列表
  365. */
  366. public function commentList($request)
  367. {
  368. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  369. $where = [];
  370. if(isset($request['post_id'])){
  371. $where[] = ['post_id', $request['post_id']];
  372. }
  373. if(isset($request['uid'])){
  374. $where[] = ['uid', $request['uid']];
  375. }
  376. return $this->postComment
  377. ->where($where)
  378. ->orderBy('id','desc')
  379. ->paginate($perPage);
  380. }
  381. /**
  382. * 推荐内容
  383. */
  384. public function suggest($request)
  385. {
  386. $post = $this->post->where('id', $request['id'])->first();
  387. if(!$post){
  388. return Response::create([
  389. 'message' => '获取内容信息失败',
  390. 'status_code' => 500
  391. ]);
  392. }
  393. if($post->is_suggest == 1){
  394. $post->is_suggest = 0;
  395. }else{
  396. $post->is_suggest = 1;
  397. }
  398. DB::beginTransaction();
  399. try{
  400. $post->save();
  401. DB::commit();
  402. return Response::create();
  403. }catch (QueryException $exception){
  404. DB::rollBack();
  405. Log::debug('推荐内容:'.$request['id'].$exception->getMessage());
  406. return Response::create([
  407. 'message' => '操作失败,请重试',
  408. 'error' => $exception->getMessage(),
  409. 'status_code' => 500
  410. ]);
  411. }
  412. }
  413. /**
  414. * 删除内容
  415. */
  416. public function delete($request)
  417. {
  418. $post = $this->post->where('id', $request['id'])->first();
  419. if(!$post){
  420. return Response::create([
  421. 'message' => '获取内容信息失败',
  422. 'status_code' => 500
  423. ]);
  424. }
  425. DB::beginTransaction();
  426. try{
  427. $post->delete();
  428. DB::commit();
  429. return Response::create();
  430. }catch (QueryException $exception){
  431. DB::rollBack();
  432. Log::debug('删除内容:'.$request['id'].$exception->getMessage());
  433. return Response::create([
  434. 'message' => '操作失败,请重试',
  435. 'error' => $exception->getMessage(),
  436. 'status_code' => 500
  437. ]);
  438. }
  439. }
  440. /**
  441. * 复原内容
  442. */
  443. public function restore($request)
  444. {
  445. $post = $this->post->withTrashed()->where('id', $request['id'])->first();
  446. if(!$post){
  447. return Response::create([
  448. 'message' => '获取内容信息失败',
  449. 'status_code' => 500
  450. ]);
  451. }
  452. DB::beginTransaction();
  453. try{
  454. $post->restore();
  455. DB::commit();
  456. return Response::create();
  457. }catch (QueryException $exception){
  458. DB::rollBack();
  459. Log::debug('复原内容:'.$request['id'].$exception->getMessage());
  460. return Response::create([
  461. 'message' => '操作失败,请重试',
  462. 'error' => $exception->getMessage(),
  463. 'status_code' => 500
  464. ]);
  465. }
  466. }
  467. /**
  468. * 删除评论
  469. */
  470. public function commentDelete($request)
  471. {
  472. $comment = $this->postComment->find($request['id']);
  473. if(!$comment){
  474. return Response::create([
  475. 'message' => '获取评论信息失败',
  476. 'status_code' => 500
  477. ]);
  478. }
  479. if($comment->is_delete == 1){
  480. return Response::create([
  481. 'message' => '该评论已经删除',
  482. 'status_code' => 500
  483. ]);
  484. }
  485. DB::beginTransaction();
  486. try{
  487. $comment->is_delete = 1;
  488. $comment->save();
  489. DB::commit();
  490. return Response::create();
  491. }catch (QueryException $exception){
  492. DB::rollBack();
  493. Log::debug('删除评论:'.$request['id'].$exception->getMessage());
  494. return Response::create([
  495. 'message' => '操作失败,请重试',
  496. 'error' => $exception->getMessage(),
  497. 'status_code' => 500
  498. ]);
  499. }
  500. }
  501. /**
  502. * 隐藏内容
  503. */
  504. public function hide($request)
  505. {
  506. $post = $this->post->where('id', $request['id'])->first();
  507. if(!$post){
  508. return Response::create([
  509. 'message' => '获取内容信息失败',
  510. 'status_code' => 500
  511. ]);
  512. }
  513. if($post->is_hide == 1){
  514. $post->is_hide = 0;
  515. }else{
  516. $post->is_hide = 1;
  517. }
  518. DB::beginTransaction();
  519. try{
  520. $post->save();
  521. DB::commit();
  522. return Response::create();
  523. }catch (QueryException $exception){
  524. DB::rollBack();
  525. Log::debug('隐藏内容:'.$request['id'].$exception->getMessage());
  526. return Response::create([
  527. 'message' => '操作失败,请重试',
  528. 'error' => $exception->getMessage(),
  529. 'status_code' => 500
  530. ]);
  531. }
  532. }
  533. /**
  534. * 日志列表
  535. */
  536. public function log($request)
  537. {
  538. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  539. $where = [];
  540. if(isset($request['log_type'])){
  541. $where[] = ['log_type', $request['log_type']];
  542. }
  543. return $this->postLog
  544. ->where($where)
  545. ->where(function($query) use ($request){
  546. if(isset($request['created_at'])){
  547. $time = explode('_', $request['created_at']);
  548. $query->whereBetween('created_at', $time);
  549. }
  550. })
  551. ->orderBy('id','desc')
  552. ->paginate($perPage);
  553. }
  554. public function download($filePath, $type, $request)
  555. {
  556. try {
  557. set_time_limit(0);
  558. if (!ini_get("auto_detect_line_endings")) {
  559. ini_set("auto_detect_line_endings", '1');
  560. }
  561. // 文件路径
  562. $writer = Writer::createFromPath(public_path($filePath), 'w+');
  563. // 设置标题
  564. if($type == 'post'){
  565. $title = [
  566. '内容', date('Y年m月d日')
  567. ];
  568. }else{
  569. $title = [
  570. '回收站内容', date('Y年m月d日')
  571. ];
  572. }
  573. $title = eval('return ' . iconv('utf-8', 'gbk//IGNORE', var_export($title, true) . ';'));
  574. $writer->insertone($title);
  575. // 内容
  576. if($type == 'post') {
  577. $header = [
  578. '内容ID', '发布时间', '用户昵称', '城市', '内容标签', '内容前20个字',
  579. '真实浏览量', '总浏览量', '真实点赞数', '总赞数', '真实分享数', '总分享数',
  580. '真实收藏数', '总收藏数', '评论数'
  581. ];
  582. } else {
  583. $header = [
  584. '内容ID', '发布时间', '用户昵称', '内容标签', '内容前20个字',
  585. '真实浏览量', '真实点赞数', '真实点赞数', '真实分享数', '真实收藏数', '评论数'
  586. ];
  587. }
  588. $header = eval('return ' . iconv('utf-8', 'gbk//IGNORE', var_export($header, true) . ';'));
  589. // $writer->setOutputBOM(Reader::BOM_UTF8);
  590. $writer->insertone($header);
  591. $where = [];
  592. if(isset($request['content'])){
  593. $where[] = ['content', 'like', "%{$request['content']}%"];
  594. }
  595. if(isset($request['is_suggest'])){
  596. $where[] = ['is_suggest', $request['is_suggest']];
  597. }
  598. if(isset($request['type'])){
  599. $where[] = ['type', $request['type']];
  600. }
  601. $sort = 'post.id';
  602. if(isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])){
  603. $sort = $request['sort'];
  604. }
  605. $post = $this->post;
  606. if($type == 'post_waste'){
  607. $post = $post->onlyTrashed();
  608. }
  609. $post->join('post_data', 'post_data.post_id', '=', 'post.id')
  610. ->select('post.*')
  611. ->where($where)
  612. ->where(function($query) use ($request){
  613. if(isset($request['keyword'])){
  614. $query->where('uid', '=', $request['keyword'])
  615. ->orWhere('username', 'like', "%{$request['keyword']}%")
  616. ->orWhere('mobile', 'like', "%{$request['keyword']}%");
  617. }
  618. })
  619. ->where(function($query) use ($request){
  620. if(isset($request['created_at'])){
  621. $time = explode('_', $request['created_at']);
  622. $query->whereBetween('post.created_at', $time);
  623. }
  624. })
  625. ->where(function ($query) use ($request){
  626. if(isset($request['category_ids']) || isset($request['topic_ids'])){
  627. $ids = [];
  628. if (isset($request['category_ids'])) {
  629. $categoryIds = explode('_', $request['category_ids']);
  630. $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
  631. }
  632. if (isset($request['topic_ids'])) {
  633. $ids = array_merge($ids, explode('_', $request['topic_ids']));
  634. }
  635. Log::debug('话题ids:'.json_encode($ids));
  636. foreach ($ids as $key=>$id) {
  637. if ($key==0) {
  638. $query = $query->whereRaw('FIND_IN_SET('.$id.',topic_ids)');
  639. } else {
  640. $query = $query->orWhereRaw('FIND_IN_SET('.$id.',topic_ids)');
  641. }
  642. }
  643. }
  644. })
  645. ->orderBy($sort,'desc')
  646. ->chunk(1, function ($posts) use ($writer, $type){
  647. $data = [];
  648. foreach ($posts as $post) {
  649. if($type == 'post'){
  650. $tmp = [
  651. $post->id,
  652. Carbon::parse($post->created_at)->toDateTimeString(),
  653. $post->username,
  654. $post->location,
  655. implode(' ', $post->topic()->toArray()),
  656. subtext($post->content, 20),
  657. $post->data->pv_real,
  658. $post->data->pv,
  659. $post->data->praise_real_count,
  660. $post->data->praise_count,
  661. $post->data->share_real_count,
  662. $post->data->share_count,
  663. $post->data->collect_real_count,
  664. $post->data->collect_count,
  665. $post->data->comment_count
  666. ];
  667. }else{
  668. $tmp = [
  669. $post->id,
  670. Carbon::parse($post->created_at)->toDateTimeString(),
  671. $post->username,
  672. Carbon::parse($post->created_at)->toDateTimeString(),
  673. subtext($post->content, 20),
  674. $post->data->pv_real,
  675. $post->data->praise_real_count,
  676. $post->data->share_real_count,
  677. $post->data->collect_real_count,
  678. $post->data->comment_count
  679. ];
  680. }
  681. foreach ($tmp as $key => $value) {
  682. $tmp[$key] = iconv('utf-8', 'gbk//IGNORE', $value);
  683. }
  684. $data[] = $tmp;
  685. }
  686. $writer->insertAll($data);
  687. });
  688. Log::channel('download')->info('内容导出成功!');
  689. } catch (CannotInsertRecord $e) {
  690. $e->getRecord();
  691. Log::channel('download')->info('内容导出失败!');
  692. }
  693. }
  694. }