feat(EasyComponent): 优化选项管理器支持深度查找和缓存清理
- 引入 deepFindObjArr 工具函数用于深度查找对象数组 - 改进 Promise 处理逻辑,失败时设置为空数组而非删除缓存 - 新增 asyncGetOptionsItemByBaseCode 方法支持深度选项查找 - 重构 asyncGetOptionsLabelByBaseCode 依赖新的查找方法 - 添加 removeOptionsCache 方法用于清理选项缓存 fix(request): 调整响应拦截器条件判断逻辑 - 修改错误处理条件,当 useResponseData 配置为 true 时也跳过拦截处理
This commit is contained in:
parent
50d8d1e992
commit
883fbdc4c9
|
|
@ -1,3 +1,4 @@
|
|||
import { deepFindObjArr } from './object-helper'
|
||||
import { ComponentConfig } from './globalConfig'
|
||||
import { createPathObject } from './utils'
|
||||
|
||||
|
|
@ -14,16 +15,18 @@ export function getOptionsByBaseCode(baseCode) {
|
|||
return OptionsMap[baseCode]
|
||||
}
|
||||
if (OptionsMap[baseCode] instanceof Promise) {
|
||||
OptionsMap[baseCode].then((data) => {
|
||||
if (Array.isArray(data)) {
|
||||
OptionsMap[baseCode] = data
|
||||
} else {
|
||||
throw new Error('BaseOptionsLoader必须返回Array或Promise<Array>')
|
||||
}
|
||||
}).catch((e) => {
|
||||
console.error(e)
|
||||
delete OptionsMap[baseCode]
|
||||
})
|
||||
OptionsMap[baseCode]
|
||||
.then((data) => {
|
||||
if (Array.isArray(data)) {
|
||||
OptionsMap[baseCode] = data
|
||||
} else {
|
||||
throw new Error('BaseOptionsLoader必须返回Array或Promise<Array>')
|
||||
}
|
||||
})
|
||||
.catch((e) => {
|
||||
console.error(e)
|
||||
OptionsMap[baseCode] = []
|
||||
})
|
||||
return []
|
||||
}
|
||||
throw new Error('BaseOptionsLoader必须返回Array或Promise<Array>')
|
||||
|
|
@ -39,10 +42,26 @@ export async function asyncGetOptionsByBaseCode(baseCode) {
|
|||
return OptionsMap[baseCode]
|
||||
}
|
||||
|
||||
export async function asyncGetOptionsLabelByBaseCode(baseCode, value) {
|
||||
export async function asyncGetOptionsItemByBaseCode(baseCode, value, deepOption) {
|
||||
return asyncGetOptionsByBaseCode(baseCode).then((arr) => {
|
||||
return (arr.find((item) => {
|
||||
return item.value === value
|
||||
}) || {}).label
|
||||
if (deepOption) {
|
||||
return deepFindObjArr(arr, (item) => {
|
||||
return item.value === value
|
||||
}, typeof deepOption === 'object' ? deepOption : undefined)
|
||||
} else {
|
||||
return (
|
||||
arr.find((item) => {
|
||||
return item.value === value
|
||||
}) || {}
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export async function asyncGetOptionsLabelByBaseCode(baseCode, value, deepOption) {
|
||||
return (await asyncGetOptionsItemByBaseCode(baseCode, value, deepOption))?.label
|
||||
}
|
||||
|
||||
export function removeOptionsCache(baseCode) {
|
||||
delete OptionsMap[baseCode]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -78,7 +78,7 @@ service.interceptors.response.use(
|
|||
location.reload()
|
||||
})
|
||||
} else {
|
||||
if (error.config.throwError) { // 不被拦截处理
|
||||
if (error.config.throwError || error.config.useResponseData) { // 不被拦截处理
|
||||
throw error.response
|
||||
} else {
|
||||
Message({
|
||||
|
|
|
|||
Loading…
Reference in New Issue