Commit cb471566 by 杨树贤

修改上传格式

parent 50d2dfea
......@@ -129,7 +129,7 @@ class SupplierExaminationApiController extends Controller
if ($file->isValid()) {
$ext = $file->getClientOriginalExtension();
$realPath = $file->getRealPath();
if ($ext != 'csv') {
if ($ext != 'xlsx') {
$this->response(-1, '上传格式错误');
}
// 上传文件
......
......@@ -53,23 +53,12 @@ class SupplierExaminationService
ini_set('memory_limit', -1);
try {
$content = file_get_contents($filePath);
$fileType = mb_detect_encoding($content, array('UTF-8', 'GBK', 'LATIN1', 'BIG5'));
Excel::load($filePath, function ($reader) {
$result = $reader->getSheet(0)->toArray();
$header = $result[0];
$data = $reader->getSheet(0)->toArray();
$header = $data[0];
if (!$this->checkImportCsvHeader($header)) {
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));
if (SupplierExaminationModel::where('data_md5', $dataMd5)->exists()) {
throw new \Exception('该文件已经上传过,请选择其它文件');
......@@ -81,7 +70,10 @@ class SupplierExaminationService
//拼装数据插入校验,校验完成插入数据库
$examineData = [];
foreach ($data as $index => $item) {
$lineNo = $index + 2;
if ($index == 0) {
continue;
}
$lineNo = $index + 1;
$item = array_map(function ($value) {
return trim($value);
}, $item);
......@@ -180,17 +172,17 @@ class SupplierExaminationService
}
$examineData = collect($examineData);
foreach ($examineData->chunk(50) as $chunk) {
SupplierExaminationModel::insert($chunk->toArray());
}
return true;
});
return true;
} catch (\Exception $exception) {
return $exception->getMessage();
} finally {
unlink($filePath);
}
}
private function checkImportCsvHeader($header)
......
No preview for this file type
......@@ -207,7 +207,7 @@
elem: '#import_supplier_examination', //绑定元素
url: '/api/supplier_examination/ImportSupplierExamination', //上传接口
auto: true,
exts: 'csv',
exts: 'xlsx',
before: function (obj) { //obj参数包含的信息,跟 choose回调完全一致,可参见上文。
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