2022-03-24 14:19:07 +08:00
|
|
|
import { ref, toRefs, watchEffect, nextTick } from 'vue'
|
|
|
|
import type VChart from 'vue-echarts'
|
2022-03-23 20:41:50 +08:00
|
|
|
import { http } from '@/api/http'
|
|
|
|
import { CreateComponentType } from '@/packages/index.d'
|
|
|
|
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
|
|
|
|
import { RequestDataTypeEnum } from '@/enums/httpEnum'
|
2022-03-24 14:19:07 +08:00
|
|
|
import { isPreview } from '@/utils'
|
2022-03-23 20:41:50 +08:00
|
|
|
|
|
|
|
/**
|
2022-03-24 14:19:07 +08:00
|
|
|
* 图表的 setdata 数据监听与更改
|
2022-03-23 20:41:50 +08:00
|
|
|
* @param chartConfig
|
|
|
|
*/
|
|
|
|
export const useChartDataFetch = (chartConfig: CreateComponentType) => {
|
2022-03-24 14:19:07 +08:00
|
|
|
const vChartRef = ref<typeof VChart | null>(null)
|
|
|
|
let fetchInterval: any = 0
|
2022-03-23 20:41:50 +08:00
|
|
|
|
2022-03-24 16:51:31 +08:00
|
|
|
isPreview() && watchEffect(() => {
|
2022-03-23 20:41:50 +08:00
|
|
|
clearInterval(fetchInterval)
|
|
|
|
|
|
|
|
const chartEditStore = useChartEditStore()
|
|
|
|
const { requestOriginUrl, requestInterval } = toRefs(
|
|
|
|
chartEditStore.getRequestGlobalConfig
|
|
|
|
)
|
|
|
|
const { requestDataType, requestHttpType, requestUrl } = toRefs(
|
|
|
|
chartConfig.data
|
|
|
|
)
|
|
|
|
if (requestDataType.value !== RequestDataTypeEnum.AJAX) return
|
|
|
|
// 处理地址
|
|
|
|
if (requestUrl?.value && requestInterval.value > 0) {
|
|
|
|
// requestOriginUrl 允许为空
|
2022-03-24 14:19:07 +08:00
|
|
|
const completePath =
|
|
|
|
requestOriginUrl && requestOriginUrl.value + requestUrl.value
|
2022-03-23 20:41:50 +08:00
|
|
|
if (!completePath) return
|
|
|
|
|
|
|
|
fetchInterval = setInterval(async () => {
|
|
|
|
const res = await http(requestHttpType.value)(completePath || '', {})
|
2022-03-24 14:19:07 +08:00
|
|
|
if (res.data) {
|
|
|
|
nextTick(() => {
|
2022-03-24 16:51:31 +08:00
|
|
|
if(vChartRef.value) {
|
|
|
|
vChartRef.value.setOption({dataset: res.data})
|
2022-03-24 14:19:07 +08:00
|
|
|
}
|
|
|
|
})
|
2022-03-23 20:41:50 +08:00
|
|
|
}
|
|
|
|
}, requestInterval.value * 1000)
|
|
|
|
}
|
|
|
|
})
|
2022-03-24 14:19:07 +08:00
|
|
|
|
|
|
|
return { vChartRef }
|
2022-03-23 20:41:50 +08:00
|
|
|
}
|