fix: 🐛 共享下拉数据,避免重复请求

This commit is contained in:
超凡
2025-04-24 00:18:42 +08:00
parent 2899e13ecd
commit 0ab21ab02c
11 changed files with 84 additions and 65 deletions

View File

@@ -0,0 +1,31 @@
/** 公共下载数据,减少重复请求次数 */
import DeptAPI from "@/api/system/dept.api";
import RoleAPI from "@/api/system/role.api";
interface OptionType {
label: string;
value: any;
[key: string]: any; // 允许其他属性
}
// 明确指定类型为 OptionType[]
export let deptArr = ref<OptionType[]>([]);
export let roleArr = ref<OptionType[]>([]);
export let stateArr = ref<OptionType[]>([
{ label: "启用", value: 1 },
{ label: "禁用", value: 0 },
]);
// 初始化逻辑,在 onMounted 钩子中调用
export const initOptions = async () => {
try {
// 使用Promise.all并行请求
const [dept, roles] = await Promise.all([DeptAPI.getOptions(), RoleAPI.getOptions()]);
// 获取部门选项并赋值
deptArr.value = dept;
// 获取角色选项并赋值
roleArr.value = roles;
} catch (error) {
console.error("初始化选项失败:", error);
}
};