Files
li 5f40e4e1b5 feat: 前端全量更新 - 含所有本次需求
- 移除秒合约页面及UI
- 合约页Tab改为Position Held/Transaction Record
- 存款/提款/转账页增加BTC/ETH
- 资产页重构(用户信息+盈亏+多语言)
- 注册页改版(匹配新设计稿)
- 日文全面翻译+首页菜单多语言
- 代理专属注册链接支持(?code=)
- 10个locale文件同步更新
2026-03-30 20:14:20 +08:00

208 lines
5.5 KiB
JavaScript

// import Event from './event.js'
// var pako = window.pako
var socket = {
socket: null, // socket name
realTimeData: null, // 请求实时数据的参数
intervalObj: null, // 定时器的名字
lastRealTimeData: null, // 上一次请求的产品
sendData(historyData, realTimeDatas, history) {
// 储存历史数据
this.historyData = historyData
this.realTimeData = realTimeDatas
var newData = new Array(); //定义一数组
newData = this.realTimeData.split("."); //字符分割
// 如果上一次订阅过产品
if (this.lastRealTimeData) {
var oldData = new Array(); //定义一数组
oldData = this.lastRealTimeData.split("."); //字符分割
// 如果不是订阅历史产品 那么肯定就是切换周期咯 或者 切换产品
// 那么就取消订阅上一次的产品实时数据
if (!history) {
Event.emit('zhezhaoceng', 1)
this.sendWsRequest({
'subs': 'tradenow',
"user_id": "",
"unsub": this.lastRealTimeData,
"id": "BTC"
})
}
// 如果不是订阅历史产品 那么肯定就是切换周期咯 或者切换产品咯
// 那么就订阅一下 这次产品的或者周期的 实时数据
if (!history) {
this.lastRealTimeData = this.realTimeData
if (bb_type == 2) {
// Event.emit('reconnect', newData[3])
} else {
// 请求这一次的历史
this.sendWsRequest({
'subs': 'history',
"sub": this.lastRealTimeData,
"period": newData[3],
'symbol': newData[1],
'size': "200",
"id": "BTC"
})
}
this.sendWsRequest({
'subs': 'tradenow',
"user_id": "",
"sub": this.lastRealTimeData,
"id": "BTC"
})
}
} else {
Event.emit('changeflag', false)
// 如果是第一次订阅,就是说刚进入交易所,
// 先存起来这一次请求的产品 作为历史产品
this.lastRealTimeData = this.realTimeData
// 然后 初始化一下websocket
this.initWs()
}
},
initWs() {
this.socket = new WebSocket('wss://jyshd.pp1008.top:17878')
this.socket.onopen = () => {
var oldData = new Array(); //定义一数组
oldData = this.historyData.req.split("."); //字符分割
// 如果是aptusdt币种,在WebSocket里拿不到历史数据,需要用定时请求拿数据 看kline.js里的 getHistory()方法
if (bb_type == 1) {
this.sendWsRequest({
'subs': 'history',
"sub": this.historyData.req,
"period": oldData[3],
'symbol': oldData[1],
'size': "200",
"id": "BTC"
})
}
this.sendWsRequest({
'subs': 'tradenow',
"user_id": "",
"sub": this.historyData.req,
"id": "BTC"
})
this.sendWsRequest({
'subs': 'depth',
"user_id": "",
"sub": "market." + oldData[1] + ".depth.step0",
"id": "BTC",
})
}
this.socket.onmessage = resp => {
this.message(resp)
}
this.socket.onclose = () => {
this.close()
}
this.socket.onerror = err => {
this.error(err)
}
},
error(err) {
console.log(err, 'depth-socket::error')
},
close() {
// 如果websocket关闭的话,就从新打开一下。
this.initWs()
},
message(resp) {
let this_ = this
try {
let msg = JSON.parse(resp.data)
if (msg.subs == "depth") {
Event.emit('depth', msg.tick)
} else {
// 如果是实时数据触发Event('realTime') 喂数据
if (msg.tick) {
if (msg.ch == this.lastRealTimeData) {
Event.emit('realTime', msg.tick)
Event.emit('newData', msg.tick)
lastminClose = msg.tick.close
}
}
//响应服务器,避免断开连接
if (msg.ping) {
this_.socket.send(JSON.stringify({
pong: msg.ping
}));
this_.hasCheck = true
}
this_.lastRealTimeData = this_.realTimeData
// 历史数据绘制
if (msg.data && Array.isArray(msg.data)) {
// 确保数据不为空并且有效
if (msg.data.length > 0) {
Event.emit('data', msg.data)
Event.emit('newData', msg.data[msg.data.length - 1])
}
}
// 处理API直接返回的单条数据
else if (msg.code && msg.code === 1 && msg.data) {
if (typeof msg.data === 'object') {
// 将单条数据转为数组格式发送
if (msg.data.symbol) {
const klineData = [{
id: Math.floor(Date.now() / 1000),
open: Number(msg.data.open || 0),
high: Number(msg.data.high || 0),
low: Number(msg.data.low || 0),
close: Number(msg.data.close || 0),
vol: 0,
symbol: msg.data.symbol
}];
Event.emit('data', klineData);
Event.emit('newData', klineData[0]);
}
}
}
}
} catch (error) {
// 出错时也要触发信号,避免界面卡住
Event.emit('zhezhaoceng', 2)
Event.emit('changeflag', true)
}
},
checkSendMessage(options) {
// 这里处理websocket 连接不上的问题
var checkTimes = 10
var i = 0
this.intervalObj = setInterval(() => {
i += 1
if (this.socket.readyState === 1) {
this.socket.send(options)
clearInterval(this.intervalObj)
return
}
if (i >= checkTimes) {
clearInterval(this.intervalObj)
// 发送超时也要触发信号,避免界面卡住
Event.emit('zhezhaoceng', 2)
Event.emit('changeflag', true)
}
}, 500)
},
sendWsRequest(options) {
switch (this.socket.readyState) {
case 0:
this.checkSendMessage(JSON.stringify(options))
break
case 1:
this.socket.send(JSON.stringify(options))
break
case 2:
break
case 3:
this.initWs()
break
default:
break
}
}
}
// export default socket