Files
aiot-uniapp/src/http/queryString.ts
feige996 dc5fdda452 refactor(http): 重构HTTP模块结构,将相关文件迁移至src/http目录
将原本分散在src/utils和src/interceptors下的HTTP相关代码统一迁移至src/http目录,包括请求工具、拦截器、类型定义等
移除不再使用的src/interceptors目录
调整相关文件的引用路径
新增统一的HTTP模块入口文件
2025-07-08 16:59:32 +08:00

30 lines
946 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 将对象序列化为URL查询字符串用于替代第三方的 qs 库,节省宝贵的体积
* 支持基本类型值和数组,不支持嵌套对象
* @param obj 要序列化的对象
* @returns 序列化后的查询字符串
*/
export function stringifyQuery(obj: Record<string, any>): string {
if (!obj || typeof obj !== 'object' || Array.isArray(obj))
return ''
return Object.entries(obj)
.filter(([_, value]) => value !== undefined && value !== null)
.map(([key, value]) => {
// 对键进行编码
const encodedKey = encodeURIComponent(key)
// 处理数组类型
if (Array.isArray(value)) {
return value
.filter(item => item !== undefined && item !== null)
.map(item => `${encodedKey}=${encodeURIComponent(item)}`)
.join('&')
}
// 处理基本类型
return `${encodedKey}=${encodeURIComponent(value)}`
})
.join('&')
}