!78 feat: 过滤方法增加api返回顶级对象作为参数,增强过滤器兼容性

Merge pull request !78 from dodu/dev-api-filter
This commit is contained in:
奔跑的面条
2022-10-13 10:05:39 +00:00
committed by Gitee
4 changed files with 27 additions and 31 deletions
@@ -127,8 +127,8 @@ const sendHandle = async () => {
try {
const res = await customizeHttp(toRaw(targetData.value.request), toRaw(chartEditStore.requestGlobalConfig))
loading.value = false
if (res && res.data) {
targetData.value.option.dataset = newFunctionHandle(res.data, targetData.value.filter)
if (res) {
targetData.value.option.dataset = newFunctionHandle(res?.data, res, targetData.value.filter)
showMatching.value = true
return
}
@@ -1,7 +1,7 @@
<template>
<template v-if="targetData.filter">
<n-card>
<p><span class="func-keyword">function</span>&nbsp;&nbsp;filter(data)&nbsp;&nbsp;{</p>
<p><span class="func-keyword">function</span>&nbsp;&nbsp;filter(data, res)&nbsp;&nbsp;{</p>
<!-- 函数体 -->
<div class="go-ml-4">
<n-code :code="targetData.filter" language="typescript"></n-code>
@@ -47,7 +47,7 @@
<div>
<n-space vertical>
<n-tag type="info">
<span class="func-keyword">function</span>&nbsp;&nbsp;filter(data)&nbsp;&nbsp;{
<span class="func-keyword">function</span>&nbsp;&nbsp;filter(data, res)&nbsp;&nbsp;{
</n-tag>
<monaco-editor v-model:modelValue="filter" width="460px" height="380px" language="javascript" />
<n-tag type="info">}</n-tag>
@@ -58,7 +58,13 @@
<n-space :size="15" vertical>
<div class="editor-data-show">
<n-space>
<n-text depth="3">目标数据</n-text>
<n-text depth="3">目标数据(data)</n-text>
<n-code :code="toString(sourceData?.data) || 'undefined'" language="json" :word-wrap="true"></n-code>
</n-space>
</div>
<div class="editor-data-show">
<n-space>
<n-text depth="3">目标数据(res)</n-text>
<n-code :code="toString(sourceData)" language="json" :word-wrap="true"></n-code>
</n-space>
</div>
@@ -123,8 +129,8 @@ const sourceData = ref<any>('')
const fetchTargetData = async () => {
try {
const res = await customizeHttp(toRaw(targetData.value.request), toRaw(chartEditStore.requestGlobalConfig))
if (res && res.data) {
sourceData.value = res.data
if (res) {
sourceData.value = res
return
}
window['$message'].warning('数据异常,请检查参数!')
@@ -136,8 +142,9 @@ const fetchTargetData = async () => {
// 过滤结果
const filterRes = computed(() => {
try {
const fn = new Function('data', filter.value)
const res = fn(cloneDeep(sourceData.value))
const fn = new Function('data', 'res', filter.value)
const response = cloneDeep(sourceData.value)
const res = fn(response?.data, response)
// eslint-disable-next-line vue/no-side-effects-in-computed-properties
errorFlag.value = false
return toString(res)