csv文件导入之坑(大坑,不注意全盘皆奔)

接手新公司项目要做批量插入和批量更新商品信息功能的开发;这可以说是遇到了很多坑;首先是csv留空和输入0在php接收处理的方式是不一样的,empty没用的,产品又允许客户可填可不填等等;然后我就参考了公司项目就方法的写法进行搬砖操作;

//例子大概:
public function actionBatchPrice()
{
    set_time_limit(0);
    if (\Yii::$app->request->isPost) {
        $file = \Yii::$app->request->post();
        if (!$file['url']) {
            return [
                'code' => 1,
                'msg' => '请输入模板地址'
            ];
        }
        $fileName = $file['url'];
        //将文件一次性全部读出来
        $excelData = $insertRows = [];
        $content = str_replace("\n,,", '', trim(file_get_contents($fileName)));
        $excelData = explode("\n", $content);
        unset($excelData[0]);
        $chunkData = array_chunk($excelData, 200);
        $count = count($chunkData);
        if ($count > 30) {
            return [
                'code' => 1,
                'msg' => '允许导入最大限制数为6000条,导入失败,请筛选数据后重新导入。'
            ];
        }
        $page = isset($file['page']) ? $file['page'] : 0;
        //这里就是大bug的地方,当客户输入英文字符逗号的时候数据都会错乱了!!!!!!!!!!!
        //但是奇怪的时候,客户都很听话,这个系统运行2年了都没有人输错过,这是服了!!!!!!!
        foreach ($chunkData[$page] as $value) {
            $string = mb_convert_encoding(trim(strip_tags(str_replace('"', '', $value))), 'utf-8', 'gbk');//转码
            $v = explode(',', trim($string));
            $insertRows[] = $v;
        }
        $form = new GoodsForm();
        $form->store_id = $this->store->id;
        $form->plugin = get_plugin_type();
        $data = $form->batch($insertRows);
        $data['page'] = $page;
        $data['count'] = $count;
        return [
            'code' => 0,
            'msg' => '操作成功',
            'data' => $data,
        ];
    }
    return $this->render('batch-price', [
    ]);
}

 

 

改造后方法大概如下:

//商品信息管理增加批量导入功能  by Mr.DJ
public function actionBatchInsert()
{
    set_time_limit(0);
    if (\Yii::$app->request->isPost) {
        $file = \Yii::$app->request->post();
        if (!$file['url']) {
            return [
                'code' => 1,
                'msg' => '请输入模板地址'
            ];
        }
        $fileName = $file['url'];
        //将文件一次性全部读出来
        $excelData = $insertRows = [];
        $fp = fopen($fileName,'r');                                //Mr.DJ修复系统csv存在英文逗号输入即爆炸bug!
        while ( ($row = fgetcsv($fp)) !== FALSE ) {
            $excelData[] = $row;
        }
        fclose($fp);
        unset($excelData[0]);
        $excelData = eval('return '.iconv('gbk','utf-8',var_export($excelData,true)).';');
        if (empty($excelData)) {
            return [
                'code' => 1,
                'msg' => '数据为空,请重新确认。'
            ];
        }
        $chunkData = array_chunk($excelData, 200);  //200个做一个记录对象
        $count = count($chunkData);
        if ($count > 30) {
            return [
                'code' => 1,
                'msg' => '允许导入最大限制数为6000条,导入失败,请筛选数据后重新导入。'
            ];
        }
        $page = isset($file['page']) ? $file['page'] : 0;

        $form = new GoodsInsertForm();  //限制属性传送
        $form->store_id = $this->store->id;
        $form->plugin = get_plugin_type();
        $data = $form->batchInsert($excelData);
        $data['page'] = $page;
        $data['count'] = $count;
        return [
            'code' => 0,
            'msg' => '操作成功',
            'data' => $data,
        ];
    }
    return $this->render('batch-insert', [
    ]);
}

你可能感兴趣的:(YII)