feat(http): 添加深度过滤请求数据中的 null 和 undefined 值
在 axios 请求处理中新增 omitDeepNil 工具函数,用于深度过滤请求数据中的 null 和 undefined 值,避免无效数据影响请求处理
This commit is contained in:
parent
a33d5eed54
commit
7eb6521dfd
|
|
@ -0,0 +1,28 @@
|
|||
import { isNil, isPlainObject } from 'lodash-es';
|
||||
|
||||
/**
|
||||
* 深度递归移除对象中的 null 和 undefined 值
|
||||
* @param obj - 需要处理的对象、数组或原始值
|
||||
* @returns 移除 nil 值后的新对象/数组/原始值
|
||||
*/
|
||||
export const omitDeepNil = <T>(obj: T): T => {
|
||||
if (obj === null || obj === undefined) {
|
||||
return obj;
|
||||
}
|
||||
|
||||
if (Array.isArray(obj)) {
|
||||
return obj.map(item => omitDeepNil(item)) as T;
|
||||
}
|
||||
|
||||
if (isPlainObject(obj)) {
|
||||
const result: Record<string, any> = {};
|
||||
for (const [key, value] of Object.entries(obj)) {
|
||||
if (!isNil(value)) {
|
||||
result[key] = omitDeepNil(value);
|
||||
}
|
||||
}
|
||||
return result as T;
|
||||
}
|
||||
|
||||
return obj;
|
||||
};
|
||||
|
|
@ -3,6 +3,7 @@ import { useUserStore } from "@/pinia/stores/user"
|
|||
import { getToken } from "@@/utils/cache/cookies"
|
||||
import axios from "axios"
|
||||
import { get, merge } from "lodash-es"
|
||||
import { omitDeepNil } from "@/common/utils/lodash/lodashObj"
|
||||
|
||||
/** 退出登录并强制刷新页面(会重定向到登录页) */
|
||||
function logout() {
|
||||
|
|
@ -117,6 +118,10 @@ function createRequest(instance: AxiosInstance) {
|
|||
}
|
||||
// 将默认配置 defaultConfig 和传入的自定义配置 config 进行合并成为 mergeConfig
|
||||
const mergeConfig = merge(defaultConfig, config)
|
||||
// 深度过滤掉 undefined 和 null 字段
|
||||
if (mergeConfig.data && typeof mergeConfig.data === "object") {
|
||||
mergeConfig.data = omitDeepNil(mergeConfig.data)
|
||||
}
|
||||
return instance(mergeConfig)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,14 +27,8 @@
|
|||
<van-button size="small" type="primary" @click="refresh">重试</van-button>
|
||||
</div>
|
||||
|
||||
<!-- 空状态 -->
|
||||
<div v-else-if="!hasAvailableCells" class="empty-state">
|
||||
<van-icon name="box" size="48px" color="#C0C4CC" />
|
||||
<span class="empty-text">暂无可用格口</span>
|
||||
</div>
|
||||
|
||||
<!-- 格口类型选择区域 -->
|
||||
<div v-else class="cell-type-selection">
|
||||
<div class="cell-type-selection">
|
||||
<div class="cell-type-grid">
|
||||
<div
|
||||
v-for="stat in cellTypeStats"
|
||||
|
|
@ -56,12 +50,12 @@
|
|||
</div>
|
||||
|
||||
<!-- 操作按钮区域 -->
|
||||
<div v-if="props.showButtons && hasAvailableCells && !loading" class="action-buttons">
|
||||
<div v-if="props.showButtons && !loading" class="action-buttons">
|
||||
<van-row :gutter="16">
|
||||
<van-col :span="12">
|
||||
<van-button type="primary" block @click="handleDeposit"
|
||||
:loading="depositLoading"
|
||||
:disabled="depositLoading || retrieveLoading">
|
||||
:disabled="depositLoading || retrieveLoading || !hasAvailableCells">
|
||||
物品暂存
|
||||
</van-button>
|
||||
</van-col>
|
||||
|
|
|
|||
Loading…
Reference in New Issue