FarmerImporter1.php 3.9 KB
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
 * 导入类 : 老师 导入 操作类
+-----------------------------------------------------------------------------------------------------------------------
 *
 * PHP version 7
 *
 * @category  App\Admin\Extensions\Importer
 * @package   App\Admin\Extensions\Importer
 * @author    Richer <yangzi1028@163.com>
 * @date      2021年11月08日14:45:16
 * @copyright 2021-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
namespace App\Admin\Extensions\Importer;

use Encore\Admin\Actions\Action;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Excel;

/**
 * Class FarmerImporter
 *
 * @category  App\Admin\Extensions\Importer
 * @package   App\Admin\Extensions\Importer
 * @author    Richer <yangzi1028@163.com>
 * @date      2021年11月08日14:45:16
 * @copyright 2021-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
class FarmerImporter1 extends Action
{
    public $name = '导入数据';

    protected $selector = '.import-post';

    public function handle(Request $request)
    {
        // 下面的代码获取到上传的文件,然后使用`maatwebsite/excel`等包来处理上传你的文件,保存到数据库

        // 1、文件获取:取特定的参数名,可以改为配置参数
        $file = $request->file('file', 0);//文件名称

        if (!$file) {
            return $this->response()->error('操作失败,文件不存在。')->refresh();
        }
        // 2、判断文件格式以及各种错误
        // 获取文件的扩展名
        $ext = $file->getClientOriginalExtension();
        if ($ext !== 'xls' && $ext !== 'xlsx') {
            return $this->response()->error('操作失败,文件格式只能为 xls 或者 xlsx。')->refresh();
        }

        // 获取文件的绝对路径
        $path = $file->getRealPath();
        $file_name = date("Ymdhis") . '.' .$ext;
        $origin_name = $file->getClientOriginalName();

        // 3、上传文件到服务器,成功返回true, 失败返回false
        $result = Storage::disk('import')->put($file_name, file_get_contents($path));
        if (!$result) {
            return $this->response()->error('操作失败,文件保存失败!')->refresh();
        }

        Excel::import(new UsersImport, 'users.xlsx');

//        Excel::
        // 4、 读取文件内容
        $inputFileType = PHPExcel_IOFactory::identify($path);
        $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
        $objPHPExcel = $objReader->load($path);
        $sheet = $objPHPExcel->getSheet(0);
        $origin_data = $sheet->toArray();
        if (!$origin_data) {
            $this->message = '操作失败,文件内容为空。';
            return false;
        }

        // 5、将记录保存
        $record = [
            'admin_id' => $this->getLoginUserId('admin'),
            'path'      => $file_name,
            'name'          => $file_name,
            'origin_name'   => $origin_name,
            'origin_data'   => $origin_data,
            'type'          => $type,
        ];
        $model = $this->model->create($record);

        // 6、根据类型解析数据,并进行相关的操作
        $result = $this->dealData($model, $type, $origin_data);

        dd($file);
        $request->file('file');

        // 处理相关数据

        return $this->response()->success('导入完成!')->refresh();
    }

    public function form()
    {
        $this->file('file', '请选择文件')->required();
    }

    public function html()
    {
        return <<<HTML
        <a class="btn btn-sm btn-danger import-post"><i class="fa fa-upload"></i> 导入数据</a>
HTML;
    }
}