fix(utils): getUrlObj 健壮性

This commit is contained in:
菲鸽 2024-06-11 13:29:18 +08:00
parent 66faff8614
commit 81e660877e

View File

@ -50,12 +50,18 @@ const ensureDecodeURIComponent = (url: string) => {
*/ */
export const getUrlObj = (url: string) => { export const getUrlObj = (url: string) => {
const [path, queryStr] = url.split('?') const [path, queryStr] = url.split('?')
console.log(path, queryStr) // console.log(path, queryStr)
if (!queryStr) {
return {
path,
query: {},
}
}
const query: Record<string, string> = {} const query: Record<string, string> = {}
queryStr.split('&').forEach((item) => { queryStr.split('&').forEach((item) => {
const [key, value] = item.split('=') const [key, value] = item.split('=')
console.log(key, value) // console.log(key, value)
query[key] = ensureDecodeURIComponent(value) // 这里需要统一 decodeURIComponent 一下可以兼容h5和微信y query[key] = ensureDecodeURIComponent(value) // 这里需要统一 decodeURIComponent 一下可以兼容h5和微信y
}) })
return { path, query } return { path, query }