Browse Source

优化

master
vee 3 years ago
parent
commit
13d70ef9cf
  1. 4
      pages/home/index.vue
  2. 33
      pages/markets/index.vue
  3. 149
      pages/markets/kLine.vue
  4. 124
      pages/markets/trade.vue
  5. 168
      utils/websocket.js

4
pages/home/index.vue

@ -1,7 +1,3 @@
原始文件
永久链接
Blame
文件历史
<template>
<view class="main">
<!-- nav -->

33
pages/markets/index.vue

@ -32,7 +32,6 @@
BASE_URL: '',
symbolList: [],
websock: null,
websockId: null
}
},
onLoad() {
@ -75,39 +74,15 @@
},
subWebSocket() {
const data = {
event: 'sub',
type: 'pairsgroup',
id: this.websockId,
channel: ["market.pairsgroup"]
};
this.websock.webSocketSendMsg(data)
console.log("websocket发送", data);
},
unsubWebSocket() {
const data = {
event: 'un_sub',
type: 'pairsgroup',
id: this.websockId,
channel: ["market.pairsgroup"]
};
this.websock.webSocketSendMsg(data)
},
initWebSocket() {
this.websock = new this.$websocket(this.$constant.WSSURL) // xxx URL
this.websock = new this.$websocket(this.$constant.WSSURL)
var that = this
this.websock.getWebSocketMsg(data => {
console.log(data, 1111)
if (data.channel === 'conn') {
that.websockId = data.data
that.subWebSocket();
that.websock.setId(data.data);
that.websock.subPairsgroup();
} else if (data.channel === 'market.pairsgroup') {
that.symbolList = data.data.USDT;
}

149
pages/markets/kLine.vue

@ -114,7 +114,6 @@
usdRate: 0
},
bboList: {},
webSocketChannel: {},
sum: {}
};
@ -176,10 +175,10 @@
},
onChangeType(type) {
this.unsubkline();
this.type = type
this.subKHistory()
this.subkline();
this.websock.unSubKline(this.type,this.symbol.symbol);
this.type = type
this.websock.subKHistory(this.type,this.symbol.symbol);
this.websock.subKline(this.type,this.symbol.symbol);
},
formateDate(time) {
@ -247,25 +246,22 @@
if (data.channel === 'conn') {
that.websockId = data.data
that.subbbo()
that.submarketDetail()
that.subKHistory()
that.subkline();
that.websock.setId(data.data);
that.websock.subBbo(that.symbol.symbol)
that.websock.subDetail(that.symbol.symbol)
that.websock.subKHistory(that.type,that.symbol.symbol)
that.websock.subKline(that.type,that.symbol.symbol)
} else if (data.channel === 'market.pairsgroup') {
that.symbolList = data.data.USDT;
} else if (data.channel === that.webSocketChannel.bbo) {
that.bboList = data.data;
} else if (data.channel === that.webSocketChannel.bbo) {
} else if (data.channel === 'market.' + that.symbol.symbol + '.bbo') {
that.bboList = data.data;
that.statisticsSum();
} else if (data.channel === that.webSocketChannel.trade) {
} else if (data.channel === 'market.' + that.symbol.symbol + '.trade') {
that.tradeList = data.data;
} else if (data.channel === that.webSocketChannel.detail) {
} else if (data.channel === 'market.' + that.symbol.symbol + '.detail') {
that.marketDetail = data.data;
} else if (data.channel === that.webSocketChannel.kline) {
} else if (data.channel === 'market.' + that.symbol.symbol + ".kline." + that.type) {
if (data.event === 'req') {
that.kdata = that.splitData(data.data)
} else {
@ -275,74 +271,7 @@
}
});
},
//
subKHistory() {
var toDate = parseInt(new Date().getTime() / 1000);
var num = 100000;
var fromDate = parseInt(new Date().getTime() / 1000);
switch (this.type) {
case '1min':
fromDate = fromDate - num;
break;
case '5min':
fromDate = fromDate - (5 * num);
break;
case '15min':
fromDate = fromDate - (15 * num);
break;
case '30min':
fromDate = fromDate - (30 * num);
break;
case '60min':
fromDate = fromDate - (60 * num);
break;
case '1day':
fromDate = fromDate - (60 * 24 * num);
break;
case '1week':
fromDate = fromDate - (60 * 24 * 7 * num);
break;
case '1mon':
fromDate = fromDate - (60 * 24 * 30 * num);
break;
}
this.webSocketChannel.kline = 'market.' + this.symbol.symbol + ".kline." + this.type;
const data = {
event: 'req',
type: 'kline',
id: this.websockId,
channel: [this.webSocketChannel.kline],
fromDate: parseInt(fromDate),
toDate: toDate
};
this.websock.webSocketSendMsg(data)
},
subkline() {
this.webSocketChannel.kline = 'market.' + this.symbol.symbol + ".kline." + this.type;
const data = {
event: 'sub',
type: 'kline',
id: this.websockId,
channel: [this.webSocketChannel.kline],
};
this.websock.webSocketSendMsg(data)
},
unsubkline() {
this.webSocketChannel.kline = 'market.' + this.symbol.symbol + ".kline." + this.type;
const data = {
event: 'un_sub',
type: 'kline',
id: this.websockId,
channel: [this.webSocketChannel.kline],
};
this.websock.webSocketSendMsg(data)
},
upkline(data) {
if (data.id === this.kdata.values[this.kdata.values.length - 1][5]) {
@ -358,52 +287,6 @@
this.kdata.categoryData.push(this.formateDate(data.kTime));
}
},
subbbo() {
this.webSocketChannel.bbo = 'market.' + this.symbol.symbol + ".bbo";
this.subWebSocket('bbo', [this.webSocketChannel.bbo])
},
unsubbbo() {
this.unsubWebSocket('bbo', [this.webSocketChannel.bbo])
},
subtrade() {
this.webSocketChannel.trade = 'market.' + this.symbol.symbol + ".trade";
this.subWebSocket('trade', [this.webSocketChannel.trade])
},
unsubtrade() {
this.unsubWebSocket('trade', [this.webSocketChannel.trade])
},
submarketDetail() {
this.webSocketChannel.detail = 'market.' + this.symbol.symbol + ".detail";
this.subWebSocket('detail', [this.webSocketChannel.detail])
},
unsubmarketDetail() {
this.unsubWebSocket('detail', [this.webSocketChannel.detail])
},
subWebSocket(type, channel) {
const data = {
event: 'sub',
type: type,
id: this.websockId,
channel: channel
};
this.websock.webSocketSendMsg(data)
console.log("websocket发送", data);
},
unsubWebSocket(type, channel) {
const data = {
event: 'un_sub',
type: type,
id: this.websockId,
channel: channel
};
this.websock.webSocketSendMsg(data)
},
splitData(rawData) {
let categoryData = [];
let values = [];
@ -419,7 +302,7 @@
categoryData: categoryData,
values: values,
volumes: volumes,
webSocketChannel: this.webSocketChannel.kline
webSocketChannel: 'market.' + this.symbol.symbol + ".kline." + this.type
};

