mirror of
https://gitee.com/dromara/go-view.git
synced 2025-04-06 03:58:04 +08:00
perf: 修改数据展示为动态获取
This commit is contained in:
parent
33d78ffcda
commit
f53f4d57f2
@ -45,14 +45,15 @@ export const useChartDataFetch = (
|
|||||||
const res: any = await http(requestHttpType.value)(completePath || '', {})
|
const res: any = await http(requestHttpType.value)(completePath || '', {})
|
||||||
if (res.data) {
|
if (res.data) {
|
||||||
try {
|
try {
|
||||||
|
const filter = targetComponent.filter
|
||||||
// 更新回调函数
|
// 更新回调函数
|
||||||
if (updateCallback) {
|
if (updateCallback) {
|
||||||
updateCallback(res.data)
|
updateCallback(newFunctionHandle(res.data, filter))
|
||||||
} else {
|
} else {
|
||||||
// eCharts 组件配合 vChart 库更新方式
|
// eCharts 组件配合 vChart 库更新方式
|
||||||
if (chartFrame === ChartFrameEnum.ECHARTS) {
|
if (chartFrame === ChartFrameEnum.ECHARTS) {
|
||||||
if (vChartRef.value) {
|
if (vChartRef.value) {
|
||||||
const filter = targetComponent.filter
|
|
||||||
vChartRef.value.setOption({ dataset: newFunctionHandle(res.data, filter) })
|
vChartRef.value.setOption({ dataset: newFunctionHandle(res.data, filter) })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
fontSize: `${indicatorTextSize}px`
|
fontSize: `${indicatorTextSize}px`
|
||||||
}"
|
}"
|
||||||
>
|
>
|
||||||
{{option.dataset}} {{unit}}
|
{{ option.dataset }} {{ unit }}
|
||||||
</n-text>
|
</n-text>
|
||||||
</n-progress>
|
</n-progress>
|
||||||
</template>
|
</template>
|
||||||
@ -24,7 +24,8 @@
|
|||||||
import { PropType, toRefs, watch, shallowReactive } from 'vue'
|
import { PropType, toRefs, watch, shallowReactive } from 'vue'
|
||||||
import { useChartDataFetch } from '@/hooks'
|
import { useChartDataFetch } from '@/hooks'
|
||||||
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
|
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
|
||||||
import config, { option as configOption } from './config'
|
import config, { option as configOption } from './config'
|
||||||
|
import { toNumber } from '@/utils'
|
||||||
|
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
chartConfig: {
|
chartConfig: {
|
||||||
@ -57,11 +58,11 @@ const option = shallowReactive({
|
|||||||
watch(
|
watch(
|
||||||
() => props.chartConfig.option.dataset,
|
() => props.chartConfig.option.dataset,
|
||||||
(newData: any) => {
|
(newData: any) => {
|
||||||
option.dataset = newData
|
option.dataset = toNumber(newData, 2)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
// 预览更新
|
// 预览更新
|
||||||
useChartDataFetch(props.chartConfig, useChartEditStore, (newData: number) => {
|
useChartDataFetch(props.chartConfig, useChartEditStore, (newData: number) => {
|
||||||
option.dataset = newData
|
option.dataset = toNumber(newData, 2)
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
@ -10,7 +10,7 @@ import 'echarts-liquidfill/src/liquidFill.js'
|
|||||||
import { CanvasRenderer } from 'echarts/renderers'
|
import { CanvasRenderer } from 'echarts/renderers'
|
||||||
import { GridComponent } from 'echarts/components'
|
import { GridComponent } from 'echarts/components'
|
||||||
import config from './config'
|
import config from './config'
|
||||||
import { isPreview } from '@/utils'
|
import { isPreview, isString } from '@/utils'
|
||||||
import { chartColorsSearch, defaultTheme } from '@/settings/chartThemes/index'
|
import { chartColorsSearch, defaultTheme } from '@/settings/chartThemes/index'
|
||||||
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
|
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
|
||||||
import { useChartDataFetch } from '@/hooks'
|
import { useChartDataFetch } from '@/hooks'
|
||||||
@ -66,7 +66,8 @@ watch(
|
|||||||
)
|
)
|
||||||
|
|
||||||
// 数据处理
|
// 数据处理
|
||||||
const dataHandle = (newData: number) => {
|
const dataHandle = (newData: number | string) => {
|
||||||
|
newData = isString(newData) ? parseFloat(newData) : newData
|
||||||
return parseFloat(newData.toFixed(2))
|
return parseFloat(newData.toFixed(2))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
import isObject from 'lodash/isObject'
|
||||||
|
|
||||||
export function isString(p: any): p is string {
|
export function isString(p: any): p is string {
|
||||||
return typeof p === 'string'
|
return typeof p === 'string'
|
||||||
}
|
}
|
||||||
@ -21,3 +23,11 @@ export function isNull(p: any): p is null {
|
|||||||
export function isArray(p: any): p is [] {
|
export function isArray(p: any): p is [] {
|
||||||
return Array.isArray(p)
|
return Array.isArray(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const toNumber = (number: number | string, toFixedNumber = 2) => {
|
||||||
|
return isString(number) ? parseFloat(parseFloat(number).toFixed(2)) : number
|
||||||
|
}
|
||||||
|
|
||||||
|
export const toString = (str: any) => {
|
||||||
|
return isNumber(str) ? `${str}` : (isObject(str) ? JSON.stringify(str) : str)
|
||||||
|
}
|
@ -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) ? JSON.stringify(fnRes) : fnRes
|
const resHandle = toString && isString(fnRes) ? fnRes : JSON.stringify(fnRes)
|
||||||
// 成功回调
|
// 成功回调
|
||||||
successCallBack && successCallBack(resHandle)
|
successCallBack && successCallBack(resHandle)
|
||||||
return resHandle
|
return resHandle
|
||||||
|
@ -90,6 +90,7 @@ import { useFile } from '../../hooks/useFile.hooks'
|
|||||||
import { useTargetData } from '../../../hooks/useTargetData.hook'
|
import { useTargetData } from '../../../hooks/useTargetData.hook'
|
||||||
import isObject from 'lodash/isObject'
|
import isObject from 'lodash/isObject'
|
||||||
import cloneDeep from 'lodash/cloneDeep'
|
import cloneDeep from 'lodash/cloneDeep'
|
||||||
|
import { toString } from '@/utils'
|
||||||
|
|
||||||
const { targetData } = useTargetData()
|
const { targetData } = useTargetData()
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
@ -120,11 +121,6 @@ const filterShow = computed(() => {
|
|||||||
return targetData.value.data.requestDataType === RequestDataTypeEnum.AJAX
|
return targetData.value.data.requestDataType === RequestDataTypeEnum.AJAX
|
||||||
})
|
})
|
||||||
|
|
||||||
// 字符串处理
|
|
||||||
const dataToString = (str: any) => {
|
|
||||||
return isObject(str) ? JSON.stringify(str) : str
|
|
||||||
}
|
|
||||||
|
|
||||||
// 是图表类型
|
// 是图表类型
|
||||||
const isCharts = computed(() => {
|
const isCharts = computed(() => {
|
||||||
return targetData.value.chartConfig.package === PackagesCategoryEnum.CHARTS
|
return targetData.value.chartConfig.package === PackagesCategoryEnum.CHARTS
|
||||||
@ -178,7 +174,7 @@ const filterRes = (data: any) => {
|
|||||||
if(targetData.value.filter) {
|
if(targetData.value.filter) {
|
||||||
const fn = new Function('data', targetData.value.filter)
|
const fn = new Function('data', targetData.value.filter)
|
||||||
const res = fn(cloneDeep(data))
|
const res = fn(cloneDeep(data))
|
||||||
return dataToString(res)
|
return toString(res)
|
||||||
}
|
}
|
||||||
return data
|
return data
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -55,7 +55,6 @@
|
|||||||
<n-space justify="space-between">
|
<n-space justify="space-between">
|
||||||
<div>
|
<div>
|
||||||
<n-space vertical>
|
<n-space vertical>
|
||||||
<n-text depth="3">// 数据参数 => data </n-text>
|
|
||||||
<n-tag type="info">
|
<n-tag type="info">
|
||||||
<span class="func-keyword">function</span> filter(data) {
|
<span class="func-keyword">function</span> filter(data) {
|
||||||
</n-tag>
|
</n-tag>
|
||||||
@ -69,11 +68,7 @@
|
|||||||
<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
|
<n-code :code="toString(sourceData)" language="typescript" :word-wrap="true"></n-code>
|
||||||
:code="dataToString(targetData.option.dataset)"
|
|
||||||
language="typescript"
|
|
||||||
:word-wrap="true"
|
|
||||||
></n-code>
|
|
||||||
</n-space>
|
</n-space>
|
||||||
</div>
|
</div>
|
||||||
<div class="editor-data-show">
|
<div class="editor-data-show">
|
||||||
@ -109,15 +104,19 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { ref, computed } from 'vue'
|
import { ref, computed, watch, toRefs } from 'vue'
|
||||||
import { MonacoEditor } from '@/components/Pages/MonacoEditor'
|
import { MonacoEditor } from '@/components/Pages/MonacoEditor'
|
||||||
import { useTargetData } from '../../../hooks/useTargetData.hook'
|
import { useTargetData } from '../../../hooks/useTargetData.hook'
|
||||||
|
import { RequestHttpEnum, RequestDataTypeEnum, ResultEnum } from '@/enums/httpEnum'
|
||||||
import { icon } from '@/plugins'
|
import { icon } from '@/plugins'
|
||||||
import { goDialog, isString } from '@/utils'
|
import { goDialog, toString } from '@/utils'
|
||||||
|
import { http } from '@/api/http'
|
||||||
import cloneDeep from 'lodash/cloneDeep'
|
import cloneDeep from 'lodash/cloneDeep'
|
||||||
|
|
||||||
const { PencilIcon, TrashIcon, FilterIcon, DocumentTextIcon } = icon.ionicons5
|
const { PencilIcon, TrashIcon, FilterIcon, DocumentTextIcon } = icon.ionicons5
|
||||||
const { targetData, chartEditStore } = useTargetData()
|
const { targetData, chartEditStore } = useTargetData()
|
||||||
|
const { requestDataType } = toRefs(targetData.value.data)
|
||||||
|
const { requestOriginUrl } = toRefs(chartEditStore.getRequestGlobalConfig)
|
||||||
|
|
||||||
// 受控弹窗
|
// 受控弹窗
|
||||||
const showModal = ref(false)
|
const showModal = ref(false)
|
||||||
@ -125,17 +124,35 @@ const showModal = ref(false)
|
|||||||
const filter = ref(targetData.value.filter || `return data`)
|
const filter = ref(targetData.value.filter || `return data`)
|
||||||
// 过滤错误标识
|
// 过滤错误标识
|
||||||
const errorFlag = ref(false)
|
const errorFlag = ref(false)
|
||||||
|
// 目标静态/接口数据
|
||||||
|
const sourceData = ref<any>('')
|
||||||
|
|
||||||
// 字符串处理
|
// 动态获取数据
|
||||||
const dataToString = (str: any) => {
|
const fetchTargetData = async () => {
|
||||||
return isString(str) ? str : JSON.stringify(str)
|
try {
|
||||||
|
const { requestUrl, requestHttpType } = targetData.value.data
|
||||||
|
if (!requestUrl) {
|
||||||
|
window['$message'].warning('请求参数不正确,请检查!')
|
||||||
|
sourceData.value = '请求参数不正确,请检查!'
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const completePath = requestOriginUrl && requestOriginUrl.value + requestUrl
|
||||||
|
const res = await http(requestHttpType)(completePath || '', {})
|
||||||
|
if (res.status === ResultEnum.SUCCESS) {
|
||||||
|
sourceData.value = res.data
|
||||||
|
console.log(sourceData.value)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
window['$message'].warning('数据异常,请检查接口数据!')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 过滤结果
|
// 过滤结果
|
||||||
const filterRes = computed(() => {
|
const filterRes = computed(() => {
|
||||||
try {
|
try {
|
||||||
const fn = new Function('data', filter.value)
|
const fn = new Function('data', filter.value)
|
||||||
const res = fn(cloneDeep(targetData.value.option.dataset))
|
const res = fn(cloneDeep(sourceData.value))
|
||||||
errorFlag.value = false
|
errorFlag.value = false
|
||||||
return JSON.stringify(res)
|
return JSON.stringify(res)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -173,6 +190,13 @@ const saveFilter = () => {
|
|||||||
targetData.value.filter = filter.value
|
targetData.value.filter = filter.value
|
||||||
closeFilter()
|
closeFilter()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
watch(
|
||||||
|
() => showModal.value,
|
||||||
|
(newData: boolean) => {
|
||||||
|
if (newData) fetchTargetData()
|
||||||
|
}
|
||||||
|
)
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="scss" scoped>
|
<style lang="scss" scoped>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user