更换导入接口

This commit is contained in:
庞东林
2021-12-14 21:31:43 +08:00
parent 5a30d9ca4c
commit f9b9123164
2 changed files with 270 additions and 225 deletions

View File

@@ -0,0 +1,24 @@
import axios from 'axios';
const baseUri = '/api/lakeLibrary/lakeLibraryImport'
export const cityOther = `${baseUri}/cityOther`
export const appointCity = `${baseUri}/appointCity`
export const special = `${baseUri}/special`
// 城市内湖-其他内湖:/api/lakeLibrary/lakeLibraryImport/
export const importCityOther = function (data) {
return axios.post(cityOther,data)
}
// 城市内湖-五象湖:/api/lakeLibrary/lakeLibraryImport/appointCity
export const importCppointCity = function (data) {
console.log('data',data)
return axios.post(appointCity,data)
}
// 专项湖库:/api/lakeLibrary/lakeLibraryImport/special
export const importSpecial = function (data) {
return axios.post(special,data)
}

View File

@@ -61,13 +61,14 @@
</template> </template>
<script> <script>
import XLSX from "xlsx"; // import XLSX from "xlsx";
import utils from "./utils"; // import utils from "./utils";
import {Modal} from "ant-design-vue"; // import {Modal} from "ant-design-vue";
import WaterBill from "./water-bill.vue"; import WaterBill from "./water-bill.vue";
import SpecialBill from "./special-bill.vue"; import SpecialBill from "./special-bill.vue";
import {saveLakeBill} from "@/api/ecology/lake"; // import {saveLakeBill} from "@/api/ecology/lake";
import {saveLakeBill as saveSpecialLake} from "@/api/ecology/special-lake"; // import {saveLakeBill as saveSpecialLake} from "@/api/ecology/special-lake";
import {importCityOther,importCppointCity,importSpecial} from "@/api/ecology/water/lake/import";
export default { export default {
name: "LakeCollectIndex", name: "LakeCollectIndex",
@@ -87,255 +88,275 @@ export default {
/* 导入本地excel文件 */ /* 导入本地excel文件 */
importFileInnerCity(file) { importFileInnerCity(file) {
const hide = this.$message.loading("导入中..", 0); const hide = this.$message.loading("导入中..", 0);
const formData = new FormData();
let reader = new FileReader(); formData.append('file', file);
reader.onload = (e) => { importCityOther(formData).then(res=>{
try { console.log(res)
let data = new Uint8Array(e.target.result);
let workbook = XLSX.read(data, {
type: "array",
});
//0.昼间数据 1.夜间数据 2.其他信息
let sheetNames = workbook.SheetNames;
// 解析成二维数组
let aoa = XLSX.utils.sheet_to_json(workbook.Sheets[sheetNames[0]], {
header: 1,
});
const reportDate = aoa[2][0].replace(/[^\d]+/g, "-");
const lakeList = aoa.filter(
(item) => {
return item.length >= 24 && item[0] && !item[0].includes("点位名称");
}
);
// 解析成对象数组
const billName = file.name;
const billData = utils.toInnerCityLakeObjData(lakeList, reportDate);
if (!billData || billData.length == 0) {
hide() hide()
Modal.error({
title: "导入失败",
content: "找不到数据",
});
return;
}
const tasks = [];
if (billData.length > 0) {
tasks.push(
saveLakeBill({
reportTime: new Date(reportDate).getTime(),
billName: billName,
regionLevel: "市级",
lakeLibraryInnerCityList: billData,
}) })
);
} // let reader = new FileReader();
// reader.onload = (e) => {
// try {
// let data = new Uint8Array(e.target.result);
// let workbook = XLSX.read(data, {
// type: "array",
// });
// //0.昼间数据 1.夜间数据 2.其他信息
// let sheetNames = workbook.SheetNames;
// // 解析成二维数组
// let aoa = XLSX.utils.sheet_to_json(workbook.Sheets[sheetNames[0]], {
// header: 1,
// });
// const reportDate = aoa[2][0].replace(/[^\d]+/g, "-");
// const lakeList = aoa.filter(
// (item) => {
// return item.length >= 24 && item[0] && !item[0].includes("点位名称");
// }
// );
// 上传到服务器 // // 解析成对象数组
// const billName = file.name;
// const billData = utils.toInnerCityLakeObjData(lakeList, reportDate);
// if (!billData || billData.length == 0) {
// hide()
// Modal.error({
// title: "导入失败",
// content: "找不到数据",
// });
// return;
// }
// const tasks = [];
// if (billData.length > 0) {
// tasks.push(
// saveLakeBill({
// reportTime: new Date(reportDate).getTime(),
// billName: billName,
// regionLevel: "市级",
// lakeLibraryInnerCityList: billData,
// })
// );
// }
Promise.all(tasks)
.then((res) => {
if (res[0].data.code == 0) {
Modal.success({
title: "导入成功",
content: `成功导入${billData.length}条数据`,
});
this.$refs.water && this.$refs.water.reload();
} else {
Modal.error({
title: "导入失败",
content: "数据上传出错",
});
}
}) // // 上传到服务器
.catch(() => {
Modal.error({ // Promise.all(tasks)
title: "导入失败", // .then((res) => {
content: "数据上传出错", // if (res[0].data.code == 0) {
}); // Modal.success({
}) // title: "导入成功",
.finally(() => { // content: `成功导入${billData.length}条数据`,
hide(); // });
}); // this.$refs.water && this.$refs.water.reload();
} catch (error) { // } else {
hide(); // Modal.error({
Modal.error({ // title: "导入失败",
title: "导入失败", // content: "数据上传出错",
content: error.message, // });
}); // }
}
// console.log(billData); // })
}; // .catch(() => {
reader.readAsArrayBuffer(file); // Modal.error({
// title: "导入失败",
// content: "数据上传出错",
// });
// })
// .finally(() => {
// hide();
// });
// } catch (error) {
// hide();
// Modal.error({
// title: "导入失败",
// content: error.message,
// });
// }
// // console.log(billData);
// };
// reader.readAsArrayBuffer(file);
return false; return false;
}, },
importFileWuxiang(file) { importFileWuxiang(file) {
const hide = this.$message.loading("导入中..", 0); const hide = this.$message.loading("导入中..", 0);
let reader = new FileReader(); const formData = new FormData();
reader.onload = (e) => { formData.append('file', file);
try { importCppointCity(formData).then(res=>{
let data = new Uint8Array(e.target.result); console.log(res)
let workbook = XLSX.read(data, {
type: "array",
});
let sheetNames = workbook.SheetNames;
// 解析成二维数组
let aoa = XLSX.utils.sheet_to_json(workbook.Sheets[sheetNames[0]], {
header: 1,
});
const lakeList = aoa.filter(
(item) => {
return item.length >= 24 && item[0] && !item[0].includes("点位名称");
}
);
// 解析成对象数组
const billName = file.name;
const billData = utils.toWuxiangLakeObjData(lakeList);
if (!billData || billData.length == 0) {
hide() hide()
Modal.error({
title: "导入失败",
content: "找不到数据",
});
return;
}
const tasks = [];
if (billData.length > 0) {
tasks.push(
saveLakeBill({
billName: billName,
lakeLibraryInnerCityList: billData,
}) })
);
}
// 上传到服务器 // let reader = new FileReader();
// reader.onload = (e) => {
// try {
// let data = new Uint8Array(e.target.result);
// let workbook = XLSX.read(data, {
// type: "array",
// });
// let sheetNames = workbook.SheetNames;
// // 解析成二维数组
// let aoa = XLSX.utils.sheet_to_json(workbook.Sheets[sheetNames[0]], {
// header: 1,
// });
// const lakeList = aoa.filter(
// (item) => {
// return item.length >= 24 && item[0] && !item[0].includes("点位名称");
// }
// );
// // 解析成对象数组
// const billName = file.name;
// const billData = utils.toWuxiangLakeObjData(lakeList);
// if (!billData || billData.length == 0) {
// hide()
// Modal.error({
// title: "导入失败",
// content: "找不到数据",
// });
// return;
// }
// const tasks = [];
// if (billData.length > 0) {
// tasks.push(
// saveLakeBill({
// billName: billName,
// lakeLibraryInnerCityList: billData,
// })
// );
// }
Promise.all(tasks)
.then((res) => {
if (res[0].data.code == 0) {
Modal.success({
title: "导入成功",
content: `成功导入${billData.length}条数据`,
});
this.$refs.water && this.$refs.water.reload();
} else {
Modal.error({
title: "导入失败",
content: "数据上传出错",
});
}
}) // // 上传到服务器
.catch(() => {
Modal.error({ // Promise.all(tasks)
title: "导入失败", // .then((res) => {
content: "数据上传出错", // if (res[0].data.code == 0) {
}); // Modal.success({
}) // title: "导入成功",
.finally(() => { // content: `成功导入${billData.length}条数据`,
hide(); // });
}); // this.$refs.water && this.$refs.water.reload();
} catch (error) { // } else {
hide(); // Modal.error({
Modal.error({ // title: "导入失败",
title: "导入失败", // content: "数据上传出错",
content: error.message, // });
}); // }
}
// console.log(billData); // })
}; // .catch(() => {
reader.readAsArrayBuffer(file); // Modal.error({
// title: "导入失败",
// content: "数据上传出错",
// });
// })
// .finally(() => {
// hide();
// });
// } catch (error) {
// hide();
// Modal.error({
// title: "导入失败",
// content: error.message,
// });
// }
// // console.log(billData);
// };
// reader.readAsArrayBuffer(file);
return false; return false;
}, },
importFileSpecial(file) { importFileSpecial(file) {
const hide = this.$message.loading("导入中..", 0); const hide = this.$message.loading("导入中..", 0);
const formData = new FormData();
let reader = new FileReader(); formData.append('file', file);
reader.onload = (e) => { importSpecial(formData).then(res=>{
try { console.log(res)
let data = new Uint8Array(e.target.result);
let workbook = XLSX.read(data, {
type: "array",
});
let sheetNames = workbook.SheetNames;
// 解析成二维数组
let aoa = XLSX.utils.sheet_to_json(workbook.Sheets[sheetNames[0]], {
header: 1,
});
const lakeList = aoa.filter(
(item) => {
return item.length >= 24 && item[1] && !item[1].includes("测站名称");
}
);
// 解析成对象数组
const billName = file.name;
const billData = utils.toSpecialLakeData(lakeList);
if (!billData || billData.length == 0) {
hide() hide()
Modal.error({
title: "导入失败",
content: "找不到数据",
});
return;
}
const tasks = [];
if (billData.length > 0) {
tasks.push(
saveSpecialLake({
billName: billName,
lakeLibrarySpecialList: billData,
}) })
);
} // let reader = new FileReader();
// reader.onload = (e) => {
// try {
// let data = new Uint8Array(e.target.result);
// let workbook = XLSX.read(data, {
// type: "array",
// });
// let sheetNames = workbook.SheetNames;
// // 解析成二维数组
// let aoa = XLSX.utils.sheet_to_json(workbook.Sheets[sheetNames[0]], {
// header: 1,
// });
// const lakeList = aoa.filter(
// (item) => {
// return item.length >= 24 && item[1] && !item[1].includes("测站名称");
// }
// );
// // 解析成对象数组
// const billName = file.name;
// const billData = utils.toSpecialLakeData(lakeList);
// if (!billData || billData.length == 0) {
// hide()
// Modal.error({
// title: "导入失败",
// content: "找不到数据",
// });
// return;
// }
// const tasks = [];
// if (billData.length > 0) {
// tasks.push(
// saveSpecialLake({
// billName: billName,
// lakeLibrarySpecialList: billData,
// })
// );
// }
// 上传到服务器 // // 上传到服务器
Promise.all(tasks) // Promise.all(tasks)
.then((res) => { // .then((res) => {
if (res[0].data.code == 0) { // if (res[0].data.code == 0) {
Modal.success({ // Modal.success({
title: "导入成功", // title: "导入成功",
content: `成功导入${billData.length}条数据`, // content: `成功导入${billData.length}条数据`,
}); // });
this.$refs.special && this.$refs.special.reload(); // this.$refs.special && this.$refs.special.reload();
} else { // } else {
Modal.error({ // Modal.error({
title: "导入失败", // title: "导入失败",
content: "数据上传出错", // content: "数据上传出错",
}); // });
} // }
}) // })
.catch(() => { // .catch(() => {
Modal.error({ // Modal.error({
title: "导入失败", // title: "导入失败",
content: "数据上传出错", // content: "数据上传出错",
}); // });
}) // })
.finally(() => { // .finally(() => {
hide(); // hide();
}); // });
} catch (error) { // } catch (error) {
hide(); // hide();
Modal.error({ // Modal.error({
title: "导入失败", // title: "导入失败",
content: error.message, // content: error.message,
}); // });
} // }
// console.log(billData); // // console.log(billData);
}; // };
reader.readAsArrayBuffer(file); // reader.readAsArrayBuffer(file);
return false; return false;
}, },