124
pages/markets/trade.vue

@ -61,8 +61,7 @@
usdRate: 0
},
bboList: {},
type:'buy',
webSocketChannel: {},
type:'buy',
sum:{}
};
},
@ -121,15 +120,7 @@
tradeList.then(res => {
this.tradeList = res
})
.catch(e => {
console.log(e)
uni.showToast({
title: e,
icon: 'none',
duration: 2500
})
})
});
},
getBboList() {
const bboList = this.$api.bboList({
@ -138,15 +129,7 @@
bboList.then(res => {
this.bboList = res
this.statisticsSum()
})
.catch(e => {
console.log(e)
uni.showToast({
title: e,
icon: 'none',
duration: 2500
})
})
});
},
getMarketDetail() {
const marketDetail = this.$api.marketDetail({
@ -156,14 +139,6 @@
this.marketDetail = res
})
.catch(e => {
console.log(e)
uni.showToast({
title: e,
icon: 'none',
duration: 2500
})
})
},
getSymbolGroup() {
const symbolGroup = this.$api.symbolGroup({
@ -180,15 +155,7 @@
}
})
.catch(e => {
console.log(e)
uni.showToast({
title: e,
icon: 'none',
duration: 2500
})
})
});
@ -203,80 +170,25 @@
this.websock.getWebSocketMsg(data => {
if (data.channel === 'conn') {
that.websockId = data.data
that.subpairsgroup()
that.subbbo()
that.submarketDetail()
that.websock.setId(data.data);
that.websock.subPairsgroup();
that.websock.subBbo(that.symbol.symbol)
that.websock.subDetail(that.symbol.symbol)
} else if (data.channel === 'market.pairsgroup') {
that.symbolList = data.data.USDT;
} else if (data.channel === that.webSocketChannel.bbo) {
} else if (data.channel === 'market.' + that.symbol.symbol + '.bbo') {
that.bboList = data.data;
} else if (data.channel === that.webSocketChannel.bbo) {
that.bboList = data.data;
that.statisticsSum();
} else if (data.channel === that.webSocketChannel.trade) {
} else if (data.channel === 'market.' + that.symbol.symbol + '.trade') {
that.tradeList = data.data;
} else if (data.channel === that.webSocketChannel.detail) {
} else if (data.channel === 'market.' + that.symbol.symbol + '.detail') {
that.marketDetail = data.data;
}else{
console.log('未知',data.data)
}
});
},
//
subpairsgroup() {
this.subWebSocket('pairsgroup', ['market.pairsgroup'])
},
//
unsubpairsgroup() {
this.unsubWebSocket('pairsgroup', ['market.pairsgroup'])
},
subbbo() {
this.webSocketChannel.bbo = 'market.' + this.symbol.symbol + ".bbo";
this.subWebSocket('bbo', [this.webSocketChannel.bbo])
},
unsubbbo() {
this.unsubWebSocket('bbo', [this.webSocketChannel.bbo])
},
subtrade() {
this.webSocketChannel.trade = 'market.' + this.symbol.symbol + ".trade";
this.subWebSocket('trade', [this.webSocketChannel.trade])
},
unsubtrade() {
this.unsubWebSocket('trade', [this.webSocketChannel.trade])
},
submarketDetail() {
this.webSocketChannel.detail = 'market.' + this.symbol.symbol + ".detail";
this.subWebSocket('detail', [this.webSocketChannel.detail])
},
unsubmarketDetail() {
this.unsubWebSocket('detail', [this.webSocketChannel.detail])
},
subWebSocket(type, channel) {
const data = {
event: 'sub',
type: type,
id: this.websockId,
channel: channel
};
this.websock.webSocketSendMsg(data)
console.log("websocket发送", data);
},
unsubWebSocket(type, channel) {
const data = {
event: 'un_sub',
type: type,
id: this.websockId,
channel: channel
};
this.websock.webSocketSendMsg(data)
},
depthChange(e) {
console.log(e);
},
@ -299,14 +211,14 @@
switchTo(item) {
this.popupShow = false;
this.unsubbbo()
this.unsubmarketDetail()
this.websock.unSubBbo(this.symbol.symbol)
this.websock.unSubTrade(this.symbol.symbol)
this.symbol = item;
uni.setStorageSync('symbol', this.symbol);
this.getMarketDetail()
this.getBboList();
this.subbbo()
this.submarketDetail()
this.websock.subBbo(this.symbol.symbol)
this.websock.subDetail(this.symbol.symbol)
}
},

