2022-06-25 17:03:03 +08:00
|
|
|
|
import { ref, toRefs } from 'vue'
|
2022-03-24 14:19:07 +08:00
|
|
|
|
import type VChart from 'vue-echarts'
|
2022-03-23 20:41:50 +08:00
|
|
|
|
import { http } from '@/api/http'
|
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-05 21:44:16 +08:00
|
|
|
|
import { isPreview, newFunctionHandle } from '@/utils'
|
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-07-05 21:44:16 +08:00
|
|
|
|
const requestIntervalFn = () => {
|
2022-06-25 17:03:03 +08:00
|
|
|
|
const chartEditStore = useChartEditStore()
|
2022-07-05 21:44:16 +08:00
|
|
|
|
const { requestOriginUrl, requestInterval } = toRefs(chartEditStore.getRequestGlobalConfig)
|
2022-06-25 17:03:03 +08:00
|
|
|
|
// 组件类型
|
|
|
|
|
const { chartFrame } = targetComponent.chartConfig
|
|
|
|
|
// 请求配置
|
2022-07-08 17:53:52 +08:00
|
|
|
|
const {
|
|
|
|
|
requestDataType,
|
|
|
|
|
requestHttpType,
|
|
|
|
|
requestUrl,
|
|
|
|
|
requestInterval: targetInterval
|
|
|
|
|
} = toRefs(targetComponent.request)
|
2022-06-25 17:03:03 +08:00
|
|
|
|
// 非请求类型
|
|
|
|
|
if (requestDataType.value !== RequestDataTypeEnum.AJAX) return
|
|
|
|
|
// 处理地址
|
|
|
|
|
if (requestUrl?.value && requestInterval.value > 0) {
|
|
|
|
|
// requestOriginUrl 允许为空
|
2022-07-05 21:44:16 +08:00
|
|
|
|
const completePath = requestOriginUrl && requestOriginUrl.value + requestUrl.value
|
2022-06-25 17:03:03 +08:00
|
|
|
|
if (!completePath) return
|
2022-03-23 20:41:50 +08:00
|
|
|
|
|
2022-07-05 21:44:16 +08:00
|
|
|
|
clearInterval(fetchInterval)
|
|
|
|
|
|
2022-06-25 17:03:03 +08:00
|
|
|
|
const fetchFn = async () => {
|
|
|
|
|
const res: any = await http(requestHttpType.value)(completePath || '', {})
|
|
|
|
|
if (res.data) {
|
|
|
|
|
try {
|
2022-07-06 17:18:38 +08:00
|
|
|
|
const filter = targetComponent.filter
|
2022-06-25 17:03:03 +08:00
|
|
|
|
// 更新回调函数
|
|
|
|
|
if (updateCallback) {
|
2022-07-06 17:18:38 +08:00
|
|
|
|
updateCallback(newFunctionHandle(res.data, filter))
|
2022-06-25 20:29:42 +08:00
|
|
|
|
} else {
|
|
|
|
|
// eCharts 组件配合 vChart 库更新方式
|
|
|
|
|
if (chartFrame === ChartFrameEnum.ECHARTS) {
|
|
|
|
|
if (vChartRef.value) {
|
2022-07-05 21:44:16 +08:00
|
|
|
|
vChartRef.value.setOption({ dataset: newFunctionHandle(res.data, filter) })
|
2022-06-25 20:29:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-25 17:03:03 +08:00
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error)
|
2022-04-02 11:34:54 +08:00
|
|
|
|
}
|
2022-06-25 17:03:03 +08:00
|
|
|
|
}
|
2022-04-02 11:34:54 +08:00
|
|
|
|
}
|
2022-07-05 21:44:16 +08:00
|
|
|
|
|
2022-06-25 17:03:03 +08:00
|
|
|
|
// 立即调用
|
|
|
|
|
fetchFn()
|
2022-07-08 17:53:52 +08:00
|
|
|
|
|
2022-06-25 17:03:03 +08:00
|
|
|
|
// 开启定时
|
2022-07-08 17:53:52 +08:00
|
|
|
|
const time = targetInterval && targetInterval.value ? targetInterval.value : requestInterval.value
|
|
|
|
|
fetchInterval = setInterval(fetchFn, time * 1000)
|
2022-06-25 17:03:03 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-05 21:44:16 +08:00
|
|
|
|
isPreview() && requestIntervalFn()
|
2022-03-24 14:19:07 +08:00
|
|
|
|
return { vChartRef }
|
2022-03-23 20:41:50 +08:00
|
|
|
|
}
|