refactor: ♻️ 导入增加错误提示信息
导入增加错误提示信息
This commit is contained in:
@@ -126,7 +126,7 @@ const UserAPI = {
|
|||||||
import(deptId: number, file: File) {
|
import(deptId: number, file: File) {
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append("file", file);
|
formData.append("file", file);
|
||||||
return request({
|
return request<any, ExcelResult>({
|
||||||
url: `${USER_BASE_URL}/import`,
|
url: `${USER_BASE_URL}/import`,
|
||||||
method: "post",
|
method: "post",
|
||||||
params: { deptId: deptId },
|
params: { deptId: deptId },
|
||||||
|
|||||||
13
src/types/global.d.ts
vendored
13
src/types/global.d.ts
vendored
@@ -89,5 +89,18 @@ declare global {
|
|||||||
/** 子列表 */
|
/** 子列表 */
|
||||||
children?: OptionType[];
|
children?: OptionType[];
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* 导入结果
|
||||||
|
*/
|
||||||
|
interface ExcelResult {
|
||||||
|
/** 状态码 */
|
||||||
|
code: string;
|
||||||
|
/** 无效数据条数 */
|
||||||
|
invalidCount: number;
|
||||||
|
/** 有效数据条数 */
|
||||||
|
validCount: number;
|
||||||
|
/** 错误信息 */
|
||||||
|
messageList: Array<string>;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
export {};
|
export {};
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
<template>
|
<template>
|
||||||
|
<div>
|
||||||
<el-dialog
|
<el-dialog
|
||||||
v-model="visible"
|
v-model="visible"
|
||||||
:align-center="true"
|
:align-center="true"
|
||||||
@@ -49,6 +50,9 @@
|
|||||||
</el-scrollbar>
|
</el-scrollbar>
|
||||||
<template #footer>
|
<template #footer>
|
||||||
<div style="padding-right: var(--el-dialog-padding-primary)">
|
<div style="padding-right: var(--el-dialog-padding-primary)">
|
||||||
|
<el-button v-if="resultData.length > 0" type="primary" @click="handleShowResult">
|
||||||
|
错误信息
|
||||||
|
</el-button>
|
||||||
<el-button
|
<el-button
|
||||||
type="primary"
|
type="primary"
|
||||||
:disabled="importFormData.files.length === 0"
|
:disabled="importFormData.files.length === 0"
|
||||||
@@ -60,11 +64,34 @@
|
|||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
</el-dialog>
|
</el-dialog>
|
||||||
|
|
||||||
|
<el-dialog v-model="resultVisible" title="导入结果" width="600px">
|
||||||
|
<el-alert
|
||||||
|
:title="`导入结果:${invalidCount}条无效数据,${validCount}条有效数据`"
|
||||||
|
type="warning"
|
||||||
|
:closable="false"
|
||||||
|
/>
|
||||||
|
<el-table :data="resultData" style="width: 100%; max-height: 400px">
|
||||||
|
<el-table-column prop="index" align="center" width="100" type="index" label="序号" />
|
||||||
|
<el-table-column prop="message" label="错误信息" width="400">
|
||||||
|
<template #default="scope">
|
||||||
|
{{ scope.row }}
|
||||||
|
</template>
|
||||||
|
</el-table-column>
|
||||||
|
</el-table>
|
||||||
|
<template #footer>
|
||||||
|
<div class="dialog-footer">
|
||||||
|
<el-button @click="handleCloseResult">关闭</el-button>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
</el-dialog>
|
||||||
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { ElMessage, type UploadUserFile } from "element-plus";
|
import { ElMessage, type UploadUserFile } from "element-plus";
|
||||||
import UserAPI from "@/api/system/user";
|
import UserAPI from "@/api/system/user";
|
||||||
|
import { ResultEnum } from "@/enums/ResultEnum";
|
||||||
|
|
||||||
const emit = defineEmits(["import-success"]);
|
const emit = defineEmits(["import-success"]);
|
||||||
const visible = defineModel("modelValue", {
|
const visible = defineModel("modelValue", {
|
||||||
@@ -73,6 +100,11 @@ const visible = defineModel("modelValue", {
|
|||||||
default: false,
|
default: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const resultVisible = ref(false);
|
||||||
|
const resultData = ref<string[]>([]);
|
||||||
|
const invalidCount = ref(0);
|
||||||
|
const validCount = ref(0);
|
||||||
|
|
||||||
const importFormRef = ref(null);
|
const importFormRef = ref(null);
|
||||||
const uploadRef = ref(null);
|
const uploadRef = ref(null);
|
||||||
|
|
||||||
@@ -82,6 +114,15 @@ const importFormData = reactive<{
|
|||||||
files: [],
|
files: [],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
watch(visible, (newValue) => {
|
||||||
|
if (newValue) {
|
||||||
|
resultData.value = [];
|
||||||
|
resultVisible.value = false;
|
||||||
|
invalidCount.value = 0;
|
||||||
|
validCount.value = 0;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const importFormRules = {
|
const importFormRules = {
|
||||||
files: [{ required: true, message: "文件不能为空", trigger: "blur" }],
|
files: [{ required: true, message: "文件不能为空", trigger: "blur" }],
|
||||||
};
|
};
|
||||||
@@ -122,15 +163,33 @@ const handleUpload = async () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await UserAPI.import(1, importFormData.files[0].raw as File);
|
const result = await UserAPI.import(1, importFormData.files[0].raw as File);
|
||||||
ElMessage.success("上传成功");
|
if (result.code === ResultEnum.SUCCESS && result.invalidCount === 0) {
|
||||||
|
ElMessage.success("导入成功,导入数据:" + result.validCount + "条");
|
||||||
emit("import-success");
|
emit("import-success");
|
||||||
handleClose();
|
handleClose();
|
||||||
|
} else {
|
||||||
|
ElMessage.error("上传失败");
|
||||||
|
resultVisible.value = true;
|
||||||
|
resultData.value = result.messageList;
|
||||||
|
invalidCount.value = result.invalidCount;
|
||||||
|
validCount.value = result.validCount;
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
ElMessage.error("上传失败");
|
ElMessage.error("上传失败");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 显示错误信息
|
||||||
|
const handleShowResult = () => {
|
||||||
|
resultVisible.value = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 关闭错误信息弹窗
|
||||||
|
const handleCloseResult = () => {
|
||||||
|
resultVisible.value = false;
|
||||||
|
};
|
||||||
|
|
||||||
// 关闭弹窗
|
// 关闭弹窗
|
||||||
const handleClose = () => {
|
const handleClose = () => {
|
||||||
importFormData.files.length = 0;
|
importFormData.files.length = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user