|
|
|
@ -18,7 +18,14 @@ export default function axiosFormatting (customConfig) {
|
|
|
|
|
* config.headers['token'] = sessionStorage.getItem('token') || ''
|
|
|
|
|
*/
|
|
|
|
|
// 执行请求脚本
|
|
|
|
|
// https://mock.presstime.cn/mock/64bf8a00ce1b0ea640809069/test_copy_copy_copy/httpData?token=123&ss=ss
|
|
|
|
|
const req = { ...config, urlKey: {} }
|
|
|
|
|
eval(customConfig.requestScript)
|
|
|
|
|
for (const key in req.urlKey) {
|
|
|
|
|
customConfig.url = replaceUrlParam(customConfig.url, key, req.urlKey[key])
|
|
|
|
|
}
|
|
|
|
|
config = { ...config, ...req, url: customConfig.url }
|
|
|
|
|
console.log(config.url)
|
|
|
|
|
return config
|
|
|
|
|
}, error => {
|
|
|
|
|
// 对请求错误做些什么
|
|
|
|
@ -45,6 +52,9 @@ export default function axiosFormatting (customConfig) {
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
const body = {}
|
|
|
|
|
const pattern = /(body\.\w+)=(\w+)/g
|
|
|
|
|
const replacement = "$1='$2'"
|
|
|
|
|
customConfig.body = customConfig.body.replace(pattern, replacement)
|
|
|
|
|
eval(customConfig.body)
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
instance({
|
|
|
|
@ -59,11 +69,13 @@ export default function axiosFormatting (customConfig) {
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
// 数组转化为对象
|
|
|
|
|
function arrToObject (list) {
|
|
|
|
|
const obj = {}
|
|
|
|
|
list.forEach(item => {
|
|
|
|
|
obj[item.key] = item.value
|
|
|
|
|
})
|
|
|
|
|
return obj
|
|
|
|
|
// 动态替换url后面参数的值
|
|
|
|
|
function replaceUrlParam (url, paramName, paramValue) {
|
|
|
|
|
const regex = new RegExp(`([?&])${paramName}=.*?(&|$)`, 'i')
|
|
|
|
|
const separator = url.indexOf('?') !== -1 ? '&' : '?'
|
|
|
|
|
if (url.match(regex)) {
|
|
|
|
|
return url.replace(regex, `$1${paramName}=${paramValue}$2`)
|
|
|
|
|
} else {
|
|
|
|
|
return `${url}${separator}${paramName}=${paramValue}`
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|