goview_vue/src/hooks/useChartDataFetch.hook.ts

123 lines
3.8 KiB
TypeScript
Raw Normal View History

2023-03-08 15:10:33 +08:00
import { ref, toRefs, toRaw, watch } from 'vue'
import type VChart from 'vue-echarts'
2022-07-20 21:30:32 +08:00
import { customizeHttp } from '@/api/http'
2022-11-15 21:25:35 +08:00
import { useChartDataPondFetch } from '@/hooks/'
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'
2023-02-15 09:20:48 +08:00
import { setOption } from '@/packages/public/chart'
// 获取类型
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
2022-11-15 21:25:35 +08:00
// 数据池
const { addGlobalDataInterface } = useChartDataPondFetch()
// 组件类型
const { chartFrame } = targetComponent.chartConfig
2022-12-15 20:53:28 +08:00
2022-11-15 21:25:35 +08:00
// eCharts 组件配合 vChart 库更新方式
const echartsUpdateHandle = (dataset: any) => {
if (chartFrame === ChartFrameEnum.ECHARTS) {
if (vChartRef.value) {
2023-02-15 09:20:48 +08:00
setOption(vChartRef.value, { dataset: dataset })
2022-11-15 21:25:35 +08:00
}
}
}
const requestIntervalFn = () => {
2022-06-25 17:03:03 +08:00
const chartEditStore = useChartEditStore()
2022-07-20 21:30:32 +08:00
// 全局数据
2022-07-20 18:14:11 +08:00
const {
requestOriginUrl,
requestIntervalUnit: globalUnit,
requestInterval: globalRequestInterval
} = toRefs(chartEditStore.getRequestGlobalConfig)
2022-07-20 21:30:32 +08:00
// 目标组件
const {
requestDataType,
requestUrl,
2022-07-20 18:14:11 +08:00
requestIntervalUnit: targetUnit,
requestInterval: targetInterval
} = toRefs(targetComponent.request)
2022-07-16 19:01:05 +08:00
2022-06-25 17:03:03 +08:00
// 非请求类型
2022-07-20 18:14:11 +08:00
if (requestDataType.value !== RequestDataTypeEnum.AJAX) return
2022-07-16 20:57:01 +08:00
2022-07-16 19:01:05 +08:00
try {
// 处理地址
// @ts-ignore
2022-07-20 18:14:11 +08:00
if (requestUrl?.value) {
2022-07-16 19:01:05 +08:00
// 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 () => {
2022-11-15 21:25:35 +08:00
const res = await customizeHttp(toRaw(targetComponent.request), toRaw(chartEditStore.getRequestGlobalConfig))
if (res) {
2022-07-16 19:01:05 +08:00
try {
const filter = targetComponent.filter
2022-11-15 21:25:35 +08:00
echartsUpdateHandle(newFunctionHandle(res?.data, res, filter))
2022-07-16 19:01:05 +08:00
// 更新回调函数
if (updateCallback) {
updateCallback(newFunctionHandle(res?.data, res, 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
}
// 普通初始化与组件交互处理监听
2023-03-11 16:58:32 +08:00
watch(
() => targetComponent.request,
() => {
fetchFn()
},
{
immediate: true,
2023-03-11 16:58:32 +08:00
deep: true
}
)
2023-03-08 15:10:33 +08:00
2022-07-20 18:14:11 +08:00
// 定时时间
const time = targetInterval && targetInterval.value ? targetInterval.value : globalRequestInterval.value
// 单位
const unit = targetInterval && targetInterval.value ? targetUnit.value : globalUnit.value
// 开启轮询
if (time) fetchInterval = setInterval(fetchFn, intervalUnitHandle(time, unit))
2022-07-16 19:01:05 +08:00
}
// eslint-disable-next-line no-empty
2022-09-27 10:18:20 +08:00
} catch (error) {
console.log(error)
2022-09-27 10:18:20 +08:00
}
2022-06-25 17:03:03 +08:00
}
2022-11-15 21:25:35 +08:00
if (isPreview()) {
2022-12-15 20:53:28 +08:00
// 判断是否是数据池类型
targetComponent.request.requestDataType === RequestDataTypeEnum.Pond
2022-11-15 21:25:35 +08:00
? addGlobalDataInterface(targetComponent, useChartEditStore, updateCallback || echartsUpdateHandle)
: requestIntervalFn()
}
return { vChartRef }
}