/**
* 将对象参数转换为 URL 查询字符串
* @param params 要转换的参数对象
* @returns 转换后的查询字符串,不包含开头的 ?
*/
export function objectToQueryString(params: Record<string, any>): string {
if (!params || Object.keys(params).length === 0) {
return ''
}
return Object.entries(params)
.filter(([_, value]) => value !== undefined && value !== null)
.map(([key, value]) => {
if (Array.isArray(value)) {
return value.map(v => `${encodeURIComponent(key)}=${encodeURIComponent(String(v))}`).join('&')
}
return `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`
})
.join('&')
}