import socket from "@/utils/websocket.js" let socket_api = 'https://doc.okenx.com:2000' import { Api_getQuotationNew } from "@/api/index.js" const state = { bids: [], asks: [], daymarket: {}, // getQuotationStatus: false, usdtList: [], optional:[], marketAll:[], currencyVal:{} } const mutations = { 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) { var sk = socket(socket_api); sk.on(data.type, function(msg) { if (state.currencyVal.legal_id == msg.legal_id && state.currencyVal.currency_id == msg.currency_id) { //btc 匹配 // var buyIn = JSON.parse(msg.bids); // var out = JSON.parse(msg.asks).reverse(); 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; } } }) }, getQuotationNew({ commit, state }, data) { 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(() => { rq() // if (state.getQuotationStatus) { // } }, 1000) }) } rq() }, } export default { namespaced: true, state, mutations, actions }