Skip to content

Commit

Permalink
chore: 优化字典重复请求问题
Browse files Browse the repository at this point in the history
  • Loading branch information
han201379873 committed May 17, 2024
1 parent c369b88 commit 30222b0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
9 changes: 7 additions & 2 deletions src/components/GiCell/GiCellTag.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ const props = defineProps({
}
})
const dictItem = computed(() =>
props.dict.find((d) => d.value === String(props.value) || d.value === Number(props.value))
const dictItem = computed(() => {
try {
return props.dict.find((d) => d.value === String(props.value) || d.value === Number(props.value))
} catch (error) {
return []
}
}
)
</script>

Expand Down
22 changes: 17 additions & 5 deletions src/hooks/app/useDict.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,32 @@ import { ref, toRefs } from 'vue'
import { listCommonDict } from '@/apis'
import { useDictStore } from '@/stores'

const dictStore = useDictStore()
const tmpCodeZone: string[] = []
export function useDict(...codes: Array<string>) {
const res = ref<any>({})
return (() => {
const dictStore = useDictStore()
codes.forEach((code) => {
res.value[code] = []
const dict = dictStore.getDict(code)
if (dict) {
res.value[code] = dict
} else {
listCommonDict(code).then((resp) => {
res.value[code] = resp.data
dictStore.setDict(code, res.value[code])
})
if (!tmpCodeZone.includes(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)
Expand Down

0 comments on commit 30222b0

Please sign in to comment.