修复筛选字典数据加载竞态

This commit is contained in:
Apcallover
2026-07-23 21:29:45 +08:00
parent 2c29c2a792
commit 4c8f79555d
3 changed files with 91 additions and 80 deletions

View File

@@ -11,6 +11,8 @@ import {
// 静态树 O(1) 超快查询!!!!!
let IndustryMap = null
// 复用同一批字典请求,避免 App 启动和页面组件同时初始化时产生竞态。
let dictDataPromise = null
// 构建索引
function buildIndex(tree) {
const map = new Map();
@@ -50,46 +52,54 @@ const useDictStore = defineStore("dict", () => {
return data
})
}
if (complete.value) return
if (dictLoading.value) return
dictLoading.value = true
try {
const [education, experience, area, scale, sex, affiliation, nature, noticeType] =
await Promise.all([
getDictSelectOption('education'),
getDictSelectOption('experience'),
getDictSelectOption('area', true),
getDictSelectOption('scale'),
getDictSelectOption('app_sex'),
getDictSelectOption('political_affiliation'),
getDictSelectOption('company_nature'),
getDictSelectOption('sys_notice_type'),
]);
if (complete.value) return Promise.resolve()
// App.vue 会在启动时加载字典,页面组件也可能同时加载。
// 不能在请求进行中直接 return否则调用方会把当时的空 state 复制下来。
if (dictDataPromise) return dictDataPromise
state.education = education;
state.experience = experience;
state.area = area;
state.scale = scale;
state.sex = sex;
state.affiliation = affiliation;
state.nature = nature
state.noticeType = noticeType
complete.value = true
getIndustryDict() // 获取行业
} catch (error) {
console.error('Error fetching dictionary data:', error);
// 确保即使出错也能返回空数组
state.education = [];
state.experience = [];
state.area = [];
state.scale = [];
state.sex = [];
state.affiliation = [];
state.nature = [];
state.noticeType = [];
} finally {
dictLoading.value = false
}
dictLoading.value = true
dictDataPromise = (async () => {
try {
const [education, experience, area, scale, sex, affiliation, nature, noticeType] =
await Promise.all([
getDictSelectOption('education'),
getDictSelectOption('experience'),
getDictSelectOption('area', true),
getDictSelectOption('scale'),
getDictSelectOption('app_sex'),
getDictSelectOption('political_affiliation'),
getDictSelectOption('company_nature'),
getDictSelectOption('sys_notice_type'),
]);
state.education = education;
state.experience = experience;
state.area = area;
state.scale = scale;
state.sex = sex;
state.affiliation = affiliation;
state.nature = nature
state.noticeType = noticeType
complete.value = true
getIndustryDict() // 获取行业
} catch (error) {
console.error('Error fetching dictionary data:', error);
// 确保即使出错也能返回空数组
state.education = [];
state.experience = [];
state.area = [];
state.scale = [];
state.sex = [];
state.affiliation = [];
state.nature = [];
state.noticeType = [];
} finally {
dictLoading.value = false
dictDataPromise = null
}
})()
return dictDataPromise
};
async function getIndustryDict() {