useChartDataFetch.hook.ts 3.1 KB

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