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: '',
@ -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,9 +887,58 @@ 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)
@ -918,19 +966,18 @@ 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++) {
@ -943,11 +990,15 @@ export default {
// //
scriptExecute (isInit = false) { scriptExecute (isInit = false) {
this.getPramsList() 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.requestType === 'front') {
if (this.dataForm.config.paramsList && this.dataForm.config.paramsList.length){ axiosFormatting({ ...this.newDataForm.config }).then((res) => {
}
axiosFormatting({ ...this.dataForm.config }).then((res) => {
this.dataPreviewList = res.list this.dataPreviewList = res.list
}) })
} else { } else {
@ -964,6 +1015,7 @@ export default {
}) })
} }
}
}, },
// //
clearType () { clearType () {

@ -2,12 +2,12 @@ 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 => {

Loading…
Cancel
Save