diff --git a/data-room-ui/packages/DataSetManagement/src/HttpEditForm.vue b/data-room-ui/packages/DataSetManagement/src/HttpEditForm.vue index 6975cdb1..30ecfd96 100644 --- a/data-room-ui/packages/DataSetManagement/src/HttpEditForm.vue +++ b/data-room-ui/packages/DataSetManagement/src/HttpEditForm.vue @@ -307,16 +307,6 @@ - - - + + + { return '${params.' + p1 + '}' }) - const transformStr = '' // 将字符串中的${}替换为变量, 使用eval执行 eval('transformStr = `' + str + '`') @@ -998,6 +996,7 @@ export default { // 如果动态参数已配置则调接口 // 如果是前端代理,则自行组装接口及参数并调接口 if (this.dataForm.config.requestType === 'front') { + this.replaceParams(this.dataForm.config.paramsList) axiosFormatting({ ...this.newDataForm.config }).then((res) => { this.dataPreviewList = res.list }) diff --git a/data-room-ui/packages/js/utils/httpParamsFormatting.js b/data-room-ui/packages/js/utils/httpParamsFormatting.js index 339e153f..372a07a3 100644 --- a/data-room-ui/packages/js/utils/httpParamsFormatting.js +++ b/data-room-ui/packages/js/utils/httpParamsFormatting.js @@ -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}` + } }