chore: 优化字典重复请求问题

This commit is contained in:
秋帆
2024-05-16 20:15:42 +08:00
parent 0a6cd6ef98
commit 6705027273

View File

@@ -2,6 +2,7 @@ import { ref, toRefs } from 'vue'
import { listCommonDict } from '@/apis' import { listCommonDict } from '@/apis'
import { useDictStore } from '@/stores' import { useDictStore } from '@/stores'
const tmpCodeZone: string[] = []
export function useDict(...codes: Array<string>) { export function useDict(...codes: Array<string>) {
const res = ref<any>({}) const res = ref<any>({})
return (() => { return (() => {
@@ -12,10 +13,19 @@ export function useDict(...codes: Array<string>) {
if (dict) { if (dict) {
res.value[code] = dict res.value[code] = dict
} else { } else {
listCommonDict(code).then((resp) => { if (!tmpCodeZone.includes(code)) {
res.value[code] = resp.data // 防止重复请求
dictStore.setDict(code, res.value[code]) tmpCodeZone.push(code)
}) listCommonDict(code)
.then((resp) => {
res.value[code] = resp.data
dictStore.setDict(code, res.value[code])
tmpCodeZone.splice(tmpCodeZone.indexOf(code), 1)
})
.catch(() => {
tmpCodeZone.splice(tmpCodeZone.indexOf(code), 1)
})
}
} }
}) })
return toRefs(res.value) return toRefs(res.value)