From 806020211c46dbea8a2ef321e78d54fb001057a0 Mon Sep 17 00:00:00 2001 From: gerson <1405270578@qq.com> Date: 星期四, 04 七月 2024 00:22:48 +0800 Subject: [PATCH] 路由和请求均可唤起登录弹窗 --- src/utils/util.ts | 36 ++++++++++++++++++++++++++++++++++++ 1 files changed, 36 insertions(+), 0 deletions(-) diff --git a/src/utils/util.ts b/src/utils/util.ts index 5821a21..327d7e7 100644 --- a/src/utils/util.ts +++ b/src/utils/util.ts @@ -11,6 +11,42 @@ import axios from 'axios'; import { MAIN_URL } from '../constants'; + +/** + * 鏅�氬璞¤浆涓� formData + * @param obj + */ +export const toFormData = (obj: any) => { + const formData = new FormData(); + + const addFormData = (subObj, prePrefix = '', isArray = false) => { + for (const key in subObj) { + if (Object.prototype.hasOwnProperty.call(subObj, key)) { + const value = subObj[key]; + let currentKey = ''; + if (prePrefix === '') { + currentKey = key; + } else if (isArray) { + currentKey = `${prePrefix}[${key}]`; + } else { + currentKey = `${prePrefix}.${key}`; + } + + if (value != null && Array.isArray(value) && value.length > 0) { + addFormData(value, currentKey, true); + } else if (value != null && typeof value === 'object' && Object.values(value).length > 0) { + addFormData(value, currentKey, false); + } else { + formData.append(currentKey, value); + } + } + } + }; + addFormData(obj); + return formData; +}; + + /** * @description 褰撶鍒� JSON 涓瓨鍦ㄨ繃闀跨殑鏁板瓧鏃讹紝浣跨敤 JSONbigString 瑙f瀽锛屾暟瀛椾細杞负瀛楃涓插鐞� * 鐢ㄦ硶锛欽SONbigString.parse(jsonStr)锛� -- Gitblit v1.9.3