feat:http数据集参数配置

main
liu.shiyi 2 years ago
parent 2dd48e2a9d
commit 8753d23d0f

@ -542,6 +542,7 @@
ref="paramsSettingDialog"
:params-list="dataForm.config.paramsList"
@saveParams="saveParams"
@replaceParams="replaceParams"
/>
<OutputFieldDialog
ref="outputFieldDialog"
@ -642,6 +643,7 @@ export default {
value: 'date',
label: '日期'
}],
newDataForm: {}, //
dataForm: {
id: '',
name: '',
@ -734,9 +736,8 @@ export default {
const { script, paramsList, fieldDesc, fieldList } = config
this.dataForm = { id, name, typeId, remark, datasetType, moduleCode, editable, sourceId, config: { ...config } }
this.fieldDesc = fieldDesc
console.log(fieldList)
this.outputFieldList = fieldList
console.log(this.outputFieldList)
this.replaceParams(paramsList)
this.scriptExecute(true)
})
}
@ -761,7 +762,6 @@ export default {
return
}
}
requestType: '',
this.$refs[formName].validate((valid) => {
if (valid) {
this.saveloading = true
@ -809,7 +809,6 @@ export default {
// header
addHeader () {
const header = { key: '', type: 'string', value: '', remark: '' }
console.log(this.dataForm)
this.dataForm.config.headers.push(_.cloneDeep(header))
},
// header
@ -888,9 +887,58 @@ export default {
})
this.fieldDesc = fieldDesc
},
//
replaceParams (paramsList) {
this.newDataForm = _.cloneDeep(this.dataForm)
this.newDataForm.config.url = this.evalStrFunc(paramsList, this.newDataForm.config.url)
this.newDataForm.config.headers = this.evalArrFunc(paramsList, this.newDataForm.config.headers)
this.newDataForm.config.params = this.evalArrFunc(paramsList, this.newDataForm.config.params)
this.newDataForm.config.body = this.evalStrFunc(paramsList, this.newDataForm.config.body)
},
evalStrFunc (paramsList, string) {
// name, value { name: '', token: '123'}
const params = paramsList.reduce((acc, cur) => {
acc[cur.name] = cur.value
return acc
}, {})
// url ${xxx} ${params.xxx}
const str = string.replace(/\$\{(\w+)\}/g, (match, p1) => {
return '${params.' + p1 + '}'
})
const transformStr = ''
// ${}, 使eval
eval('transformStr = `' + str + '`')
return transformStr
},
evalArrFunc (paramsList, arr) {
// name, value { name: '', token: '123'}
const params = paramsList.reduce((acc, cur) => {
acc[cur.name] = cur.value
return acc
}, {})
// name, value { _name: '${name}', _token: '${token}'}
const paramsListObj = arr.reduce((acc, cur) => {
acc[cur.key] = cur.value
return acc
}, {})
//
const paramsListStr = JSON.stringify(paramsListObj)
// url ${xxx} ${params.xxx}
const str = paramsListStr.replace(/\$\{(\w+)\}/g, (match, p1) => {
return '${params.' + p1 + '}'
})
const transformStr = ''
// ${}, 使eval
eval('transformStr = `' + str + '`')
const obj = JSON.parse(transformStr)
return obj
},
//
getPramsList () {
const reg = /\${(.*?)}/g
const paramNames1 = this.getValName(this.dataForm.config.url)
const paramNames2 = this.dataForm.config?.headers.map(item => {
const nameList = this.getValName(item.value)
@ -918,19 +966,18 @@ export default {
type: 'String',
value: '',
status: 1,
require: 0,
require: 1,
remark: ''
})
}
})
this.dataForm.config.paramsList = _.cloneDeep(params)
},
// ${}
getValName (str) {
const reg = /\$\{(.+?)\}/;
const reg_g = /\$\{(.+?)\}/g;
const result = str.match(reg_g);
const reg = /\$\{(.+?)\}/
const reg_g = /\$\{(.+?)\}/g
const result = str.match(reg_g)
const list = []
if (result) {
for (let i = 0; i < result.length; i++) {
@ -943,11 +990,15 @@ export default {
//
scriptExecute (isInit = false) {
this.getPramsList()
//
const flag = this.dataForm.config.paramsList.some(item => !item.value)
if (this.dataForm.config.paramsList && this.dataForm.config.paramsList.length && flag) {
this.$refs.paramsSettingDialog.open()
} else {
//
//
if (this.dataForm.config.requestType === 'front') {
if (this.dataForm.config.paramsList && this.dataForm.config.paramsList.length){
}
axiosFormatting({ ...this.dataForm.config }).then((res) => {
axiosFormatting({ ...this.newDataForm.config }).then((res) => {
this.dataPreviewList = res.list
})
} else {
@ -964,6 +1015,7 @@ export default {
})
}
}
},
//
clearType () {

@ -2,12 +2,12 @@ import axios from 'axios'
import { Loading, Message } from 'element-ui'
export default function axiosFormatting (customConfig) {
// 将请求头和请求参数的值转化为对象形式
const headers = arrToObject(customConfig.headers)
const params = arrToObject(customConfig.params)
// const headers = arrToObject(customConfig.headers)
// const params = arrToObject(customConfig.params)
const httpConfig = {
timeout: 1000 * 30,
baseURL: '',
headers
headers: customConfig.headers
}
// let loadingInstance = null // 加载全局的loading
const instance = axios.create(httpConfig)
@ -44,12 +44,14 @@ export default function axiosFormatting (customConfig) {
return Promise.reject(response.data.message)
}
})
const body = {}
eval(customConfig.body)
return new Promise((resolve, reject) => {
instance({
method: customConfig.method,
url: customConfig.url,
params,
data: customConfig.method === 'post' ? customConfig.body : undefined
params: customConfig.params,
data: customConfig.method === 'post' ? body : undefined
}).then(response => {
resolve(response)
}).catch(error => {

Loading…
Cancel
Save