Commit cb471566 by 杨树贤

修改上传格式

parent 50d2dfea
...@@ -129,7 +129,7 @@ class SupplierExaminationApiController extends Controller ...@@ -129,7 +129,7 @@ class SupplierExaminationApiController extends Controller
if ($file->isValid()) { if ($file->isValid()) {
$ext = $file->getClientOriginalExtension(); $ext = $file->getClientOriginalExtension();
$realPath = $file->getRealPath(); $realPath = $file->getRealPath();
if ($ext != 'csv') { if ($ext != 'xlsx') {
$this->response(-1, '上传格式错误'); $this->response(-1, '上传格式错误');
} }
// 上传文件 // 上传文件
......
...@@ -53,23 +53,12 @@ class SupplierExaminationService ...@@ -53,23 +53,12 @@ class SupplierExaminationService
ini_set('memory_limit', -1); ini_set('memory_limit', -1);
try { try {
$content = file_get_contents($filePath);
$fileType = mb_detect_encoding($content, array('UTF-8', 'GBK', 'LATIN1', 'BIG5'));
Excel::load($filePath, function ($reader) { Excel::load($filePath, function ($reader) {
$result = $reader->getSheet(0)->toArray(); $data = $reader->getSheet(0)->toArray();
$header = $result[0]; $header = $data[0];
if (!$this->checkImportCsvHeader($header)) { if (!$this->checkImportCsvHeader($header)) {
throw new \Exception('文件不是标准上传模板,请核对后再上传'); throw new \Exception('文件不是标准上传模板,请核对后再上传');
} }
}, $fileType);
$valueBinder = new CsvValueBinder();
$data = Excel::setValueBinder($valueBinder)->load($filePath, $fileType)->get();
if (empty($data)) {
throw new \Exception('上传数据不能为空');
}
$data = $data->toArray();
$dataMd5 = md5(json_encode($data)); $dataMd5 = md5(json_encode($data));
if (SupplierExaminationModel::where('data_md5', $dataMd5)->exists()) { if (SupplierExaminationModel::where('data_md5', $dataMd5)->exists()) {
throw new \Exception('该文件已经上传过,请选择其它文件'); throw new \Exception('该文件已经上传过,请选择其它文件');
...@@ -81,7 +70,10 @@ class SupplierExaminationService ...@@ -81,7 +70,10 @@ class SupplierExaminationService
//拼装数据插入校验,校验完成插入数据库 //拼装数据插入校验,校验完成插入数据库
$examineData = []; $examineData = [];
foreach ($data as $index => $item) { foreach ($data as $index => $item) {
$lineNo = $index + 2; if ($index == 0) {
continue;
}
$lineNo = $index + 1;
$item = array_map(function ($value) { $item = array_map(function ($value) {
return trim($value); return trim($value);
}, $item); }, $item);
...@@ -180,17 +172,17 @@ class SupplierExaminationService ...@@ -180,17 +172,17 @@ class SupplierExaminationService
} }
$examineData = collect($examineData); $examineData = collect($examineData);
foreach ($examineData->chunk(50) as $chunk) { foreach ($examineData->chunk(50) as $chunk) {
SupplierExaminationModel::insert($chunk->toArray()); SupplierExaminationModel::insert($chunk->toArray());
} }
return true; return true;
});
return true;
} catch (\Exception $exception) { } catch (\Exception $exception) {
return $exception->getMessage(); return $exception->getMessage();
} finally { } finally {
unlink($filePath); unlink($filePath);
} }
} }
private function checkImportCsvHeader($header) private function checkImportCsvHeader($header)
......
No preview for this file type
...@@ -207,7 +207,7 @@ ...@@ -207,7 +207,7 @@
elem: '#import_supplier_examination', //绑定元素 elem: '#import_supplier_examination', //绑定元素
url: '/api/supplier_examination/ImportSupplierExamination', //上传接口 url: '/api/supplier_examination/ImportSupplierExamination', //上传接口
auto: true, auto: true,
exts: 'csv', exts: 'xlsx',
before: function (obj) { //obj参数包含的信息,跟 choose回调完全一致,可参见上文。 before: function (obj) { //obj参数包含的信息,跟 choose回调完全一致,可参见上文。
layer.msg('上传中', {icon: 16}); //上传loading layer.msg('上传中', {icon: 16}); //上传loading
}, },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment