wzq 5 年之前
父节点
当前提交
28dc6b8dff
共有 6 个文件被更改,包括 263 次插入7 次删除
  1. 1 0
      .gitignore
  2. 80 0
      app/Console/Commands/Downloads.php
  3. 8 2
      app/Console/Kernel.php
  4. 18 5
      app/Helper/helper.php
  5. 155 0
      app/Repositories/Post/PostRepository.php
  6. 1 0
      composer.json

+ 1 - 0
.gitignore

@@ -6,3 +6,4 @@ Homestead.yaml
 composer.lock
 /storage/*
 /config/customer.php
+/public/export/*

+ 80 - 0
app/Console/Commands/Downloads.php

@@ -0,0 +1,80 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/6/11
+ * Time: 13:39
+ */
+
+namespace App\Console\Commands;
+
+use App\Repositories\Post\PostRepository;
+use Illuminate\Console\Command;
+use App\Models\Download;
+use Illuminate\Support\Carbon;
+
+class Downloads extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'download:download';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '处理下载';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct(Download $download, PostRepository $postRepository)
+    {
+        parent::__construct();
+        $this->download = $download;
+        $this->postRepository = $postRepository;
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        $this->line("开始检索生成中下载");
+
+        $fileDir = 'export/';
+        $path = public_path($fileDir);
+        if(!file_exists($path)){
+            @mkdir($path, 775);
+        }
+
+        $download = $this->download->where('download_status', 0)->orderBy('id', 'desc')->first();
+
+        if($download->download_type == 'post'){
+            $filePath = $fileDir .'内容-'.Carbon::now()->format('Y-m-d') .'_' .uniqid() .'.csv';
+        }elseif($download->download_type == 'post_waste'){
+            $filePath = $fileDir .'回收站内容-'.Carbon::now()->format('Y-m-d') .'_' .uniqid() .'.csv';
+        }else{
+            exit;
+        }
+
+        try {
+            $this->postRepository->download($filePath, $download->download_type, json_decode($download->params, true));
+            $download->url = $filePath;
+            $download->download_status = 1;
+            $download->save();
+        } catch (\Exception $e) {
+            $download->download_status = 2;
+            $download->url = $e->getMessage();
+            $download->save();
+        }
+    }
+}

+ 8 - 2
app/Console/Kernel.php

@@ -3,6 +3,7 @@
 namespace App\Console;
 
 use App\Console\Commands\Apollo;
+use App\Console\Commands\Downloads;
 use Illuminate\Console\Scheduling\Schedule;
 use Laravel\Lumen\Console\Kernel as ConsoleKernel;
 
@@ -14,7 +15,8 @@ class Kernel extends ConsoleKernel
      * @var array
      */
     protected $commands = [
-        Apollo::class
+        Apollo::class,
+        Downloads::class
     ];
 
     /**
@@ -25,6 +27,10 @@ class Kernel extends ConsoleKernel
      */
     protected function schedule(Schedule $schedule)
     {
-        //
+        $path = storage_path('logs/'.date('Y-m-d').'-schedule.log');
+
+        $schedule->command('download:download')
+            ->everyMinute()
+            ->withoutOverlapping()->appendOutputTo($path);
     }
 }

+ 18 - 5
app/Helper/helper.php

@@ -15,13 +15,26 @@ if ( ! function_exists('config_path'))
     {
         return app()->basePath() . '/config' . ($path ? '/' . $path : $path);
     }
+}
 
