页面和command命令行双导入的实现

需求描述:

excel的导入逻辑在web页面和command均可以,command可以显示导入进度条功能。

实现思路:

  1. 创建excel导入类的实现ImportVersionAtomextra,实现接口ToModel, WithHeadingRow, WithBatchInserts,不实现接口WithProgressBar;
  2. 创建excel导入类的command实现ImportVersionAtomextraCmd,继承ImportVersionAtomextra并同时实现WithProgressBar接口;
  3. 在command逻辑调用ImportVersionAtomextraCmd的实例;
  4. 在文件上传逻辑调用ImportVersionAtomextra的实例。

关键代码

  1. excel导入类的实现

    class ImportVersionAtomextra implements ToModel, WithHeadingRow, WithBatchInserts
    {
     public function model(array $row)
     {
         //逻辑实现略
     }
     public function batchSize(): int
     {
         return 100;
     }
    }    
  2. excel导入类的command实现

    class ImportVersionAtomextraCmd extends ImportVersionAtomextra implements WithProgressBar
    {
     use Importable;
    }
  3. command逻辑调用

    class importVersionAtomExtraExcel extends Command
    {
     /**
      * The name and signature of the console command.
      *
      * @var string
      */
     protected $signature = 'import:versionatomextra {versionid : The ID of the version for product} {filepath : the file path}';
    
     /**
      * The console command description.
      *
      * @var string
      */
     protected $description = 'import version atom\'s extra information from excel';
    
     /**
      * Create a new command instance.
      *
      * @return void
      */
     public function __construct()
     {
         parent::__construct();
     }
    
     /**
      * Execute the console command.
      *
      * @return mixed
      */
     public function handle()
     {
         $this->output->title('Starting import');
         $this->info('1. Starting sendfile.');
         Storage::delete('allparalist.xlsx');
         Storage::putFileAs('', new File($this->argument('filepath')), 'allparalist.xlsx');
         $this->output->text('Import successful');
    
         $this->info('2. Starting import version atom extra info.');
         $VerionAtomextra = new ImportVersionAtomextraCmd;
         $VerionAtomextra->versionid = $this->argument('versionid');
         $VerionAtomextra->withOutput($this->output)->import('storage\app\allparalist.xlsx');
         Storage::delete('allparalist.xlsx');
         $this->output->success('Import successful');
     }
    }
  4. 文件上传逻辑调用

    // $model ...
         try{
             // $request ...
             $file = $request->file('file'); 
             $ImportVersionAtomextra = new ImportVersionAtomextra;            
             Excel::import($ImportVersionAtomextra,$file);            
    
             return $this->response()->success('Import success...')->refresh();
         }
         catch (\Exception $e){
             return $this->response()->error($e->getMessage());
         }
    // $model ...
         try{
             // $request ...
             $file = $request->file('file'); 
             $ImportVersionAtomextra = new ImportVersionAtomextra;            
             Excel::import($ImportVersionAtomextra,$file);            
    
             return $this->response()->success('Import success...')->refresh();
         }
         catch (\Exception $e){
             return $this->response()->error($e->getMessage());
         }
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!