goview_vue/src/hooks/useChartDataFetch.hook.ts

96 lines
3.0 KiB
TypeScript
Raw Normal View History

2022-06-25 17:03:03 +08:00
import { ref, toRefs } from 'vue'
import type VChart from 'vue-echarts'
import { http } from '@/api/http'
2022-06-25 17:03:03 +08:00
import { CreateComponentType, ChartFrameEnum } from '@/packages/index.d'
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
import { RequestDataTypeEnum } from '@/enums/httpEnum'
2022-07-16 19:01:05 +08:00
import { isPreview, newFunctionHandle, intervalUnitHandle } from '@/utils'
// 获取类型
2022-03-25 19:58:39 +08:00
type ChartEditStoreType = typeof useChartEditStore
/**
* setdata
* @param targetComponent
* @param useChartEditStore
* @param updateCallback
*/
export const useChartDataFetch = (
targetComponent: CreateComponentType,
2022-06-25 17:03:03 +08:00
useChartEditStore: ChartEditStoreType,
updateCallback?: (...args: any) => any
) => {
const vChartRef = ref<typeof VChart | null>(null)
let fetchInterval: any = 0
const requestIntervalFn = () => {
2022-06-25 17:03:03 +08:00
const chartEditStore = useChartEditStore()
2022-07-16 19:01:05 +08:00
const { requestOriginUrl, requestInterval, requestIntervalUnit } = toRefs(chartEditStore.getRequestGlobalConfig)
2022-06-25 17:03:03 +08:00
// 组件类型
const { chartFrame } = targetComponent.chartConfig
// 请求配置
const {
requestDataType,
requestHttpType,
requestUrl,
requestInterval: targetInterval
} = toRefs(targetComponent.request)
2022-07-16 19:01:05 +08:00
2022-06-25 17:03:03 +08:00
// 非请求类型
2022-07-16 20:57:01 +08:00
if (
requestDataType.value !== RequestDataTypeEnum.AJAX ||
!requestInterval ||
!requestInterval.value ||
!targetInterval ||
!targetInterval.value
)
return
2022-07-16 19:01:05 +08:00
try {
// 处理地址
// @ts-ignore
if (requestUrl?.value && requestInterval && requestInterval.value > 0) {
// requestOriginUrl 允许为空
const completePath = requestOriginUrl && requestOriginUrl.value + requestUrl.value
if (!completePath) return
2022-07-16 19:01:05 +08:00
clearInterval(fetchInterval)
2022-07-16 19:01:05 +08:00
const fetchFn = async () => {
const res: any = await http(requestHttpType.value)(completePath || '', {})
if (res.data) {
try {
const filter = targetComponent.filter
// 更新回调函数
if (updateCallback) {
updateCallback(newFunctionHandle(res.data, filter))
} else {
// eCharts 组件配合 vChart 库更新方式
if (chartFrame === ChartFrameEnum.ECHARTS) {
if (vChartRef.value) {
vChartRef.value.setOption({ dataset: newFunctionHandle(res.data, filter) })
}
2022-06-25 20:29:42 +08:00
}
}
2022-07-16 19:01:05 +08:00
} catch (error) {
console.error(error)
2022-06-25 17:03:03 +08:00
}
}
2022-06-25 17:03:03 +08:00
}
2022-07-16 19:01:05 +08:00
// 立即调用
fetchFn()
2022-07-16 19:01:05 +08:00
// 开启定时
const time = targetInterval && targetInterval.value ? targetInterval.value : requestInterval.value
// 处理单位时间
fetchInterval = setInterval(fetchFn, intervalUnitHandle(time, requestIntervalUnit.value))
}
} catch (error) {}
2022-06-25 17:03:03 +08:00
}
isPreview() && requestIntervalFn()
return { vChartRef }
}