index.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { ref, computed, watch } from 'vue'
  2. //import { timerInterceptor } from '@/utils/timer'
  3. import { queryTSData } from '@/services/api/quote'
  4. import { getQuoteDayInfoByCode } from '@/business/common'
  5. import { useDataset } from './dataset'
  6. import { useOptions } from './options'
  7. import moment from 'moment';
  8. export function useTimelineChart(goodscode: string) {
  9. const { dataset, handleData, clearData, calcIndicator } = useDataset();
  10. const { options, initOptions, updateOptions } = useOptions(dataset);
  11. const loading = ref(false);
  12. const isEmpty = ref(false);
  13. const dataIndex = ref(-1); // 当前数据索引值
  14. const quote = getQuoteDayInfoByCode(goodscode); // 实时行情
  15. // 当前选中的数据项
  16. const selectedItem = computed(() => {
  17. const { close, ma5 } = dataset.timeline.source;
  18. return {
  19. close: close[dataIndex.value] ?? '--',
  20. ma5: ma5[dataIndex.value] ?? '--'
  21. }
  22. })
  23. /**
  24. * 初始化数据
  25. */
  26. const initData = () => {
  27. clearData();
  28. dataIndex.value = -1;
  29. isEmpty.value = true;
  30. loading.value = true;
  31. // 获取历史行情
  32. queryTSData({
  33. data: {
  34. goodscode
  35. },
  36. success: (res) => {
  37. const { historyDatas } = res.data;
  38. if (historyDatas.length) {
  39. dataIndex.value = historyDatas.length - 1;
  40. isEmpty.value = false;
  41. handleData(res.data);
  42. }
  43. },
  44. complete: () => {
  45. loading.value = false;
  46. }
  47. })
  48. }
  49. /**
  50. * 更新图表数据
  51. */
  52. const updateChart = () => {
  53. if (quote.value) {
  54. const { last, lasttime } = quote.value;
  55. const { close, ma5 } = dataset.timeline.source;
  56. const lastIndex = close.length - 1; // 历史数据最后索引位置
  57. const cycleMilliseconds = 60 * 1000; // 一分钟毫秒数
  58. const oldTime = lastIndex === -1 ? moment(lasttime) : moment(dataset.rawDate[lastIndex]); // 历史行情最后时间
  59. const diffTime = moment(lasttime).valueOf() - oldTime.valueOf(); // 计算时间差
  60. if (diffTime > cycleMilliseconds * 2) {
  61. // 时间间隔超过两个周期,重新请求历史数据
  62. //timerInterceptor.debounce(() => initData(), 1000, 'updateChart');
  63. } else {
  64. // 判断时间差是否大于周期时间
  65. if (lastIndex === -1 || diffTime > cycleMilliseconds) {
  66. oldTime.add(cycleMilliseconds, 'ms');
  67. const lastDate = oldTime.format('YYYY-MM-DD HH:mm:ss');
  68. // 新增分时数据
  69. dataset.rawDate.push(lastDate);
  70. close.push(last);
  71. ma5.push('-')
  72. } else {
  73. close[lastIndex] = last; // 更新最后一条记录的收盘价
  74. }
  75. // 更新各种指标
  76. calcIndicator(lastIndex === -1 ? 0 : lastIndex);
  77. updateOptions();
  78. }
  79. }
  80. }
  81. // 监听行情推送
  82. watch(() => quote.value?.last, () => {
  83. if (!loading.value && !isEmpty.value) {
  84. updateChart();
  85. }
  86. })
  87. initData();
  88. return {
  89. loading,
  90. isEmpty,
  91. dataIndex,
  92. options,
  93. selectedItem,
  94. initData,
  95. initOptions,
  96. }
  97. }