From 883fbdc4c9146501f7e661bfac4baa8adc3a7bc1 Mon Sep 17 00:00:00 2001 From: chenxf Date: Tue, 6 Jan 2026 14:17:55 +0800 Subject: [PATCH] =?UTF-8?q?feat(EasyComponent):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E9=80=89=E9=A1=B9=E7=AE=A1=E7=90=86=E5=99=A8=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E6=B7=B1=E5=BA=A6=E6=9F=A5=E6=89=BE=E5=92=8C=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E6=B8=85=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 引入 deepFindObjArr 工具函数用于深度查找对象数组 - 改进 Promise 处理逻辑,失败时设置为空数组而非删除缓存 - 新增 asyncGetOptionsItemByBaseCode 方法支持深度选项查找 - 重构 asyncGetOptionsLabelByBaseCode 依赖新的查找方法 - 添加 removeOptionsCache 方法用于清理选项缓存 fix(request): 调整响应拦截器条件判断逻辑 - 修改错误处理条件,当 useResponseData 配置为 true 时也跳过拦截处理 --- src/plugins/EasyComponent/optionsManager.js | 47 +++++++++++++++------ src/utils/request.js | 2 +- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/plugins/EasyComponent/optionsManager.js b/src/plugins/EasyComponent/optionsManager.js index a0df017..fe650ff 100644 --- a/src/plugins/EasyComponent/optionsManager.js +++ b/src/plugins/EasyComponent/optionsManager.js @@ -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') - } - }).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') + } + }) + .catch((e) => { + console.error(e) + OptionsMap[baseCode] = [] + }) return [] } throw new Error('BaseOptionsLoader必须返回Array或Promise') @@ -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] +} diff --git a/src/utils/request.js b/src/utils/request.js index 5186099..442d7c1 100644 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -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({