PostRepository.php 27 KB

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