|
@@ -14,6 +14,7 @@ const multiparty = require("multiparty");
|
|
const fs = require("fs");
|
|
const fs = require("fs");
|
|
// excel解析
|
|
// excel解析
|
|
const excel = require("node-xlsx");
|
|
const excel = require("node-xlsx");
|
|
|
|
+const rationItem = require("../models/ration_item");
|
|
|
|
|
|
class RationRepositoryController extends baseController {
|
|
class RationRepositoryController extends baseController {
|
|
async getCompilationList(req, res) {
|
|
async getCompilationList(req, res) {
|
|
@@ -142,6 +143,10 @@ class RationRepositoryController extends baseController {
|
|
* @return {void}
|
|
* @return {void}
|
|
*/
|
|
*/
|
|
async uploadSourceData(request, response) {
|
|
async uploadSourceData(request, response) {
|
|
|
|
+ let responseData = {
|
|
|
|
+ err: 0,
|
|
|
|
+ msg: ''
|
|
|
|
+ };
|
|
const allowHeader = ['application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];
|
|
const allowHeader = ['application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];
|
|
try {
|
|
try {
|
|
const uploadOption = {
|
|
const uploadOption = {
|
|
@@ -149,7 +154,12 @@ class RationRepositoryController extends baseController {
|
|
};
|
|
};
|
|
const form = new multiparty.Form(uploadOption);
|
|
const form = new multiparty.Form(uploadOption);
|
|
form.parse(request, function(err, fields, files) {
|
|
form.parse(request, function(err, fields, files) {
|
|
- const file = files.source_file !== undefined ? files.source_file[0] : null;
|
|
|
|
|
|
+ const rationRepId = fields.rationRepId !== undefined && fields.rationRepId.length > 0 ?
|
|
|
|
+ fields.rationRepId[0] : 0;
|
|
|
|
+ if (rationRepId <= 0) {
|
|
|
|
+ throw '参数错误';
|
|
|
|
+ }
|
|
|
|
+ const file = files.file !== undefined ? files.file[0] : null;
|
|
if (err || file === null) {
|
|
if (err || file === null) {
|
|
throw '上传失败';
|
|
throw '上传失败';
|
|
}
|
|
}
|
|
@@ -161,11 +171,18 @@ class RationRepositoryController extends baseController {
|
|
const uploadFullName = uploadOption.uploadDir + '/' + file.originalFilename;
|
|
const uploadFullName = uploadOption.uploadDir + '/' + file.originalFilename;
|
|
fs.renameSync(file.path, uploadFullName);
|
|
fs.renameSync(file.path, uploadFullName);
|
|
|
|
|
|
- const test = excel.parse(uploadFullName);
|
|
|
|
- console.log(test[0].data);
|
|
|
|
|
|
+ const sheet = excel.parse(uploadFullName);
|
|
|
|
+ if (sheet[0] === undefined || sheet[0].data === undefined) {
|
|
|
|
+ throw 'excel没有对应数据';
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ rationItem.batchAddFromExcel(rationRepId, sheet[0].data);
|
|
|
|
+ response.json(responseData);
|
|
});
|
|
});
|
|
} catch (error) {
|
|
} catch (error) {
|
|
- console.log(error);
|
|
|
|
|
|
+ responseData.err = 1;
|
|
|
|
+ responseData.msg = error;
|
|
|
|
+ response.json(responseData);
|
|
}
|
|
}
|
|
|
|
|
|
return;
|
|
return;
|