123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019/6/11
- * Time: 13:39
- */
- namespace App\Console\Commands;
- use App\Repositories\Behavior\BehaviorRepository;
- use App\Repositories\Post\PostRepository;
- use Illuminate\Console\Command;
- use App\Models\Download;
- use Illuminate\Support\Carbon;
- use Illuminate\Support\Facades\Storage;
- 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,BehaviorRepository $behaviorRepository)
- {
- parent::__construct();
- $this->download = $download;
- $this->postRepository = $postRepository;
- $this->behaviorRepository = $behaviorRepository;
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $this->line("开始检索生成中下载");
- $fileDir = 'export/';
- $path = public_path($fileDir);
- if(!file_exists($path)){
- mkdir($path, 0775);
- }
- $download = $this->download->where('download_status', 0)->orderBy('id', 'desc')->first();
- if(!$download) exit;
- if($download->download_type == 'post'){
- $fileName = '内容-'.Carbon::now()->format('Y-m-d') .'_' .uniqid() .'.csv';
- }elseif($download->download_type == 'post_waste'){
- $fileName = '回收站内容-'.Carbon::now()->format('Y-m-d') .'_' .uniqid() .'.csv';
- }elseif($download->download_type == 'registered_record'){
- $fileName = '新用户注册账本-'.Carbon::now()->format('Y-m-d') .'_' .uniqid() .'.csv';
- }elseif($download->download_type == 'comment_record'){
- $fileName = '评论账本-'.Carbon::now()->format('Y-m-d') .'_' .uniqid() .'.csv';
- }elseif($download->download_type == 'release_record'){
- $fileName = '发布账本-'.Carbon::now()->format('Y-m-d') .'_' .uniqid() .'.csv';
- }elseif($download->download_type == 'general_record'){
- $fileName = '普通行为账本-'.Carbon::now()->format('Y-m-d') .'_' .uniqid() .'.csv';
- }else{
- exit;
- }
- $download->download_status = 1;
- $download->save();
- $filePath = $fileDir .$download->username.$fileName;
- try {
- if ($download->download_type == 'post' || $download->download_type == 'post_waste'){
- $this->postRepository->download($filePath, $download->download_type, json_decode($download->params, true));
- }else{
- $this->behaviorRepository->download($filePath, $download->download_type, json_decode($download->params, true));
- }
- Storage::put($filePath, file_get_contents(public_path($filePath)));
- $download->url = $filePath;
- $download->download_status = 2;
- $download->save();
- unlink(public_path($filePath));
- } catch (\Exception $e) {
- $download->download_status = 3;
- $download->url = $e->getMessage();
- $download->save();
- }
- }
- }
|