// import socket from "@/utils/websocket.js" let socket_api = 'https://doc.okenx.com:2000' // import io from '@/js_sdk/hyoga-uni-socket_io/uni-socket.io.js'; import io from '@hyoga/uni-socket.io'; let socket = [] import { Api_getQuotationNew } from "@/api/index.js" const state = { bids: [], asks: [], daymarket: {}, // getQuotationStatus: false, usdtList: [], optional: [], marketAll: [], currencyVal: {}, realTimeUpdate: false, suspend: false } const mutations = { set_socket_update_status: (state, status) => { state.realTimeUpdate = status; }, set_currency_val: (state, val) => { state.currencyVal = val; } // SET_CONTRACT: (state, contract) => { // // 合约账户 // state.contractAccount = contract; // }, // SET_BIBI: (state, bibi) => { // // 币币账户 // state.bibiAccount = bibi; // }, // SET_LEGAL: (state, legal) => { // // 法币账户 // state.legalAccount = legal; // }, // SET_GIFTMONEY: (state, GiftMoney) => { // // 赠金账户 // state.GiftMoneyAccount = GiftMoney; // }, // SET_STOCKS_COLOR: (state, code) => { // state.stocksColor = code // }, // SET_TOKEN: (state, token) => { // setToken(token) // state.token = token // } } const nums = (arr) => { const att = arr.map(el => { return el[0] }) const num = Math.max.apply(null, att) arr.forEach(el => { let n = Math.round(el[0] / num * 10000) / 100 el[2] = n > 100 ? 100 : n; }) return arr } const actions = { openSocket({ commit, state }, data) { return false console.log('打开socket' , ) const socketUrl = 'https://doc.okenx.com:2000'; socket[socket.length - 1] = io(socketUrl, { query: {}, transports: ['websocket', 'polling'], timeout: 5000, }); socket[socket.length - 1].on(data.type, (msg) => { // console.log('socket = ' , socket , msg) // if(!socket){ // try{ // socket.disconnect() // socket.closeSocket() // }catch{} // socket = null; // return false // } if (state.currencyVal.legal_id == msg.legal_id && state.currencyVal.currency_id == msg .currency_id) { if (msg.type == 'market_depth') { var buyIn = msg.bids.slice(0, 7); state.bids = nums(buyIn); var out = msg.asks.slice(0, 7); // state.bids = msg.bids state.asks = nums(out); } if (msg.type == 'daymarket') { state.daymarket = msg; } } // // 主动向服务器发送数据 // socket.emit('send_data', { // time: +new Date(), // }); }); }, closeSocket({ commit, state }, data) { console.log('关闭socket' , socket) try{ socket.disconnect() // socket.forEach(el => { // el.disconnect() // }); // socket // socket.close() }catch{} socket = null; }, getQuotationNew({ commit, state }, data) { state.suspend = data ? data?.suspend : false const rq = () => { Api_getQuotationNew().then(res => { if (res && res.length > 0) { let newArr = res.find((item) => item.name == 'USDT').quotation || []; if (!state.currencyVal || !state.currencyVal?.currency_id) { state.currencyVal = newArr[0] console.log('currencyVal = ', state.currencyVal) } // 首页推荐 if (newArr && newArr.length > 0) { const nums = newArr.length > 3 ? 3 : newArr.length; state.usdtList = newArr.slice(0, nums); } else { state.usdtList = [] } // 自选 state.optional = newArr.slice(0, 6) // 全部 state.marketAll = newArr } }).catch(err => { }).finally(() => { setTimeout(() => { if (!state.suspend) { rq() } }, 1000) }) } if (!state.suspend) { rq() } }, } export default { namespaced: true, state, mutations, actions }