feat: 组件绑定js数据集,修改执行js脚本逻辑

main
wu.jian2 2 years ago
parent de551f7977
commit 74efa1cf29

@ -71,7 +71,7 @@ export default {
pageCode: this.pageCode, pageCode: this.pageCode,
size: size, size: size,
type: config.type type: config.type
}).then((data) => { }).then((res) => {
// 如果是http数据集的前端代理则需要调封装的axios请求 // 如果是http数据集的前端代理则需要调封装的axios请求
// if (data.executionByFrontend) { // if (data.executionByFrontend) {
// axiosFormatting(data.data).then(res => { // axiosFormatting(data.data).then(res => {
@ -79,23 +79,38 @@ export default {
// this.changeChartConfig(config) // this.changeChartConfig(config)
// }) // })
// } // }
if (data.datasetType !== 'http' && data.executionByFrontend) { // if (data.datasetType !== 'http' && data.executionByFrontend) {
try { // try {
const scriptAfterReplacement = data.data.replace(/\${(.*?)}/g, (match, p) => { // const scriptAfterReplacement = data.data.replace(/\${(.*?)}/g, (match, p) => {
// // 根据parmas的key获取value
// return `'${this.config.dataSource?.params[p]}' || '${p}'`
// })
// // eslint-disable-next-line no-new-func
// const scriptMethod = new Function(scriptAfterReplacement)
// data.data = scriptMethod()
// } catch (error) {
// console.error('数据集脚本执行失败', error)
// }
// }
if (res.executionByFrontend) {
if (res.data.datasetType === 'js') {
try {
const scriptAfterReplacement = res.data.script.replace(/\${(.*?)}/g, (match, p) => {
// 根据parmas的key获取value // 根据parmas的key获取value
return `'${this.config.dataSource?.params[p]}' || '${p}'` return `'${this.config.dataSource?.params[p]}' || '${p}'`
}) })
// eslint-disable-next-line no-new-func // eslint-disable-next-line no-new-func
const scriptMethod = new Function(scriptAfterReplacement) const scriptMethod = new Function(scriptAfterReplacement)
data.data = scriptMethod() res.data = scriptMethod()
} catch (error) { } catch (error) {
console.error('数据集脚本执行失败', error) console.error('JS数据集脚本执行失败', error)
}
} }
} }
config = this.dataFormatting(config, data) config = this.dataFormatting(config, res)
this.changeChartConfig(config) this.changeChartConfig(config)
}).catch((err) => { }).catch((err) => {
console.log(err) console.error(err)
}).finally(() => { }).finally(() => {
resolve(config) resolve(config)
}) })
@ -117,7 +132,7 @@ export default {
filterList: filterList || this.filterList filterList: filterList || this.filterList
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
getUpdateChartInfo(params).then((data) => { getUpdateChartInfo(params).then((res) => {
// 如果是http数据集的前端代理则需要调封装的axios请求 // 如果是http数据集的前端代理则需要调封装的axios请求
// if (data.executionByFrontend) { // if (data.executionByFrontend) {
// axiosFormatting(data.data).then(res => { // axiosFormatting(data.data).then(res => {
@ -125,20 +140,22 @@ export default {
// this.changeChartConfig(config) // this.changeChartConfig(config)
// }) // })
// } // }
if (data.executionByFrontend) { if (res.executionByFrontend) {
try { if (res.data.datasetType === 'js') {
const scriptAfterReplacement = data.data.replace(/\${(.*?)}/g, (match, p) => { try {
const scriptAfterReplacement = res.data.script.replace(/\${(.*?)}/g, (match, p) => {
// 根据parmas的key获取value // 根据parmas的key获取value
return `'${this.config.dataSource?.params[p]}' || '${p}'` return `'${this.config.dataSource?.params[p]}' || '${p}'`
}) })
// eslint-disable-next-line no-new-func // eslint-disable-next-line no-new-func
const scriptMethod = new Function(scriptAfterReplacement) const scriptMethod = new Function(scriptAfterReplacement)
data.data = scriptMethod() res.data = scriptMethod()
} catch (error) { } catch (error) {
console.error('数据集脚本执行失败', error) console.error('JS数据集脚本执行失败', error)
}
} }
} }
config = this.dataFormatting(config, data) config = this.dataFormatting(config, res)
this.changeChartConfig(config) this.changeChartConfig(config)
if (this.chart) { if (this.chart) {
// 单指标组件和多指标组件的changeData传参不同 // 单指标组件和多指标组件的changeData传参不同
@ -149,7 +166,7 @@ export default {
} }
} }
}).catch(err => { }).catch(err => {
console.log(err) console.error(err)
}).finally(() => { }).finally(() => {
resolve(config) resolve(config)
}) })

Loading…
Cancel
Save