mirror of
https://gitee.com/dromara/go-view.git
synced 2025-04-24 18:30:17 +08:00
perf: 优化获取数据 hooks 函数
This commit is contained in:
parent
340492f784
commit
78626b3c04
@ -1,7 +1,7 @@
|
|||||||
import { ref, toRefs, watchEffect, nextTick } from 'vue'
|
import { ref, toRefs } from 'vue'
|
||||||
import type VChart from 'vue-echarts'
|
import type VChart from 'vue-echarts'
|
||||||
import { http } from '@/api/http'
|
import { http } from '@/api/http'
|
||||||
import { CreateComponentType, PackagesCategoryEnum } from '@/packages/index.d'
|
import { CreateComponentType, ChartFrameEnum } from '@/packages/index.d'
|
||||||
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
|
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
|
||||||
import { RequestDataTypeEnum } from '@/enums/httpEnum'
|
import { RequestDataTypeEnum } from '@/enums/httpEnum'
|
||||||
import { isPreview } from '@/utils'
|
import { isPreview } from '@/utils'
|
||||||
@ -23,54 +23,58 @@ export const useChartDataFetch = (
|
|||||||
const vChartRef = ref<typeof VChart | null>(null)
|
const vChartRef = ref<typeof VChart | null>(null)
|
||||||
let fetchInterval: any = 0
|
let fetchInterval: any = 0
|
||||||
|
|
||||||
isPreview() &&
|
const requestInterval = () => {
|
||||||
watchEffect(() => {
|
const chartEditStore = useChartEditStore()
|
||||||
clearInterval(fetchInterval)
|
const { requestOriginUrl, requestInterval } = toRefs(
|
||||||
|
chartEditStore.getRequestGlobalConfig
|
||||||
|
)
|
||||||
|
// 组件类型
|
||||||
|
const { chartFrame } = targetComponent.chartConfig
|
||||||
|
// 请求配置
|
||||||
|
const { requestDataType, requestHttpType, requestUrl } = toRefs(
|
||||||
|
targetComponent.data
|
||||||
|
)
|
||||||
|
// 非请求类型
|
||||||
|
if (requestDataType.value !== RequestDataTypeEnum.AJAX) return
|
||||||
|
// 处理地址
|
||||||
|
if (requestUrl?.value && requestInterval.value > 0) {
|
||||||
|
// requestOriginUrl 允许为空
|
||||||
|
const completePath =
|
||||||
|
requestOriginUrl && requestOriginUrl.value + requestUrl.value
|
||||||
|
if (!completePath) return
|
||||||
|
|
||||||
const chartEditStore = useChartEditStore()
|
const fetchFn = async () => {
|
||||||
const { requestOriginUrl, requestInterval } = toRefs(
|
const res: any = await http(requestHttpType.value)(completePath || '', {})
|
||||||
chartEditStore.getRequestGlobalConfig
|
if (res.data) {
|
||||||
)
|
try {
|
||||||
const { requestDataType, requestHttpType, requestUrl } = toRefs(
|
// echarts 组件更新方式
|
||||||
targetComponent.data
|
if (chartFrame === ChartFrameEnum.ECHARTS) {
|
||||||
)
|
if (vChartRef.value) {
|
||||||
if (requestDataType.value !== RequestDataTypeEnum.AJAX) return
|
vChartRef.value.setOption({ dataset: res.data })
|
||||||
// 处理地址
|
|
||||||
if (requestUrl?.value && requestInterval.value > 0) {
|
|
||||||
// requestOriginUrl 允许为空
|
|
||||||
const completePath =
|
|
||||||
requestOriginUrl && requestOriginUrl.value + requestUrl.value
|
|
||||||
if (!completePath) return
|
|
||||||
|
|
||||||
fetchInterval = setInterval(async () => {
|
|
||||||
const res:any = await http(requestHttpType.value)(completePath || '', {})
|
|
||||||
if (res.data) {
|
|
||||||
// 是否是 Echarts 组件
|
|
||||||
const isECharts =
|
|
||||||
targetComponent.chartConfig.package ===
|
|
||||||
PackagesCategoryEnum.CHARTS
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (isECharts && vChartRef?.value?.setOption) {
|
|
||||||
nextTick(() => {
|
|
||||||
if (vChartRef.value) {
|
|
||||||
vChartRef.value.setOption({ dataset: res.data })
|
|
||||||
}
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
// 若遵守规范使用 dataset 作为数据 key,则会自动赋值数据
|
|
||||||
targetComponent.option.dataset && (targetComponent.option.dataset = res.data)
|
|
||||||
}
|
}
|
||||||
if (updateCallback) {
|
} else {
|
||||||
updateCallback(res.data)
|
// 若遵守规范使用 dataset 作为数据 key,则会自动赋值数据
|
||||||
}
|
targetComponent.option.dataset && (targetComponent.option.dataset = res.data)
|
||||||
} catch (error) {
|
|
||||||
console.error(error)
|
|
||||||
}
|
}
|
||||||
|
// 更新回调函数
|
||||||
|
if (updateCallback) {
|
||||||
|
updateCallback(res.data)
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error)
|
||||||
}
|
}
|
||||||
}, requestInterval.value * 1000)
|
}
|
||||||
}
|
}
|
||||||
})
|
|
||||||
|
// 立即调用
|
||||||
|
fetchFn()
|
||||||
|
// 开启定时
|
||||||
|
setInterval(fetchFn, requestInterval.value * 1000)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
isPreview() && requestInterval()
|
||||||
|
|
||||||
|
|
||||||
return { vChartRef }
|
return { vChartRef }
|
||||||
}
|
}
|
||||||
|
@ -15,19 +15,21 @@
|
|||||||
|
|
||||||
background-color:${backgroundColor}`"
|
background-color:${backgroundColor}`"
|
||||||
>
|
>
|
||||||
{{ dataset }}
|
{{ option.dataset }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { PropType, toRefs } from 'vue'
|
import { PropType, toRefs, shallowReactive, watch } from 'vue'
|
||||||
import { CreateComponentType } from '@/packages/index.d'
|
import { CreateComponentType } from '@/packages/index.d'
|
||||||
|
import { useChartDataFetch } from '@/hooks'
|
||||||
|
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
|
||||||
|
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
chartConfig: {
|
chartConfig: {
|
||||||
type: Object as PropType<CreateComponentType>,
|
type: Object as PropType<CreateComponentType>,
|
||||||
required: true,
|
required: true
|
||||||
},
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const { w, h } = toRefs(props.chartConfig.attr)
|
const { w, h } = toRefs(props.chartConfig.attr)
|
||||||
@ -42,8 +44,27 @@ const {
|
|||||||
borderColor,
|
borderColor,
|
||||||
borderRadius,
|
borderRadius,
|
||||||
writingMode,
|
writingMode,
|
||||||
backgroundColor,
|
backgroundColor
|
||||||
} = toRefs(props.chartConfig.option)
|
} = toRefs(props.chartConfig.option)
|
||||||
|
|
||||||
|
const option = shallowReactive({
|
||||||
|
dataset: ''
|
||||||
|
})
|
||||||
|
|
||||||
|
// 手动更新
|
||||||
|
watch(
|
||||||
|
() => props.chartConfig.option.dataset,
|
||||||
|
(newData: any) => {
|
||||||
|
option.dataset = newData
|
||||||
|
}, {
|
||||||
|
immediate: true
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// 预览更新
|
||||||
|
useChartDataFetch(props.chartConfig, useChartEditStore, (newData: any) => {
|
||||||
|
option.dataset = newData
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="scss" scoped>
|
<style lang="scss" scoped>
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { PropType, toRefs, reactive, watch } from 'vue'
|
import { PropType, toRefs, shallowReactive, watch } from 'vue'
|
||||||
import { CreateComponentType } from '@/packages/index.d'
|
import { CreateComponentType } from '@/packages/index.d'
|
||||||
import { useChartDataFetch } from '@/hooks'
|
import { useChartDataFetch } from '@/hooks'
|
||||||
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
|
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
|
||||||
@ -14,33 +14,30 @@ import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore
|
|||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
chartConfig: {
|
chartConfig: {
|
||||||
type: Object as PropType<CreateComponentType>,
|
type: Object as PropType<CreateComponentType>,
|
||||||
required: true,
|
required: true
|
||||||
},
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const option = reactive({
|
const option = shallowReactive({
|
||||||
dataset: ''
|
dataset: ''
|
||||||
})
|
})
|
||||||
|
|
||||||
const { w, h } = toRefs(props.chartConfig.attr)
|
const { w, h } = toRefs(props.chartConfig.attr)
|
||||||
|
const { size, gradient } = toRefs(props.chartConfig.option)
|
||||||
|
|
||||||
const {
|
|
||||||
size,
|
|
||||||
gradient
|
|
||||||
} = toRefs(props.chartConfig.option)
|
|
||||||
|
|
||||||
const callback = (newData: string) => {
|
|
||||||
option.dataset = newData
|
|
||||||
}
|
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
() => props.chartConfig.option.dataset,
|
() => props.chartConfig.option.dataset,
|
||||||
() => {
|
(newData: any) => {
|
||||||
option.dataset = props.chartConfig.option.dataset
|
option.dataset = newData
|
||||||
}, { immediate: true }
|
}, {
|
||||||
|
immediate: true
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
useChartDataFetch(props.chartConfig, useChartEditStore, callback)
|
useChartDataFetch(props.chartConfig, useChartEditStore, (newData: any) => {
|
||||||
|
option.dataset = newData
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="scss" scoped>
|
<style lang="scss" scoped>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user