useChartDataFetch.hook.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { ref, toRefs, toRaw } from 'vue'
  2. import type VChart from 'vue-echarts'
  3. import { customizeHttp } from '@/api/http'
  4. import { useChartDataPondFetch } from '@/hooks/'
  5. import { CreateComponentType, ChartFrameEnum } from '@/packages/index.d'
  6. import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
  7. import { RequestDataTypeEnum } from '@/enums/httpEnum'
  8. import { isPreview, newFunctionHandle, intervalUnitHandle } from '@/utils'
  9. // 获取类型
  10. type ChartEditStoreType = typeof useChartEditStore
  11. /**
  12. * setdata 数据监听与更改
  13. * @param targetComponent
  14. * @param useChartEditStore 若直接引会报错,只能动态传递
  15. * @param updateCallback 自定义更新函数
  16. */
  17. export const useChartDataFetch = (
  18. targetComponent: CreateComponentType,
  19. useChartEditStore: ChartEditStoreType,
  20. updateCallback?: (...args: any) => any
  21. ) => {
  22. const vChartRef = ref<typeof VChart | null>(null)
  23. let fetchInterval: any = 0
  24. // 数据池
  25. const { addGlobalDataInterface } = useChartDataPondFetch()
  26. const { requestDataPondId } = toRefs(targetComponent.request)
  27. // 组件类型
  28. const { chartFrame } = targetComponent.chartConfig
  29. // eCharts 组件配合 vChart 库更新方式
  30. const echartsUpdateHandle = (dataset: any) => {
  31. if (chartFrame === ChartFrameEnum.ECHARTS) {
  32. if (vChartRef.value) {
  33. vChartRef.value.setOption({ dataset: dataset })
  34. }
  35. }
  36. }
  37. const requestIntervalFn = () => {
  38. const chartEditStore = useChartEditStore()
  39. // 全局数据
  40. const {
  41. requestOriginUrl,
  42. requestIntervalUnit: globalUnit,
  43. requestInterval: globalRequestInterval
  44. } = toRefs(chartEditStore.getRequestGlobalConfig)
  45. // 目标组件
  46. const {
  47. requestDataType,
  48. requestUrl,
  49. requestIntervalUnit: targetUnit,
  50. requestInterval: targetInterval
  51. } = toRefs(targetComponent.request)
  52. // 非请求类型
  53. if (requestDataType.value !== RequestDataTypeEnum.AJAX) return
  54. try {
  55. // 处理地址
  56. // @ts-ignore
  57. if (requestUrl?.value) {
  58. // requestOriginUrl 允许为空
  59. const completePath = requestOriginUrl && requestOriginUrl.value + requestUrl.value
  60. if (!completePath) return
  61. clearInterval(fetchInterval)
  62. const fetchFn = async () => {
  63. const res = await customizeHttp(toRaw(targetComponent.request), toRaw(chartEditStore.getRequestGlobalConfig))
  64. if (res) {
  65. try {
  66. const filter = targetComponent.filter
  67. echartsUpdateHandle(newFunctionHandle(res?.data, res, filter))
  68. // 更新回调函数
  69. if (updateCallback) {
  70. updateCallback(newFunctionHandle(res?.data, res, filter))
  71. }
  72. } catch (error) {
  73. console.error(error)
  74. }
  75. }
  76. }
  77. // 立即调用
  78. fetchFn()
  79. // 定时时间
  80. const time = targetInterval && targetInterval.value ? targetInterval.value : globalRequestInterval.value
  81. // 单位
  82. const unit = targetInterval && targetInterval.value ? targetUnit.value : globalUnit.value
  83. // 开启轮询
  84. if (time) fetchInterval = setInterval(fetchFn, intervalUnitHandle(time, unit))
  85. }
  86. // eslint-disable-next-line no-empty
  87. } catch (error) {
  88. console.log(error)
  89. }
  90. }
  91. if (isPreview()) {
  92. // 判断是否有数据池对应 id
  93. requestDataPondId
  94. ? addGlobalDataInterface(targetComponent, useChartEditStore, updateCallback || echartsUpdateHandle)
  95. : requestIntervalFn()
  96. }
  97. return { vChartRef }
  98. }