feat:http数据集参数配置

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

@ -542,6 +542,7 @@
ref="paramsSettingDialog" ref="paramsSettingDialog"
:params-list="dataForm.config.paramsList" :params-list="dataForm.config.paramsList"
@saveParams="saveParams" @saveParams="saveParams"
@replaceParams="replaceParams"
/> />
<OutputFieldDialog <OutputFieldDialog
ref="outputFieldDialog" ref="outputFieldDialog"
@ -642,6 +643,7 @@ export default {
value: 'date', value: 'date',
label: '日期' label: '日期'
}], }],
newDataForm: {}, //
dataForm: { dataForm: {
id: '', id: '',
name: '', name: '',
@ -656,7 +658,7 @@ export default {
headers: [], headers: [],
params: [], params: [],
body: '', body: '',
paramsList:[], paramsList: [],
requestScript: '', requestScript: '',
responseScript: '' responseScript: ''
} }
@ -734,9 +736,8 @@ export default {
const { script, paramsList, fieldDesc, fieldList } = config const { script, paramsList, fieldDesc, fieldList } = config
this.dataForm = { id, name, typeId, remark, datasetType, moduleCode, editable, sourceId, config: { ...config } } this.dataForm = { id, name, typeId, remark, datasetType, moduleCode, editable, sourceId, config: { ...config } }
this.fieldDesc = fieldDesc this.fieldDesc = fieldDesc
console.log(fieldList)
this.outputFieldList = fieldList this.outputFieldList = fieldList
console.log(this.outputFieldList) this.replaceParams(paramsList)
this.scriptExecute(true) this.scriptExecute(true)
}) })
} }
@ -761,7 +762,6 @@ export default {
return return
} }
} }
requestType: '',
this.$refs[formName].validate((valid) => { this.$refs[formName].validate((valid) => {
if (valid) { if (valid) {
this.saveloading = true this.saveloading = true
@ -809,7 +809,6 @@ export default {
// header // header
addHeader () { addHeader () {
const header = { key: '', type: 'string', value: '', remark: '' } const header = { key: '', type: 'string', value: '', remark: '' }
console.log(this.dataForm)
this.dataForm.config.headers.push(_.cloneDeep(header)) this.dataForm.config.headers.push(_.cloneDeep(header))
}, },
// header // header
@ -888,24 +887,73 @@ export default {
}) })
this.fieldDesc = fieldDesc 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(){ getPramsList () {
const reg = /\${(.*?)}/g
const paramNames1 = this.getValName(this.dataForm.config.url) const paramNames1 = this.getValName(this.dataForm.config.url)
const paramNames2 = this.dataForm.config?.headers.map(item=>{ const paramNames2 = this.dataForm.config?.headers.map(item => {
const nameList = this.getValName(item.value) const nameList = this.getValName(item.value)
if (nameList && nameList.length){ if (nameList && nameList.length) {
return nameList[0] return nameList[0]
} }
}) })
const paramNames3 = this.dataForm.config?.params.map(item=>{ const paramNames3 = this.dataForm.config?.params.map(item => {
const nameList = this.getValName(item.value) const nameList = this.getValName(item.value)
if (nameList && nameList.length){ if (nameList && nameList.length) {
return nameList[0] return nameList[0]
} }
}) })
const paramNames4=this.getValName(this.dataForm.config.body) const paramNames4 = this.getValName(this.dataForm.config.body)
const paramNames = new Set([...paramNames1,...paramNames2,...paramNames3,...paramNames4]) const paramNames = new Set([...paramNames1, ...paramNames2, ...paramNames3, ...paramNames4])
const names = this.dataForm.config?.paramsList?.map(item => item.name) const names = this.dataForm.config?.paramsList?.map(item => item.name)
const params = [] const params = []
paramNames.forEach(name => { paramNames.forEach(name => {
@ -918,21 +966,20 @@ export default {
type: 'String', type: 'String',
value: '', value: '',
status: 1, status: 1,
require: 0, require: 1,
remark: '' remark: ''
}) })
} }
}) })
this.dataForm.config.paramsList = _.cloneDeep(params) this.dataForm.config.paramsList = _.cloneDeep(params)
}, },
// ${} // ${}
getValName(str){ getValName (str) {
const reg = /\$\{(.+?)\}/; const reg = /\$\{(.+?)\}/
const reg_g = /\$\{(.+?)\}/g; const reg_g = /\$\{(.+?)\}/g
const result = str.match(reg_g); const result = str.match(reg_g)
const list = [] const list = []
if (result){ if (result) {
for (let i = 0; i < result.length; i++) { for (let i = 0; i < result.length; i++) {
const item = result[i] const item = result[i]
list.push(item.match(reg)[1]) list.push(item.match(reg)[1])
@ -943,26 +990,31 @@ export default {
// //
scriptExecute (isInit = false) { scriptExecute (isInit = false) {
this.getPramsList() this.getPramsList()
// //
if (this.dataForm.config.requestType === 'front') { const flag = this.dataForm.config.paramsList.some(item => !item.value)
if (this.dataForm.config.paramsList && this.dataForm.config.paramsList.length){ if (this.dataForm.config.paramsList && this.dataForm.config.paramsList.length && flag) {
} this.$refs.paramsSettingDialog.open()
axiosFormatting({ ...this.dataForm.config }).then((res) => {
this.dataPreviewList = res.list
})
} else { } else {
// //
const script = JSON.stringify(this.dataForm.config) //
const executeParams = { if (this.dataForm.config.requestType === 'front') {
script, axiosFormatting({ ...this.newDataForm.config }).then((res) => {
params: this.dataForm.paramsList, this.dataPreviewList = res.list
dataSetType: 'http' })
} } else {
datasetExecuteTest(executeParams).then(res => { //
this.dataPreviewList = res const script = JSON.stringify(this.dataForm.config)
}).catch((e) => { const executeParams = {
script,
params: this.dataForm.paramsList,
dataSetType: 'http'
}
datasetExecuteTest(executeParams).then(res => {
this.dataPreviewList = res
}).catch((e) => {
}) })
}
} }
}, },
// //

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

Loading…
Cancel
Save