class Parsing {
constructor(url) {
this.url = url
this.parameter = []
}
// 添加参数
append(...args) {
if(args.length <= 1) {
for (let iterator in args[0]) {
this.parameter.push(`${iterator}=${args[0][iterator]}`)
}
}else {
this.parameter.push(`${args[0]}=${args[1]}`)
}
}
// 获取url地址
getUrl() {
this.parameter.forEach((item, index) => {
if(index === 0) {
this.url += `?${item}`
}else {
this.url += `&${item}`
}
})
return this.url
}
// 返回路径和body参数
getPostUrl(data) {
return [this.getUrl(), data]
}
}
let data = {
userName: '12',
password: '18'
}
let url = new Parsing('/ips/user/login')
// 与后端字段名相同,直接传递
url.append(data)
url.append("name", 13) // 单个添加
console.log(url.getUrl()) // /ips/user/login?name=13&userName=12&password=18
解决URL参数过长,不清晰
最新推荐文章于 2023-11-11 23:26:22 发布