Downloads.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/11
  6. * Time: 13:39
  7. */
  8. namespace App\Console\Commands;
  9. use App\Repositories\Post\PostRepository;
  10. use Illuminate\Console\Command;
  11. use App\Models\Download;
  12. use Illuminate\Support\Carbon;
  13. class Downloads extends Command
  14. {
  15. /**
  16. * The name and signature of the console command.
  17. *
  18. * @var string
  19. */
  20. protected $signature = 'download:download';
  21. /**
  22. * The console command description.
  23. *
  24. * @var string
  25. */
  26. protected $description = '处理下载';
  27. /**
  28. * Create a new command instance.
  29. *
  30. * @return void
  31. */
  32. public function __construct(Download $download, PostRepository $postRepository)
  33. {
  34. parent::__construct();
  35. $this->download = $download;
  36. $this->postRepository = $postRepository;
  37. }
  38. /**
  39. * Execute the console command.
  40. *
  41. * @return mixed
  42. */
  43. public function handle()
  44. {
  45. $this->line("开始检索生成中下载");
  46. $fileDir = 'export/';
  47. $path = public_path($fileDir);
  48. if(!file_exists($path)){
  49. @mkdir($path, 775);
  50. }
  51. $download = $this->download->where('download_status', 0)->orderBy('id', 'desc')->first();
  52. if($download->download_type == 'post'){
  53. $filePath = $fileDir .'内容-'.Carbon::now()->format('Y-m-d') .'_' .uniqid() .'.csv';
  54. }elseif($download->download_type == 'post_waste'){
  55. $filePath = $fileDir .'回收站内容-'.Carbon::now()->format('Y-m-d') .'_' .uniqid() .'.csv';
  56. }else{
  57. exit;
  58. }
  59. try {
  60. $this->postRepository->download($filePath, $download->download_type, json_decode($download->params, true));
  61. $download->url = $filePath;
  62. $download->download_status = 1;
  63. $download->save();
  64. } catch (\Exception $e) {
  65. $download->download_status = 2;
  66. $download->url = $e->getMessage();
  67. $download->save();
  68. }
  69. }
  70. }