| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- <?php
- /**
- * @link:http://www.gdqijianshi.com/
- * @copyright: Copyright (c) 2021 广东七件事集团
- * 用户操作
- * Author: zx
- * Date: 2021-03-03
- * Time: 13:30
- */
- namespace common\helpers\csv;
- use app\core\ApiCode;
- use app\models\BaseModel;
- use common\models\base\BaseActiveRecord;
- class CsvImportForm extends BaseActiveRecord
- {
- /**
- * 当前模板列数
- * @var
- */
- protected $columnNum = 0;
- /**
- * 数据读取、组装、验证等配置信息
- * @var array
- */
- protected $importFieldConfig = [];
- /**
- * 文件信息
- * @var
- */
- protected $file;
- /**
- * 文件路径
- * @var
- */
- protected $file_path;
- /**
- * 当前上传文件数
- * @var
- */
- protected $current_num = 1;
- /**
- * 系统根路径
- * @var
- */
- protected $basePath;
- /**
- *最终上传的文件路径
- * @var
- */
- protected $path;
- /**
- * csv配置信息
- * @var
- */
- protected $csvConfig;
- /**
- * 一次最大可以导入数据量
- * @var int
- */
- protected $maxCount = 0;
- /**
- * 接收到数据的form表单
- * @var
- */
- protected $dataForm;
- /**
- * 返回数据类型
- * @var
- */
- protected $returnType = 0;
- /**
- * @param $returnType
- * @return $this
- */
- public function setReturnType($returnType)
- {
- $this->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");
- }
- }
|