currency-market.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import {
  2. Api_getQuotationNew
  3. } from "@/api/index.js"
  4. import store from '@/store'
  5. let currencySuspend = false // 是否终止循环
  6. const suspendObj = {}
  7. export const setCurrencySuspend = (key = 'defaultKey') => {
  8. suspendObj[`${key}_suspend`] = true
  9. }
  10. export const getCurrencyMarket = (key = 'defaultKey', data = {}) => {
  11. if (!data.onceRefresh) {
  12. suspendObj[`${key}_suspend`] = false
  13. };
  14. const rq = () => {
  15. Api_getQuotationNew().then(res => {
  16. if (res && res.length > 0) {
  17. let newArr = res.find((item) => item.name == 'USDT').quotation || [];
  18. if (!store.getters.currencyVal || !store.getters.currencyVal?.currency_id) {
  19. // state.currencyVal = newArr[0];
  20. store.commit('websocket/set_currency_val', newArr[0])
  21. }
  22. // 首页推荐
  23. // 数据隔离 防止数据更新频繁,造成卡顿
  24. switch (key) {
  25. case 'home':
  26. /**
  27. * 首页
  28. */
  29. let usdt = []
  30. if (newArr && newArr.length > 0) {
  31. const nums = newArr.length > 3 ? 3 : newArr.length;
  32. usdt = newArr.slice(0, nums);
  33. } else {
  34. usdt = []
  35. }
  36. // 首页推荐
  37. store.commit('websocket/set_usdt_ist', usdt)
  38. // 自选
  39. store.commit('websocket/set_optional', newArr.slice(0, 6) || []);
  40. // 全部
  41. store.commit('websocket/set_marketAll', newArr || [])
  42. break;
  43. case 'contract':
  44. case 'contractDrawer':
  45. /**
  46. * 合约 - 合约抽屉(左侧)
  47. */
  48. // 自选
  49. store.commit('websocket/set_contract_optional', newArr.slice(0, 6) || []);
  50. // 全部
  51. store.commit('websocket/set_contract_marketAll', newArr || [])
  52. break;
  53. }
  54. }
  55. }).catch(err => {
  56. if (data.onceRefresh) {
  57. // 一次性调用,即检测更新,防止接口挂
  58. rq()
  59. }
  60. }).finally(() => {
  61. setTimeout(() => {
  62. if (!data.onceRefresh) {
  63. // 不是一次性调用
  64. // 没有终止循环 , 即循环调用更新
  65. suspendObj[`${key}_suspend`] || rq()
  66. }
  67. }, 3000);
  68. })
  69. }
  70. rq()
  71. }
  72. // export default class currencyMarket {
  73. // ​
  74. // constructor() {
  75. // ​
  76. // this.socket = null;​
  77. // this.typeName = null;​
  78. // this.onopen = (() => {
  79. // ​
  80. // console.log(`open ${this.typeName} websocket`);​​
  81. // })​ this.onmessage = (e => {
  82. // ​ //处理各种推送消息
  83. // ​​})​ this.onerror = (e => {
  84. // ​
  85. // console.warn(`${this.typeName}websocket服务已断开,正在重连`);​
  86. // console.error("error " + e);​​
  87. // })​ this.onclose = (() => {
  88. // ​
  89. // console.warn("close websocket");​
  90. // console.warn(`${this.typeName}websocket服务已断开,正在重连`);​​
  91. // })​ this.initWs();​
  92. // }
  93. // ​ initWs() {
  94. // ​
  95. // console.warn(`初始化${this.typeName}websocket连接`);​
  96. // this.socket = new WebSocket(this.url); // 创建连接并注册响应函数
  97. // ​
  98. // this.socket.onopen = () => {
  99. // ​
  100. // this.onopen();​
  101. // };​
  102. // this.socket.onclose = () => {
  103. // ​
  104. // this.onclose();​
  105. // this.socket = null; // 清理
  106. // ​
  107. // stopWebsocket();​
  108. // };​
  109. // this.socket.onerror = (e) => {
  110. // ​
  111. // this.onerror(e);​
  112. // stopWebsocket();​
  113. // }​
  114. // this.socket.onmessage = (e) => {
  115. // ​
  116. // this.onmessage(e);​
  117. // };​
  118. // }
  119. // ​ reConnect(that) {
  120. // ​
  121. // if (that.isReconnect) return;​
  122. // that.isReconnect = true;​ //没连接上会一直重连,设置延迟避免请求过多
  123. // ​
  124. // setTimeout(() => {
  125. // ​
  126. // that.initWs();​
  127. // that.isReconnect = false;​
  128. // }, 2000);​
  129. // }
  130. // }