Downloads.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. use Illuminate\Support\Facades\Storage;
  14. class Downloads extends Command
  15. {
  16. /**
  17. * The name and signature of the console command.
  18. *
  19. * @var string
  20. */
  21. protected $signature = 'download:download';
  22. /**
  23. * The console command description.
  24. *
  25. * @var string
  26. */
  27. protected $description = '处理下载';
  28. /**
  29. * Create a new command instance.
  30. *
  31. * @return void
  32. */
  33. public function __construct(Download $download, PostRepository $postRepository)
  34. {
  35. parent::__construct();
  36. $this->download = $download;
  37. $this->postRepository = $postRepository;
  38. }
  39. /**
  40. * Execute the console command.
  41. *
  42. * @return mixed
  43. */
  44. public function handle()
  45. {
  46. $this->line("开始检索生成中下载");
  47. $fileDir = 'export/';
  48. $path = public_path($fileDir);
  49. if(!file_exists($path)){
  50. mkdir($path, 0775);
  51. }
  52. $download = $this->download->where('download_status', 0)->orderBy('id', 'desc')->first();
  53. if(!$download) exit;
  54. if($download->download_type == 'post'){
  55. $fileName = '内容-'.Carbon::now()->format('Y-m-d') .'_' .uniqid() .'.csv';
  56. }elseif($download->download_type == 'post_waste'){
  57. $fileName = '回收站内容-'.Carbon::now()->format('Y-m-d') .'_' .uniqid() .'.csv';
  58. }elseif($download->download_type == 'registered_record'){
  59. $fileName = '新用户注册账本-'.Carbon::now()->format('Y-m-d') .'_' .uniqid() .'.csv';
  60. }elseif($download->download_type == 'comment_record'){
  61. $fileName = '评论账本-'.Carbon::now()->format('Y-m-d') .'_' .uniqid() .'.csv';
  62. }elseif($download->download_type == 'release_record'){
  63. $fileName = '发布账本-'.Carbon::now()->format('Y-m-d') .'_' .uniqid() .'.csv';
  64. }elseif($download->download_type == 'general_record'){
  65. $fileName = '唯一/普通行为账本-'.Carbon::now()->format('Y-m-d') .'_' .uniqid() .'.csv';
  66. }else{
  67. exit;
  68. }
  69. $download->download_status = 1;
  70. $download->save();
  71. $filePath = $fileDir .$download->username.$fileName;
  72. try {
  73. $this->postRepository->download($filePath, $download->download_type, json_decode($download->params, true));
  74. Storage::put(public_path($filePath), file_get_contents(public_path($filePath)));
  75. $download->url = $filePath;
  76. $download->download_status = 2;
  77. $download->save();
  78. unlink(public_path($filePath));
  79. } catch (\Exception $e) {
  80. $download->download_status = 3;
  81. $download->url = $e->getMessage();
  82. $download->save();
  83. }
  84. }
  85. }