refactor: 优化用户导入和字典数据路由跳转逻辑
移除用户导入接口的deptId参数 使用downloadFile工具函数简化模板下载代码 增加字典数据路由跳转的错误处理
This commit is contained in:
@@ -133,16 +133,14 @@ const UserAPI = {
|
||||
/**
|
||||
* 导入用户
|
||||
*
|
||||
* @param deptId 部门ID
|
||||
* @param file 导入文件
|
||||
*/
|
||||
import(deptId: string, file: File) {
|
||||
import(file: File) {
|
||||
const formData = new FormData();
|
||||
formData.append("file", file);
|
||||
return request<any, ExcelResult>({
|
||||
url: `${USER_BASE_URL}/import`,
|
||||
method: "post",
|
||||
params: { deptId },
|
||||
data: formData,
|
||||
headers: {
|
||||
"Content-Type": "multipart/form-data",
|
||||
|
||||
@@ -288,10 +288,20 @@ function handleDelete(id?: number) {
|
||||
|
||||
// 打开字典数据
|
||||
function handleOpenDictData(row: DictTypeItem) {
|
||||
router.push({
|
||||
name: "DictItem",
|
||||
query: { dictCode: row.dictCode, title: `【${row.name}】字典数据` },
|
||||
});
|
||||
try {
|
||||
const route = router.resolve({
|
||||
name: "DictItem",
|
||||
query: { dictCode: row.dictCode, title: `【${row.name}】字典数据` },
|
||||
});
|
||||
if (route.matched.length === 0) {
|
||||
ElMessage.error("路由未注册,请刷新页面后重试");
|
||||
return;
|
||||
}
|
||||
router.push(route);
|
||||
} catch (error) {
|
||||
console.error("路由跳转失败:", error);
|
||||
ElMessage.error("页面跳转失败,请刷新页面后重试");
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
|
||||
@@ -91,6 +91,7 @@
|
||||
import { ElMessage, type UploadUserFile } from "element-plus";
|
||||
import UserAPI from "@/api/system/user";
|
||||
import { ApiCodeEnum } from "@/enums/api";
|
||||
import { downloadFile } from "@/utils/download";
|
||||
|
||||
const emit = defineEmits(["import-success"]);
|
||||
const visible = defineModel("modelValue", {
|
||||
@@ -134,23 +135,7 @@ const handleFileExceed = () => {
|
||||
// 下载导入模板
|
||||
const handleDownloadTemplate = () => {
|
||||
UserAPI.downloadTemplate().then((response: any) => {
|
||||
const fileData = response.data;
|
||||
const fileName = decodeURI(response.headers["content-disposition"].split(";")[1].split("=")[1]);
|
||||
const fileType =
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8";
|
||||
|
||||
const blob = new Blob([fileData], { type: fileType });
|
||||
const downloadUrl = window.URL.createObjectURL(blob);
|
||||
|
||||
const downloadLink = document.createElement("a");
|
||||
downloadLink.href = downloadUrl;
|
||||
downloadLink.download = fileName;
|
||||
|
||||
document.body.appendChild(downloadLink);
|
||||
downloadLink.click();
|
||||
|
||||
document.body.removeChild(downloadLink);
|
||||
window.URL.revokeObjectURL(downloadUrl);
|
||||
downloadFile(response);
|
||||
});
|
||||
};
|
||||
|
||||
@@ -161,7 +146,7 @@ const handleUpload = async () => {
|
||||
return;
|
||||
}
|
||||
|
||||
const result = await UserAPI.import("1", importFormData.files[0].raw as File);
|
||||
const result = await UserAPI.import(importFormData.files[0].raw as File);
|
||||
if (result.code === ApiCodeEnum.SUCCESS && result.invalidCount === 0) {
|
||||
ElMessage.success("导入成功,导入数据:" + result.validCount + "条");
|
||||
emit("import-success");
|
||||
|
||||
Reference in New Issue
Block a user