fix: 解决过滤器的错误处理

This commit is contained in:
奔跑的面条 2022-07-06 20:14:41 +08:00
parent 4ba3d8803a
commit 74e30390cf
4 changed files with 8 additions and 15 deletions

View File

@ -53,7 +53,6 @@ export const useChartDataFetch = (
// eCharts 组件配合 vChart 库更新方式 // eCharts 组件配合 vChart 库更新方式
if (chartFrame === ChartFrameEnum.ECHARTS) { if (chartFrame === ChartFrameEnum.ECHARTS) {
if (vChartRef.value) { if (vChartRef.value) {
vChartRef.value.setOption({ dataset: newFunctionHandle(res.data, filter) }) vChartRef.value.setOption({ dataset: newFunctionHandle(res.data, filter) })
} }
} }

View File

@ -5,7 +5,7 @@ import throttle from 'lodash/throttle'
import Image_404 from '../assets/images/exception/image-404.png' import Image_404 from '../assets/images/exception/image-404.png'
import html2canvas from 'html2canvas' import html2canvas from 'html2canvas'
import { downloadByA } from './file' import { downloadByA } from './file'
import { isString } from './type' import { toString } from './type'
import cloneDeep from 'lodash/cloneDeep'; import cloneDeep from 'lodash/cloneDeep';
/** /**
@ -196,7 +196,7 @@ export const canvasCut = (html: HTMLElement | null, callback?: Function) => {
export const newFunctionHandle = ( export const newFunctionHandle = (
data: any, data: any,
funcStr?: string, funcStr?: string,
toString?: boolean, isToString?: boolean,
errorCallBack?: Function, errorCallBack?: Function,
successCallBack?: Function successCallBack?: Function
) => { ) => {
@ -204,7 +204,7 @@ export const newFunctionHandle = (
if (!funcStr) return data if (!funcStr) return data
const fn = new Function('data', funcStr) const fn = new Function('data', funcStr)
const fnRes = fn( cloneDeep(data)) const fnRes = fn( cloneDeep(data))
const resHandle = toString && isString(fnRes) ? fnRes : JSON.stringify(fnRes) const resHandle = isToString ? toString(fnRes) : fnRes
// 成功回调 // 成功回调
successCallBack && successCallBack(resHandle) successCallBack && successCallBack(resHandle)
return resHandle return resHandle

View File

@ -72,7 +72,7 @@
</div> </div>
</n-space> </n-space>
<n-card size="small"> <n-card size="small">
<n-code :code="filterRes(getSource)" language="json"></n-code> <n-code :code="filterRes(source)" language="json"></n-code>
</n-card> </n-card>
</n-space> </n-space>
</n-timeline-item> </n-timeline-item>
@ -126,11 +126,6 @@ const isCharts = computed(() => {
return targetData.value.chartConfig.package === PackagesCategoryEnum.CHARTS return targetData.value.chartConfig.package === PackagesCategoryEnum.CHARTS
}) })
//
const getSource = computed(() => {
return JSON.stringify(source.value)
})
// //
const matchingHandle = (mapping: string) => { const matchingHandle = (mapping: string) => {
let res = DataResultEnum.SUCCESS let res = DataResultEnum.SUCCESS
@ -176,7 +171,7 @@ const filterRes = (data: any) => {
const res = fn(cloneDeep(data)) const res = fn(cloneDeep(data))
return toString(res) return toString(res)
} }
return data return toString(cloneDeep(data))
} catch (error) { } catch (error) {
return '过滤函数错误' return '过滤函数错误'
} }

View File

@ -68,13 +68,13 @@
<div class="editor-data-show"> <div class="editor-data-show">
<n-space> <n-space>
<n-text depth="3">目标数据</n-text> <n-text depth="3">目标数据</n-text>
<n-code :code="toString(sourceData)" language="typescript" :word-wrap="true"></n-code> <n-code :code="toString(sourceData)" language="json" :word-wrap="true"></n-code>
</n-space> </n-space>
</div> </div>
<div class="editor-data-show"> <div class="editor-data-show">
<n-space> <n-space>
<n-text depth="3">过滤器结果</n-text> <n-text depth="3">过滤器结果</n-text>
<n-code :code="filterRes" language="typescript" :word-wrap="true"></n-code> <n-code :code="filterRes" language="json" :word-wrap="true"></n-code>
</n-space> </n-space>
</div> </div>
</n-space> </n-space>
@ -140,7 +140,6 @@ const fetchTargetData = async () => {
const res = await http(requestHttpType)(completePath || '', {}) const res = await http(requestHttpType)(completePath || '', {})
if (res.status === ResultEnum.SUCCESS) { if (res.status === ResultEnum.SUCCESS) {
sourceData.value = res.data sourceData.value = res.data
console.log(sourceData.value)
return return
} }
} catch (error) { } catch (error) {
@ -154,7 +153,7 @@ const filterRes = computed(() => {
const fn = new Function('data', filter.value) const fn = new Function('data', filter.value)
const res = fn(cloneDeep(sourceData.value)) const res = fn(cloneDeep(sourceData.value))
errorFlag.value = false errorFlag.value = false
return JSON.stringify(res) return toString(res)
} catch (error) { } catch (error) {
errorFlag.value = true errorFlag.value = true
return '过滤函数错误' return '过滤函数错误'