-    function subtext($text, $length)
+if (! function_exists('public_path')) {
+    /**
+     * Get the path to the public folder.
+     *
+     * @param  string  $path
+     * @return string
+     */
+    function public_path($path = '')
     {
-        if(mb_strlen($text, 'utf8') > $length) {
-            return mb_substr($text, 0, $length, 'utf8').'...';
-        } else {
-        return $text;
+        return app()->basePath() . '/public' . ($path ? '/' . $path : $path);
     }
 }
+
+function subtext($text, $length)
+{
+    if(mb_strlen($text, 'utf8') > $length) {
+        return mb_substr($text, 0, $length, 'utf8').'...';
+    } else {
+        return $text;
+    }
 }

+ 155 - 0
app/Repositories/Post/PostRepository.php

@@ -18,11 +18,14 @@ use App\Models\Topic;
 use App\Traits\PostTrait;
 use Illuminate\Database\QueryException;
 use Dingo\Api\Http\Response;
+use Illuminate\Support\Carbon;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Log;
 use Illuminate\Support\Facades\Redis;
 use Symfony\Component\HttpKernel\Exception\HttpException;
 use Tymon\JWTAuth\Facades\JWTAuth;
+use League\Csv\Writer;
+use League\Csv\CannotInsertRecord;
 
 class PostRepository
 {
@@ -551,4 +554,156 @@ class PostRepository
             ->paginate($perPage);
     }
 
+    public function download($filePath, $type, $request)
+    {
+        try {
+            set_time_limit(0);
+            if (!ini_get("auto_detect_line_endings")) {
+                ini_set("auto_detect_line_endings", '1');
+            }
+
+            // 文件路径
+            $writer = Writer::createFromPath(public_path($filePath), 'w+');
+
+            // 设置标题
+            if($type == 'post'){
+                $title = [
+                    '内容', date('Y年m月d日')
+                ];
+            }else{
+                $title = [
+                    '回收站内容', date('Y年m月d日')
+                ];
+            }
+
+            $title = eval('return ' . iconv('utf-8', 'gbk//IGNORE', var_export($title, true) . ';'));
+            $writer->insertone($title);
+
+            // 内容
+            if($type == 'post') {
+                $header = [
+                    '内容ID', '发布时间', '用户昵称', '城市', '内容标签', '内容前20个字',
+                    '真实浏览量', '总浏览量', '真实点赞数', '总赞数', '真实分享数', '总分享数',
+                    '真实收藏数', '总收藏数', '评论数'
+                ];
+            } else {
+                $header = [
+                    '内容ID', '发布时间', '用户昵称', '内容标签', '内容前20个字',
+                    '真实浏览量', '真实点赞数', '真实点赞数', '真实分享数', '真实收藏数', '评论数'
+                ];
+            }
+
+            $header = eval('return ' . iconv('utf-8', 'gbk//IGNORE', var_export($header, true) . ';'));
+//            $writer->setOutputBOM(Reader::BOM_UTF8);
+            $writer->insertone($header);
+
+            $where = [];
+            if(isset($request['content'])){
+                $where[] = ['content', 'like', "%{$request['content']}%"];
+            }
+            if(isset($request['is_suggest'])){
+                $where[] = ['is_suggest', $request['is_suggest']];
+            }
+            if(isset($request['type'])){
+                $where[] = ['type', $request['type']];
+            }
+
+            $sort = 'post.id';
+            if(isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])){
+                $sort = $request['sort'];
+            }
+            $post = $this->post;
+            if($type == 'post_waste'){
+                $post = $post->onlyTrashed();
+            }
+
+            $post->join('post_data', 'post_data.post_id', '=', 'post.id')
+                ->select('post.*')
+                ->where($where)
+                ->where(function($query) use ($request){
+                    if(isset($request['keyword'])){
+                        $query->where('uid', '=', $request['keyword'])
+                            ->orWhere('username', 'like', "%{$request['keyword']}%")
+                            ->orWhere('mobile', 'like', "%{$request['keyword']}%");
+                    }
+                })
+                ->where(function($query) use ($request){
+                    if(isset($request['created_at'])){
+                        $time = explode('_', $request['created_at']);
+                        $query->whereBetween('post.created_at', $time);
+                    }
+                })
+                ->where(function ($query) use ($request){
+                    if(isset($request['category_ids']) || isset($request['topic_ids'])){
+                        $ids = [];
+                        if (isset($request['category_ids'])) {
+                            $categoryIds = explode('_', $request['category_ids']);
+                            $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
+                        }
+                        if (isset($request['topic_ids'])) {
+                            $ids = array_merge($ids, explode('_', $request['topic_ids']));
+                        }
+                        Log::debug('话题ids:'.json_encode($ids));
+                        foreach ($ids as $key=>$id) {
+                            if ($key==0) {
+                                $query = $query->whereRaw('FIND_IN_SET('.$id.',topic_ids)');
+                            } else {
+                                $query = $query->orWhereRaw('FIND_IN_SET('.$id.',topic_ids)');
+                            }
+                        }
+                    }
+                })
+                ->orderBy($sort,'desc')
+                ->chunk(1, function ($posts) use ($writer, $type){
+                    $data = [];
+                    foreach ($posts as $post) {
+                        if($type == 'post'){
+                            $tmp = [
+                                $post->id,
+                                Carbon::parse($post->created_at)->toDateTimeString(),
+                                $post->username,
+                                $post->location,
+                                implode(' ', $post->topic()->toArray()),
+                                subtext($post->content, 20),
+                                $post->data->pv_real,
+                                $post->data->pv,
+                                $post->data->praise_real_count,
+                                $post->data->praise_count,
+                                $post->data->share_real_count,
+                                $post->data->share_count,
+                                $post->data->collect_real_count,
+                                $post->data->collect_count,
+                                $post->data->comment_count
+                            ];
+                        }else{
+                            $tmp = [
+                                $post->id,
+                                Carbon::parse($post->created_at)->toDateTimeString(),
+                                $post->username,
+                                '标签',
+                                subtext($post->content, 20),
+                                $post->data->pv_real,
+                                $post->data->praise_real_count,
+                                $post->data->share_real_count,
+                                $post->data->collect_real_count,
+                                $post->data->comment_count
+                            ];
+                        }
+                        foreach ($tmp as $key => $value) {
+                            $tmp[$key] = iconv('utf-8', 'gbk//IGNORE', $value);
+                        }
+
+                        $data[] = $tmp;
+                    }
+                    $writer->insertAll($data);
+            });
+            Log::channel('download')->info('内容导出成功!');
+
+        } catch (CannotInsertRecord $e) {
+            $e->getRecord();
+            Log::channel('download')->info('内容导出失败!');
+        }
+
+    }
+
 }

+ 1 - 0
composer.json

@@ -16,6 +16,7 @@
         "multilinguals/apollo-client": "^0.1.2",
         "illuminate/redis": "^5.8",
         "predis/predis": "^1.1",
+        "league/csv": "^9.1",
         "tymon/jwt-auth": "1.0.0-rc.4.1"
     },
     "require-dev": {