import { Api_getQuotationNew } from "@/api/index.js" import store from '@/store' let currencySuspend = false // 是否终止循环 const suspendObj = {} export const setCurrencySuspend = (key = 'defaultKey') => { suspendObj[`${key}_suspend`] = true } export const getCurrencyMarket = (key = 'defaultKey', data = {}) => { if (!data.onceRefresh) { suspendObj[`${key}_suspend`] = false }; const rq = () => { Api_getQuotationNew().then(res => { if (res && res.length > 0) { let newArr = res.find((item) => item.name == 'USDT').quotation || []; if (!store.getters.currencyVal || !store.getters.currencyVal?.currency_id) { // state.currencyVal = newArr[0]; store.commit('websocket/set_currency_val', newArr[0]) } // 首页推荐 // 数据隔离 防止数据更新频繁,造成卡顿 switch (key) { case 'home': /** * 首页 */ let usdt = [] if (newArr && newArr.length > 0) { const nums = newArr.length > 3 ? 3 : newArr.length; usdt = newArr.slice(0, nums); } else { usdt = [] } // 首页推荐 store.commit('websocket/set_usdt_ist', usdt) // 自选 store.commit('websocket/set_optional', newArr.slice(0, 6) || []); // 全部 store.commit('websocket/set_marketAll', newArr || []) break; case 'contract': case 'contractDrawer': /** * 合约 - 合约抽屉(左侧) */ // 自选 store.commit('websocket/set_contract_optional', newArr.slice(0, 6) || []); // 全部 store.commit('websocket/set_contract_marketAll', newArr || []) break; } } }).catch(err => { if (data.onceRefresh) { // 一次性调用,即检测更新,防止接口挂 rq() } }).finally(() => { setTimeout(() => { if (!data.onceRefresh) { // 不是一次性调用 // 没有终止循环 , 即循环调用更新 suspendObj[`${key}_suspend`] || rq() } }, 3000); }) } rq() } // export default class currencyMarket { // ​ // constructor() { // ​ // this.socket = null;​ // this.typeName = null;​ // this.onopen = (() => { // ​ // console.log(`open ${this.typeName} websocket`);​​ // })​ this.onmessage = (e => { // ​ //处理各种推送消息 // ​​})​ this.onerror = (e => { // ​ // console.warn(`${this.typeName}websocket服务已断开,正在重连`);​ // console.error("error " + e);​​ // })​ this.onclose = (() => { // ​ // console.warn("close websocket");​ // console.warn(`${this.typeName}websocket服务已断开,正在重连`);​​ // })​ this.initWs();​ // } // ​ initWs() { // ​ // console.warn(`初始化${this.typeName}websocket连接`);​ // this.socket = new WebSocket(this.url); // 创建连接并注册响应函数 // ​ // this.socket.onopen = () => { // ​ // this.onopen();​ // };​ // this.socket.onclose = () => { // ​ // this.onclose();​ // this.socket = null; // 清理 // ​ // stopWebsocket();​ // };​ // this.socket.onerror = (e) => { // ​ // this.onerror(e);​ // stopWebsocket();​ // }​ // this.socket.onmessage = (e) => { // ​ // this.onmessage(e);​ // };​ // } // ​ reConnect(that) { // ​ // if (that.isReconnect) return;​ // that.isReconnect = true;​ //没连接上会一直重连,设置延迟避免请求过多 // ​ // setTimeout(() => { // ​ // that.initWs();​ // that.isReconnect = false;​ // }, 2000);​ // } // }