Downloads.php 3.3 KB

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