returnType = $returnType; return $this; } /** * @param $maxCount * @return $this */ public function setMaxCount($maxCount) { $this->maxCount = $maxCount; return $this; } /** * @param $importFieldConfig * @return $this */ public function setImportFieldConfig($importFieldConfig) { $this->importFieldConfig = $importFieldConfig; return $this; } /** * @return array */ public function getImportFieldConfig() { return $this->importFieldConfig; } /** * @param $columnNum * @return $this */ public function setColumnNum($columnNum) { $this->columnNum = $columnNum; return $this; } /** * @return mixed */ public function getColumnNum() { return $this->columnNum; } /** * @param $dataForm * @return mixed */ public function setDataForm($dataForm) { $this->dataForm = $dataForm; return $this; } /** * @return mixed */ public function getDataForm() { return $this->dataForm; } /** * 获取csv文件数据 * @return array|bool */ public function readCsvData() { $this->basePath = \Yii::$app->basePath; $this->csvConfig = \Yii::$app->params['csvConfig']; try { if (empty($_FILES) || !isset($_FILES['file'])) { return [ 'code' => 1, 'msg' => '请上传csv文件' ]; } $fileName = $_FILES['file']['name']; $tmpName = $_FILES['file']['tmp_name']; $this->path = $this->basePath . '/web/uploads/' . $this->csvConfig['path_folder'] . '/'; if (!is_dir($this->path)) { mkdir($this->path, 0775, true); } $ext = strtolower(pathinfo($fileName, PATHINFO_EXTENSION)); if ($ext != 'csv') { throw new \Exception('请上传csv文件'); } $uploadFile = $this->path . 'ADMIN-' . date('Y-m-d His') . '.' . $ext; //上传 move_uploaded_file($tmpName, $uploadFile); $csvList = $this->readCsv($uploadFile); if (!empty($this->maxCount) && (count($csvList) > $this->maxCount)) { throw new \Exception('单次最多导入' . $this->maxCount . '条数据'); } $data = $csvList; if ($this->returnType == 1) { $data = [ 'fileName' => $fileName, 'tmpName' => $tmpName, 'csvList' => $csvList, ]; } return $data; } catch (\Exception $exception) { self::$static_error = $exception->getMessage(); return false; } } /** * 读取csv文档数据 * @param $file * @return array * @throws \Exception */ public function readCsv($file) { try { setlocale(LC_ALL, array('zh_CN.gbk', 'zh_CN.gb2312', 'zh_CN.gb18030')); $data = [];//返回的文件数据行 if (!is_file($file) && !file_exists($file)) { throw new \Exception('csv文件错误'); } $cvsFile = fopen($file, 'r'); //开始读取csv文件数据 $i = 0;//记录cvs的行 while ($fileData = fgetcsv($cvsFile)) { $i++; if ($i == 1) { $this->verifyTemplate($fileData); continue;//过滤表头 } //判断列数是否与模板一致 if (!empty($this->columnNum) && count($fileData) != $this->columnNum) { throw new \Exception('csv文件数据模板列有误'); } $arr = []; if (!empty($fileData)) { foreach ($this->importFieldConfig as $key => $item) { $arr[$item['field']] = $this->getNewData($fileData[$key]); $data[$i] = $arr; } } } fclose($cvsFile); return array_values($data); } catch (\Exception $exception) { throw $exception; } } /** * 难测模板头是否一致 * @param $fileData * @return bool * @throws \Exception */ private function verifyTemplate($fileData) { try { foreach ($this->importFieldConfig as $key => $item) { if (!isset($fileData[$key])) { throw new \Exception('导入文件的第' . ($key + 1) . '列的标题不存在与模板不一致,正确的标题为:' . $item['field_name']); } if (isset($fileData[$key]) && ($item['field_name'] != $this->getNewData($fileData[$key]))) { throw new \Exception('导入文件的第' . ($key + 1) . '列的标题与模板的不一致,正确的标题为:' . $item['field_name']); } } return true; } catch (\Exception $exception) { throw new \Exception($exception->getMessage()); } } /** * @param $data * @return mixed */ private function getNewData($data) { return mb_convert_encoding(trim($data), "UTF-8", "GBK"); } }