168
utils/websocket.js

@ -1,23 +1,26 @@
class WebSocketClass {
constructor(url) {
this.lockReconnect = false; // 是否开始重连
this.wsUrl = ""; // ws 地址
this.wsUrl = ''; // ws 地址
this.globalCallback = null; // 回调方法
this.userClose = false; // 是否主动关闭
this.createWebSocket(url);
this.id = null;
}
setId(id) {
this.id = id
}
createWebSocket(url) {
// #ifdef H5
if (typeof(WebSocket) === 'undefined') {
this.writeToScreen("您的浏览器不支持WebSocket,无法获取数据");
this.writeToScreen('您的浏览器不支持WebSocket,无法获取数据');
return false
}
// #endif
// #ifdef APP-PLUS
if (typeof(uni.connectSocket) === 'undefined') {
this.writeToScreen("您的浏览器不支持WebSocket,无法获取数据");
this.writeToScreen('您的浏览器不支持WebSocket,无法获取数据');
return false
}
// #endif
@ -36,7 +39,7 @@ class WebSocketClass {
this.ws = uni.connectSocket({
url: this.wsUrl,
success(data) {
console.log("websocket连接成功");
console.log('websocket连接成功');
that.initEventHandle();
},
@ -55,7 +58,7 @@ class WebSocketClass {
// #ifdef H5
this.ws.onopen = (event) => {
console.log("WebSocket连接打开");
console.log('WebSocket连接打开');
};
// #endif
@ -114,7 +117,7 @@ class WebSocketClass {
// #ifdef H5
this.ws.onmessage = (event) => {
this.globalCallback(JSON.parse(event.data))
};
// #endif
@ -138,23 +141,23 @@ class WebSocketClass {
}
// 发送信息方法
webSocketSendMsg(msg) {
// #ifdef H5
this.ws && this.ws.send(JSON.stringify(msg));
// #endif
// #ifdef APP-PLUS
this.ws && this.ws.send({
data: JSON.stringify(msg),
success() {
console.log("消息发送成功");
},
fail(err) {
console.log("关闭失败", err)
}
});
webSocketSendMsg(msg) {
// #ifdef H5
this.ws && this.ws.send(JSON.stringify(msg));
// #endif
// #ifdef APP-PLUS
this.ws && this.ws.send({
data: JSON.stringify(msg),
success() {
console.log('消息发送成功');
},
fail(err) {
console.log('关闭失败', err)
}
});
// #endif
}
// 获取ws返回的数据方法
@ -165,27 +168,116 @@ class WebSocketClass {
// 关闭ws方法
closeSocket() {
if (this.ws) {
this.userClose = true;
// #ifdef H5
this.ws && this.ws.close();
// #endif
// #ifdef APP-PLUS
this.ws.close({
success(res) {
console.log("关闭成功", res)
},
fail(err) {
console.log("关闭失败", err)
}
});
this.userClose = true;
// #ifdef H5
this.ws && this.ws.close();
// #endif
// #ifdef APP-PLUS
this.ws.close({
success(res) {
console.log('关闭成功', res)
},
fail(err) {
console.log('关闭失败', err)
}
});
// #endif
}
}
writeToScreen(massage) {
console.log(massage);
}
subPairsgroup() {
this.publicSend('sub', 'pairsgroup', ['market.pairsgroup'], null)
}
unSubPairsgroup() {
this.publicSend('un_sub', 'pairsgroup', ['market.pairsgroup'], null)
}
subBbo(symbol) {
this.publicSend('sub', 'bbo', ['market.' + symbol + '.bbo'], null)
}
unSubBbo(symbol) {
this.publicSend('un_sub', 'bbo', ['market.' + symbol + '.bbo'], null)
}
subTrade(symbol) {
this.publicSend('sub', 'trade', ['market.' + symbol + '.trade'], null)
}
unSubTrade(symbol) {
this.publicSend('un_sub', 'trade', ['market.' + symbol + '.trade'], null)
}
subDetail(symbol) {
this.publicSend('sub', 'detail', ['market.' + symbol + '.detail'], null)
}
unSubDetail(symbol) {
this.publicSend('un_sub', 'detail', ['market.' + symbol + '.detail'], null)
}
subKHistory(timeType, symbol) {
var toDate = parseInt(new Date().getTime() / 1000);
var num = 100000;
var fromDate = parseInt(new Date().getTime() / 1000);
switch (timeType) {
case '1min':
fromDate = fromDate - num;
break;
case '5min':
fromDate = fromDate - (5 * num);
break;
case '15min':
fromDate = fromDate - (15 * num);
break;
case '30min':
fromDate = fromDate - (30 * num);
break;
case '60min':
fromDate = fromDate - (60 * num);
break;
case '1day':
fromDate = fromDate - (60 * 24 * num);
break;
case '1week':
fromDate = fromDate - (60 * 24 * 7 * num);
break;
case '1mon':
fromDate = fromDate - (60 * 24 * 30 * num);
break;
}
this.publicSend('req', 'kline',
['market.' + symbol + ".kline." + timeType], {
fromDate: parseInt(fromDate),
toDate: toDate
})
}
subKline(timeType, symbol) {
this.publicSend('sub', 'kline',
['market.' + symbol + ".kline." + timeType], null)
}
unSubKline(timeType, symbol) {
this.publicSend('un_sub', 'kline',
['market.' + symbol + ".kline." + timeType], null)
}
publicSend(event, type, channel, date) {
var data = {
event: event,
type: type,
id: this.id,
channel: channel
};
if (date) {
data.fromDate = date.fromDate
data.toDate = date.toDate
}
this.webSocketSendMsg(data)
}
}
export default WebSocketClass;

Loading…
Cancel
Save