From 75149f994f7f0783ce857be200682d9530d90396 Mon Sep 17 00:00:00 2001 From: light <978854603@qq.com> Date: Mon, 30 Mar 2026 21:08:24 +0800 Subject: [PATCH] 1 --- src/utils/http.js | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/utils/http.js b/src/utils/http.js index ea89ad5..f6f77ef 100644 --- a/src/utils/http.js +++ b/src/utils/http.js @@ -1,6 +1,27 @@ import axios from 'axios' import { formatDate } from './tools' +/** 递归删除对象中值为空字符串的键(不修改数组内的空字符串元素) */ +function stripEmptyStringKeys(value) { + if (value === null || value === undefined) { + return value + } + if (Array.isArray(value)) { + return value.map((item) => stripEmptyStringKeys(item)) + } + if (typeof value === 'object' && Object.prototype.toString.call(value) === '[object Object]') { + const next = {} + for (const k of Object.keys(value)) { + const v = value[k] + if (v === '') { + continue + } + next[k] = stripEmptyStringKeys(v) + } + return next + } + return value +} class Http { constructor() { @@ -131,13 +152,18 @@ class Http { } param = JSON.parse(JSON.stringify(param)) - return param + return stripEmptyStringKeys(param) } formatFormDataParam(param) { + param = param || {} let formData = new FormData() - Object.keys(param).forEach(key => { - formData.append(key, param[key]) + Object.keys(param).forEach((key) => { + const v = param[key] + if (v === '') { + return + } + formData.append(key, v) }) return formData }