2023-03-08 15:10:33 +08:00
|
|
|
|
import { ref, toRefs, toRaw, watch } from 'vue'
|
2022-03-24 14:19:07 +08:00
|
|
|
|
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'
|
2022-03-23 20:41:50 +08:00
|
|
|
|
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-23 20:41:50 +08:00
|
|
|
|
|
2022-04-02 11:34:54 +08:00
|
|
|
|
// 获取类型
|
2022-03-25 19:58:39 +08:00
|
|
|
|
type ChartEditStoreType = typeof useChartEditStore
|
|
|
|
|
|
2022-03-23 20:41:50 +08:00
|
|
|
|
/**
|
2022-04-02 11:34:54 +08:00
|
|
|
|
* setdata 数据监听与更改
|
|
|
|
|
* @param targetComponent
|
|
|
|
|
* @param useChartEditStore 若直接引会报错,只能动态传递
|
|
|
|
|
* @param updateCallback 自定义更新函数
|
2022-03-23 20:41:50 +08:00
|
|
|
|
*/
|
2022-04-02 11:34:54 +08:00
|
|
|
|
export const useChartDataFetch = (
|
|
|
|
|
targetComponent: CreateComponentType,
|
2022-06-25 17:03:03 +08:00
|
|
|
|
useChartEditStore: ChartEditStoreType,
|
2022-04-02 11:34:54 +08:00
|
|
|
|
updateCallback?: (...args: any) => any
|
|
|
|
|
) => {
|
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-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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-05 21:44:16 +08:00
|
|
|
|
const requestIntervalFn = () => {
|
2022-06-25 17:03:03 +08:00
|
|
|
|
const chartEditStore = useChartEditStore()
|
2022-09-16 12:26:12 +08:00
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
// 目标组件
|
2022-07-08 17:53:52 +08:00
|
|
|
|
const {
|
|
|
|
|
requestDataType,
|
|
|
|
|
requestUrl,
|
2022-07-20 18:14:11 +08:00
|
|
|
|
requestIntervalUnit: targetUnit,
|
2022-07-08 17:53:52 +08:00
|
|
|
|
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-03-23 20:41:50 +08:00
|
|
|
|
|
2022-07-16 19:01:05 +08:00
|
|
|
|
clearInterval(fetchInterval)
|
2022-07-05 21:44:16 +08:00
|
|
|
|
|
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))
|
2022-10-12 16:41:49 +08:00
|
|
|
|
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) {
|
2022-10-12 16:41:49 +08:00
|
|
|
|
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-04-02 11:34:54 +08:00
|
|
|
|
}
|
2022-06-25 17:03:03 +08:00
|
|
|
|
}
|
2022-07-05 21:44:16 +08:00
|
|
|
|
|
2023-03-14 22:19:55 +08:00
|
|
|
|
// 普通初始化与组件交互处理监听
|
2023-03-11 16:58:32 +08:00
|
|
|
|
watch(
|
|
|
|
|
() => targetComponent.request,
|
|
|
|
|
() => {
|
|
|
|
|
fetchFn()
|
|
|
|
|
},
|
|
|
|
|
{
|
2023-03-14 22:19:55 +08:00
|
|
|
|
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
|
|
|
|
}
|
2022-09-16 12:26:12 +08:00
|
|
|
|
// eslint-disable-next-line no-empty
|
2022-09-27 10:18:20 +08:00
|
|
|
|
} catch (error) {
|
2022-10-12 16:41:49 +08:00
|
|
|
|
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()
|
|
|
|
|
}
|
2022-03-24 14:19:07 +08:00
|
|
|
|
return { vChartRef }
|
2022-03-23 20:41:50 +08:00
|
|
|
|
}
|