TP6 批量导入

php代码:

            //获取上传文件信息
            $files = request()->file();
            if(!$files){
                $this->error('请上传excel文件');
            }
            if(!isset($files['addexcel'])){
                $this->error('请上传excel文件');
            }
            $file = $files['addexcel'];
            $first = upload_files($file, $filepath='excel', $rule='',$fileExt='xlsx,xls',$fileMime='', $size = 5242880, $isSaveName=false); 
            if ($first['status'] == 0) {
                $this->error($first['msg']);
            }
            $path = $first['msg'];
            //1000b=1kb  大小不超过10mb
            // $info = $file->validate(['size'=>10000000 ,'ext'=>'xlsx,xls,csv'])->move(ROOT_PATH . 'public' . DS . 'excel');
            if($path){
                //获取上传文件地址
                $picCover = \think\facade\Filesystem::getDiskConfig('uploadfile','url').'/'.str_replace('\\','/',$path);
                $file_name = root_path() . 'public' . $picCover;
                //获取上传文件后缀
                $temp = explode(".",$path);
                $name = end($temp);                
                //引入类
                // vendor("PHPExcel.PHPExcel.IOFactory");
                require_once(root_path() . '/vendor/PHPExcel/PHPExcel.php');
                if($name =='xlsx' ){
                    $objReader =\PHPExcel_IOFactory::createReader('Excel2007');
                }else{
                    $objReader = \PHPExcel_IOFactory::createReader('Excel5'); 
                }
                //加载文件内容 utf8geshi
                $obj_PHPExcel =$objReader->load($file_name, $encode = 'utf-8');
                //转换为数组格式
                $excel_array=$obj_PHPExcel->getsheet(0)->toArray();
                //输出数组中的当前元素和下一个元素的值,然后把数组的内部指针重置到数组中的第一个元素
                $arr  = reset($excel_array);
                //销毁标题
                unset($excel_array[0]); 
                dump($excel_array);
                $data = [];  
                $i=0;  
                foreach ($excel_array as $k => $v) {
                    //客户
                    $customer_id = (new CustomerModel())->where(['name'=>$v[0]])->value('id');
                    if(!$customer_id){
                        $this->error('客户名称无效: '.$v[0]);
                    }
                    //货物分类
                    $cateid = (new GoodsCateModel())->where(['name'=>$v[1]])->value('id');
                    if(!$cateid){
                        $this->error('货物分类无效: '.$v[1]);
                    }
                    //计量单位
                    $unitid = (new GoodsUnitModel())->where(['name'=>$v[3]])->value('id');
                    if(!$unitid){
                        $this->error('计量单位无效: '.$v[3]);
                    }
                    $data[$k]['customer_id'] = $customer_id; 
                    $data[$k]['cateid'] = $cateid; 
                    $data[$k]['title'] = $v[2]; 
                    $data[$k]['unitid'] = $unitid; 
                    $data[$k]['long'] =$v[4]; 
                    $data[$k]['wide'] = $v[5]; 
                    $data[$k]['high'] = $v[6]; 
                    $data[$k]['weight'] = $v[7]; 
                    $data[$k]['guige'] = $v[8]; 
                    $data[$k]['code_layer'] = $v[9]; 
                    $data[$k]['code_layer_number'] = $v[10]; 
                    $data[$k]['remark'] = $v[11];   //备注
                    $data[$k]['volume'] = $v[4]*$v[5]*$v[6];     //计算货物体积
                    $data[$k]['code'] = $this->model->createCode($customer_id);     //货物编码
                    $data[$k]['barcode'] = $this->callcheckuser();     //货物条码
                    $data[$k]['create_people_type'] = 1;     //创建人类型:1-管理员,2-客户
                    $data[$k]['create_people'] = $this->adminInfo['id'];
                    $data[$k]['status'] = 1;
                    $i++; 
                }
                try {                
                    $res = $this->model->saveAll($data);
                } catch (\Exception $e) {
                    $this->error('保存失败'.$e->getMessage());
                }
                if($res){
                    $this->success('保存成功');
                }else{
                    $this->error('保存失败');
                }
            }else{
                $this->error('上传失败');
            }

common.php文件:


/**
 * $file 上传文件对象
 * $filepath 上传目录名,例如:'workerorder'
 * $rule 上传命名规则,例如:md5,sha1,date
 * $fileExt 上传文件后缀
 * $fileMime 上传文件类型
 * $size  上传文件的大小 默认为(5242880=5*1024*1024)  5MB
 * $isSaveName 以指定的文件名保存,例如:传入'abc.jpg'即可调用putFileAs方法
 */
function upload_files($file, $filepath, $rule='',$fileExt='jpg,jpeg,png,bmp,gif',$fileMime='image/jpeg,image/png,image/gif', $size = 5242880, $isSaveName=false) {
    try {
        // 验证
        validate(['imgFile'=>[
            'fileSize' => $size,
            'fileExt' => $fileExt,
            // 'fileMime' => $fileMime,
        ]])->check(['imgFile' => $file]);
        // 保存图片
        if($isSaveName === false){
            $path = \think\facade\Filesystem::disk('uploadfile')->putFile( $filepath, $file,$rule);
            //结果是 $path = workerorder/20200825\***.jpg
        }else{
            $path = \think\facade\Filesystem::disk('uploadfile')->putFileAs( $filepath, $file,$isSaveName);    
            //结果是 $path = workerorder/abc.jpg
        }
        // 图片路径,Filesystem::getDiskConfig('public','url')功能是获取public目录下的storage,
        // $picCover = \think\facade\Filesystem::getDiskConfig('uploadfile','url').'/'.str_replace('\\','/',$path);
        // 结果是 $picCover = upload/workerorder/20200825/***.jpg
        return ['status' => 9, 'msg' => str_replace('\\','/',$path)];
    } catch (\think\exception\ValidateException $e) {
        return ['status' => 0, 'msg' => '上传失败:'.$e->getMessage()];
    }
}

filesystem.php

use think\facade\Env;

return [
    // 默认磁盘
    'default' => Env::get('filesystem.driver', 'local'),
    // 磁盘列表
    'disks'   => [
        'local'  => [
            'type' => 'local',
            'root' => app()->getRuntimePath() . 'storage',
        ],
        'public' => [
            // 磁盘类型
            'type'       => 'local',
            // 磁盘路径
            'root'       => app()->getRootPath() . 'public',
            // 磁盘路径对应的外部URL路径
            'url'        => '/storage',
            // 可见性
            'visibility' => 'public',
        ],

        //定义上传图片配置
        'uploadfile' => [
            // 磁盘类型
            'type'       => 'local',
            // 磁盘路径
            'root'       => app()->getRootPath() . 'public/uploadfile',
            // 磁盘路径对应的外部URL路径
            'url'        => '/uploadfile',
            // 可见性
            'visibility' => 'public',
        ],
        // 更多的磁盘配置信息
    ],
];

上一篇:tp6 redis 安装与存取数据


下一篇:crmeb最后一个全开源版本。使用TP6框架、多端适配。适合二次开发