mirror of
https://github.com/continew-org/continew-admin-ui.git
synced 2025-09-10 20:57:10 +08:00
chore: 优化字典重复请求问题
This commit is contained in:
@@ -25,8 +25,13 @@ const props = defineProps({
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const dictItem = computed(() =>
|
const dictItem = computed(() => {
|
||||||
props.dict.find((d) => d.value === String(props.value) || d.value === Number(props.value))
|
try {
|
||||||
|
return props.dict.find((d) => d.value === String(props.value) || d.value === Number(props.value))
|
||||||
|
} catch (error) {
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
}
|
||||||
)
|
)
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
@@ -2,20 +2,32 @@ import { ref, toRefs } from 'vue'
|
|||||||
import { listCommonDict } from '@/apis'
|
import { listCommonDict } from '@/apis'
|
||||||
import { useDictStore } from '@/stores'
|
import { useDictStore } from '@/stores'
|
||||||
|
|
||||||
|
const dictStore = useDictStore()
|
||||||
|
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 (() => {
|
||||||
const dictStore = useDictStore()
|
|
||||||
codes.forEach((code) => {
|
codes.forEach((code) => {
|
||||||
res.value[code] = []
|
res.value[code] = []
|
||||||
const dict = dictStore.getDict(code)
|
const dict = dictStore.getDict(code)
|
||||||
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)
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
res.value[code] = computed(() => {
|
||||||
|
return dictStore.getDict(code)
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return toRefs(res.value)
|
return toRefs(res.value)
|
||||||
|
Reference in New Issue
Block a user