|
@@ -4,15 +4,19 @@ defineOptions({
|
|
|
});
|
|
|
import { ref } from "vue";
|
|
|
import * as XLSX from "xlsx";
|
|
|
-import { postImportQuotaTemplate } from "@/api/indexDefine";
|
|
|
-import { GetdownloadDataRosterTemplateApi } from "@/api/download";
|
|
|
+import {
|
|
|
+ GetdownloadDataRosterTemplateApi,
|
|
|
+ postUpdateDeptApi
|
|
|
+} from "@/api/download";
|
|
|
import { ElMessage } from "element-plus";
|
|
|
-
|
|
|
+import { useRouter } from "vue-router";
|
|
|
+const $router = useRouter();
|
|
|
const emit = defineEmits(["handleImport"]);
|
|
|
const uploadShow = ref(true);
|
|
|
const fileDocument = ref<File | null>(null); // 存储文件
|
|
|
|
|
|
// 上传文件函数
|
|
|
+// 上传文件函数
|
|
|
const uploadFile = async () => {
|
|
|
if (!fileDocument.value) {
|
|
|
ElMessage.error("请先上传文件");
|
|
@@ -21,16 +25,31 @@ const uploadFile = async () => {
|
|
|
|
|
|
try {
|
|
|
const formData = new FormData();
|
|
|
- formData.append("file", fileDocument.value); // 将文件添加到 FormData 中
|
|
|
+ formData.append("file", fileDocument.value); // 将文件添加到表单数据
|
|
|
+
|
|
|
+ // 调用 API 上传文件
|
|
|
+ const { code, msg } = await postUpdateDeptApi(formData);
|
|
|
|
|
|
- const { code } = await postImportQuotaTemplate(formData); // 发送 FormData
|
|
|
if (code === 200) {
|
|
|
- ElMessage.success("上传成功");
|
|
|
- emit("handleImport");
|
|
|
+ // 根据后端返回的 code 判断是否成功
|
|
|
+ ElMessage.success("文件上传并导入成功");
|
|
|
+ // 重置上传状态
|
|
|
+ // uploadShow.value = true;
|
|
|
+ $router.push("/IndexDefine/children/define");
|
|
|
+ tableHeaders.value = []; // 清空表头
|
|
|
+ tableData.value = [
|
|
|
+ { name: "" },
|
|
|
+ { defin: "" },
|
|
|
+ { caliber: "" },
|
|
|
+ { type: "" },
|
|
|
+ { from: "" }
|
|
|
+ ]; // 清空表格数据
|
|
|
+ } else {
|
|
|
+ ElMessage.error(msg);
|
|
|
}
|
|
|
} catch (error) {
|
|
|
- console.error("上传文件时出错:", error);
|
|
|
- ElMessage.error("上传文件时出错,请检查文件格式");
|
|
|
+ ElMessage.error("上传失败,请重试");
|
|
|
+ console.error(error);
|
|
|
}
|
|
|
};
|
|
|
|
|
@@ -51,29 +70,34 @@ const backDefine = () => {
|
|
|
const handleUploadChange = (file: File) => {
|
|
|
fileDocument.value = file; // 保存文件
|
|
|
const reader = new FileReader();
|
|
|
+ let fileType = file.name.slice(-4);
|
|
|
+ console.log("文件", file.name.slice(-4));
|
|
|
+ if (fileType == "xlsx") {
|
|
|
+ reader.onload = e => {
|
|
|
+ const data = new Uint8Array(e.target?.result as ArrayBuffer);
|
|
|
+ const workbook = XLSX.read(data, { type: "array" }); // 读取 Excel 文件
|
|
|
+ const firstSheetName = workbook.SheetNames[0]; // 获取第一个工作表名
|
|
|
+ const worksheet = workbook.Sheets[firstSheetName]; // 获取第一个工作表
|
|
|
+ const jsonData = XLSX.utils.sheet_to_json(worksheet, { header: 1 }); // 将工作表转为 JSON
|
|
|
|
|
|
- reader.onload = e => {
|
|
|
- const data = new Uint8Array(e.target?.result as ArrayBuffer);
|
|
|
- const workbook = XLSX.read(data, { type: "array" }); // 读取 Excel 文件
|
|
|
- const firstSheetName = workbook.SheetNames[0]; // 获取第一个工作表名
|
|
|
- const worksheet = workbook.Sheets[firstSheetName]; // 获取第一个工作表
|
|
|
- const jsonData = XLSX.utils.sheet_to_json(worksheet, { header: 1 }); // 将工作表转为 JSON
|
|
|
-
|
|
|
- if (jsonData.length > 0) {
|
|
|
- tableHeaders.value = jsonData[0]; // 表头
|
|
|
- tableData.value = jsonData.slice(1).map(row => {
|
|
|
- const rowData = {};
|
|
|
- tableHeaders.value.forEach((header, index) => {
|
|
|
- rowData[header] = row[index] !== undefined ? row[index] : null; // 填充缺失值
|
|
|
+ if (jsonData.length > 0) {
|
|
|
+ tableHeaders.value = jsonData[0]; // 表头
|
|
|
+ tableData.value = jsonData.slice(1).map(row => {
|
|
|
+ const rowData = {};
|
|
|
+ tableHeaders.value.forEach((header, index) => {
|
|
|
+ rowData[header] = row[index] !== undefined ? row[index] : null; // 填充缺失值
|
|
|
+ });
|
|
|
+ return rowData;
|
|
|
});
|
|
|
- return rowData;
|
|
|
- });
|
|
|
- ElMessage.success("文件上传成功");
|
|
|
- uploadShow.value = false; // 隐藏上传区域,显示数据表
|
|
|
- } else {
|
|
|
- ElMessage.error("文件为空或格式不正确");
|
|
|
- }
|
|
|
- };
|
|
|
+ ElMessage.success("文件上传成功");
|
|
|
+ uploadShow.value = false; // 隐藏上传区域,显示数据表
|
|
|
+ } else {
|
|
|
+ ElMessage.error("文件为空或格式不正确");
|
|
|
+ }
|
|
|
+ };
|
|
|
+ } else {
|
|
|
+ ElMessage.error("请上传xlsx文件");
|
|
|
+ }
|
|
|
|
|
|
reader.readAsArrayBuffer(file); // 读取文件为 ArrayBuffer
|
|
|
};
|