Browse Source

k线不出来修复

master
vee 3 years ago
parent
commit
d8e9ab3061
  1. BIN
      .DS_Store
  2. 173
      components/kline/kline.vue
  3. 58
      components/kline/tradingView/datafeed.js
  4. 130
      components/kline/tradingView/websocket.js
  5. 6
      manifest.json

BIN
.DS_Store

Binary file not shown.

173
components/kline/kline.vue

@ -1,4 +1,4 @@
<template>
<template>
<view>
<view class="tab">
<view class="item" :class="{ select: type === '1min' }" @click="onChangeType('1min')">1min
@ -17,26 +17,26 @@
</view>
<view class="item" :class="{ select: type === '1mon' }" @click="onChangeType('1mon')">1mon
</view>
</view>
<view id="kline" :windowWidth="width" :change:windowWidth="KlineJs.setWidth" :windowHeight="height"
:change:windowHeight="KlineJs.setHeight" :symbol="symbol"
:change:symbol="KlineJs.setSymbol" :language="lang" :change:language="KlineJs.changlanguage" :index_activeCycle="index_activeCycle" :change:index_activeCycle="KlineJs.changindex_activeCycle"></view>
</view>
</template>
<script>
import props from './props.js'
export default {
name: "kline",
mixins: [props],
data() {
return {
</view>
<view id="kline" :windowWidth="width" :change:windowWidth="KlineJs.setWidth" :windowHeight="height"
:change:windowHeight="KlineJs.setHeight" :symbol="symbol"
:change:symbol="KlineJs.setSymbol" :language="language" :change:language="KlineJs.changlanguage" :index_activeCycle="index_activeCycle" :change:index_activeCycle="KlineJs.changindex_activeCycle"></view>
</view>
</template>
<script>
import props from './props.js'
export default {
name: "kline",
mixins: [props],
data() {
return {
type:'1min',
index_activeCycle:1
};
},
created() {
index_activeCycle:1
};
},
created() {
}
,methods: {
onChangeType(type) {
@ -84,81 +84,81 @@
break;
}
},
}
}
</script>
<script module="KlineJs" lang="renderjs">
import {
widget
} from '@/static/charting_library/charting_library.js'
import {
chartConfig
} from './tradingView/chartConfig.js'
//
var widgetObj = null
//
var index_market = 'btcusdt'
export default {
data() {
return {
chartHeight: 0,
}
}
</script>
<script module="KlineJs" lang="renderjs">
import {
widget
} from '@/static/charting_library/charting_library.js'
import {
chartConfig
} from './tradingView/chartConfig.js'
//
var widgetObj = null
//
var index_market = 'btcusdt'
export default {
data() {
return {
chartHeight: 0,
chartWidth: 0,
chartSymbol:'btc',
language:'en'
}
},
//renderjs
mounted() {
//
this.initChart("1")
},
language:'en'
}
},
//renderjs
mounted() {
//
this.initChart("1")
},
methods: {
setSymbol(symbol){
//console.log(symbol)
this.chartSymbol=symbol
},
setWidth(width) {
//console.log(":", width)
this.chartWidth = width
},
setHeight(height) {
//console.log(":", height)
this.chartHeight = 300
},
setWidth(width) {
//console.log(":", width)
this.chartWidth = width
},
setHeight(height) {
//console.log(":", height)
this.chartHeight = 300
},
changindex_activeCycle(index_activeCycle){
this.initChart(index_activeCycle)
},
changlanguage(language){
chartConfig.locale=language
this.language=language
}
,
initChart(index_activeCycle) {
// chartConfig chartConfig.js
// chartConfig
chartConfig.interval = index_activeCycle
// chartConfig
,
initChart(index_activeCycle) {
// chartConfig chartConfig.js
// chartConfig
chartConfig.interval = index_activeCycle
// chartConfig
chartConfig.symbol = this.symbol
chartConfig.resolutions=[1];
//
chartConfig.width = this.chartWidth
chartConfig.resolutions=[1];
//
chartConfig.width = this.chartWidth
chartConfig.height = this.chartHeight
chartConfig.time_frames=[]
// TradingView
chartConfig.time_frames=[]
// TradingView
//console.log(window.TradingView.version())
widgetObj = new widget(chartConfig)
//console.log(11111,widgetObj)
widgetObj._options.interval=5
widgetObj.onChartReady(() => {
// k线 7线30线
widgetObj.chart().createStudy('Moving Average', false, false, [5], null, {
'Plot.linewidth': 2,
'Plot.color': '#2ba7d6'
widgetObj._options.interval=5
widgetObj.onChartReady(() => {
// k线 7线30线
widgetObj.chart().createStudy('Moving Average', false, false, [5], null, {
'Plot.linewidth': 2,
'Plot.color': '#2ba7d6'
})
widgetObj.chart().createStudy('Moving Average', false, false, [10], null, {
'Plot.linewidth': 2,
@ -172,21 +172,19 @@
'Plot.linewidth': 2,
'Plot.color': '#965fc4'
})
this.$nextTick(() => {
widgetObj.chart().resetData()
})
})
},
}
}
this.$nextTick(() => {
widgetObj.chart().resetData()
})
})
},
}
}
</script>
<style lang="scss" scoped>
.tab {
display: flex;
background: #000000;
position: absolute;
width:100%;
.item {
height: 64rpx;
line-height: 64rpx;
@ -204,5 +202,4 @@
}
</style>
</style>

58
components/kline/tradingView/datafeed.js

@ -15,7 +15,7 @@ import {
// 历史数据 第一条数据的 时间撮 因为k线图一次性历史数据只拿一部分,用户吧图往前滑动,就会用这个时间撮去请求更早的 历史数据
var detafeed_historyTime = 0
var detafeed_historyTime = null
// 上一次的 K线周期 切换产品的时候 需要从websock 取消订阅这个
var detafeed_lastResolution = null
// 上一次的产品 切换产品的时候 需要从websock 取消订阅这个
@ -32,7 +32,7 @@ FeedBase.prototype.getSendSymbolName = function(symbolName) {
var name = symbolName.split('/')
return (name[0] + name[1]).toLocaleLowerCase()
}
FeedBase.prototype.resolveSymbol = function(symbolName, onResolve, onError) {
onResolve({
"name": symbolName,
@ -45,10 +45,15 @@ FeedBase.prototype.resolveSymbol = function(symbolName, onResolve, onError) {
"session": "24x7",
"type": "bitcoin",
"volume_precision": 10,
"has_intraday": true,
"intraday_multipliers": ['1', '5', '15', '30', '60', '240', '1440'], // 时间
"has_weekly_and_monthly": false,
"has_no_volume": false,
"has_intraday": true,
"seconds_multipliers":['1', '5', '15', '30', '60', '1D', '1W','1M'],
"intraday_multipliers": ['1', '5', '15', '30', '60', '1D', '1W','1M'],
"has_seconds":true,
"supports_marks": true,
"supports_timescale_marks": true,
"supported_time": true,
"has_weekly_and_monthly": true,
"has_no_volume": true,
"regular_session": "24x7",
})
@ -65,7 +70,7 @@ FeedBase.prototype.resolveSymbol = function(symbolName, onResolve, onError) {
const resolutionFormat = (resolution, name, to) => {
let req = `market.${name}.kline.${resolution}min`;
let minutes = resolution;
console.log(resolution)
if (resolution.includes('D')) {
if (resolution.length < 2) resolution = '1' + resolution;
req = `market.${name}.kline.${parseInt(resolution)}day`;
@ -86,11 +91,12 @@ const resolutionFormat = (resolution, name, to) => {
let from = null;
if (to) {
from = to - 50 * minutes * 60;
from = to - 50 * minutes * 500;
if (resolution.includes('M') || resolution.includes('W')) { // 周线月线控制条数,时间超出火币规定范围, ws报错
from = to - 50 * minutes * 60;
from = to - 50 * minutes * 500;
}
}
}
return {
minutes,
req,
@ -100,11 +106,13 @@ const resolutionFormat = (resolution, name, to) => {
};
FeedBase.prototype.getBars = function(symbolInfo, resolution, periodParams, onHistoryCallback, onErrorCallback) {
console.log("获取历史数据")
console.log("获取历史数据",periodParams)
// 切换产品周期 或者 切换产品 会执行这个函数
let reso = resolutionFormat(resolution, symbolInfo.name, periodParams.to > detafeed_historyTime ? periodParams
.to :
detafeed_historyTime)
let reso = resolutionFormat(resolution, symbolInfo.name, periodParams.to)
// if (resolution.includes('M') || resolution.includes('W')|| resolution.includes('D')) { // 周线月线控制条数,时间超出火币规定范围, ws报错
// reso = resolutionFormat(resolution, symbolInfo.name, periodParams.to)
// }
// 是历史数据
var history = true
/*
@ -127,16 +135,19 @@ FeedBase.prototype.getBars = function(symbolInfo, resolution, periodParams, onHi
第二个参数订阅实时数据
第三个参数 是否是历史数据
*/
socket.sendData({
req: reso.req,
id: "id10",
from: reso.from,
to: reso.to,
socket.sendData({
event: "req",
type: "kline",
channel: [reso.req],
fromDate:reso.from,
toDate:reso.to
}, reso.req, history)
Event.off('data')
Event.on('data', data => {
Event.on('data', data => {
if (data && Array.isArray(data)) {
// 记录这次请求的时间周期
detafeed_lastResolution = resolution
@ -151,13 +162,14 @@ FeedBase.prototype.getBars = function(symbolInfo, resolution, periodParams, onHi
i.volume = i.vol
datas.push(i)
}
} else {
} else {
console.log('进来')
meta = {
noData: true,
noData: 'no_data',
nextTime: detafeed_historyTime
}
}
onHistoryCallback(datas, meta)
onHistoryCallback(datas, {noData:data.length==0})
}
})
}

130
components/kline/tradingView/websocket.js

@ -1,45 +1,47 @@
import
constant
from '@/utils/constant.js'
import {
Event
} from './event.js'
import pakoJs from './pako.js'
var pako = pakoJs
var socket = {
socket: null, // socket name
socket: null, // socket name
id:null,
realTimeData: null, // 请求实时数据的参数
intervalObj: null, // 定时器的名字
lastRealTimeData: null, // 上一次请求的产品
sendData(historyData, realTimeDatas, history) {
// 储存历史数据
this.historyData = historyData
this.historyData = historyData
this.realTimeData = realTimeDatas
// 如果上一次订阅过产品
if (this.lastRealTimeData) {
// 如果不是订阅历史产品 那么肯定就是切换周期咯 或者 切换产品
// 那么就取消订阅上一次的产品实时数据
if (!history) {
console.log('取消订阅' + this.lastRealTimeData)
this.sendWsRequest({
"unsub": this.lastRealTimeData,
"id": "id1"
})
}
// 请求这一次的历史
// 请求这一次的历史
this.historyData.id=this.id
this.sendWsRequest(this.historyData)
console.log(111111,realTimeDatas)
// 如果不是订阅历史产品 那么肯定就是切换周期咯 或者切换产品咯
// 那么就订阅一下 这次产品的或者周期的 实时数据
if (!history) {
console.log('订阅新的' + realTimeDatas)
this.sendWsRequest({
"sub": realTimeDatas,
"id": "id1"
})
//console.log('订阅新的' + realTimeDatas)
//console.log(111111,realTimeDatas)
this.sendWsRequest({
event: 'un_sub',
type: 'kline',
id: this.id,
channel:[this.lastRealTimeData],
})
this.lastRealTimeData = this.realTimeData
}
} else {
} else {
// 如果是第一次订阅,就是说刚进入交易所,
// 先存起来这一次请求的产品 作为历史产品
this.lastRealTimeData = this.realTimeData
@ -48,13 +50,9 @@ var socket = {
}
},
initWs() {
this.socket = new WebSocket('wss://api.huobi.pro/ws')
this.socket = new WebSocket(constant.WSSURL)
this.socket.onopen = () => {
this.sendWsRequest(this.historyData)
this.sendWsRequest({
"sub": this.historyData.req,
"id": "id1"
})
}
this.socket.onmessage = resp => {
this.message(resp)
@ -67,46 +65,52 @@ var socket = {
}
},
error(err) {
console.log(err, 'depth-socket::error')
//console.log(err, 'depth-socket::error')
},
close() {
// 如果websocket关闭的话,就从新打开一下。
this.initWs()
console.log('depth-socket::close')
//console.log('depth-socket::close')
},
message(resp) {
//console.log(resp)
let this_ = this
let reader = new FileReader()
reader.onload = function(e) {
// 对数据进行解压
let msg = JSON.parse(pako.ungzip(reader.result, {
to: 'string'
}))
// console.log(msg)
// 如果是实时数据触发Event('realTime') 喂数据
if (msg.tick) {
Event.emit('realTime', msg.tick)
}
//响应服务器,避免断开连接
if (msg.ping) {
this_.socket.send(JSON.stringify({
pong: msg.ping
}));
this_.hasCheck = true
}
this_.lastRealTimeData = this_.realTimeData
// 如果是历史数据触发Event('data') 绘制数据
if (msg.data && Array.isArray(msg.data)) {
console.log(msg.data)
Event.emit('data', msg.data)
}
}
// //将返回的数据解析为字符串格式
reader.readAsArrayBuffer(resp.data);
resp=JSON.parse(resp.data)
if (resp.channel === 'conn') {
this.id = resp.data
this.historyData.id=this.id
this.sendWsRequest(this.historyData)
}else {
if(resp.event === 'req'){
//Event.emit('data', resp.data)
if (resp.data && Array.isArray(resp.data)) {
Event.emit('data', resp.data)
}
this.sendWsRequest({
event: 'un_sub',
type: 'kline',
id: this.id,
channel:[this.lastRealTimeData],
})
this.sendWsRequest({
event: 'sub',
type: 'kline',
id: this.id,
channel:[this.lastRealTimeData],
})
}else{
Event.emit('realTime', resp.data)
}
}
},
checkSendMessage(options) {
// 这里处理websocket 连接不上的问题
@ -122,7 +126,7 @@ var socket = {
}
if (i >= checkTimes) {
clearInterval(this.intervalObj)
console.log('send post_data_str timeout.')
//console.log('send post_data_str timeout.')
}
}, 500)
},
@ -135,13 +139,13 @@ var socket = {
this.socket.send(JSON.stringify(options))
break
case 2:
console.log('ws关闭状态')
//console.log('ws关闭状态')
break
case 3:
this.initWs()
break
default:
console.log('ws未知错误')
//console.log('ws未知错误')
}
}
}

6
manifest.json

@ -2,8 +2,8 @@
"name" : "Same home",
"appid" : "__UNI__C4028F6",
"description" : "",
"versionName" : "1.0.1",
"versionCode" : 101,
"versionName" : "1.0.3",
"versionCode" : 103,
"transformPx" : false,
/* 5+App */
"app-plus" : {
@ -119,7 +119,7 @@
},
"/market" : {
"ws" : false,
"target" : "https://market.acefinex.com",
"target" : "https://market.gream.ltd",
"changeOrigin" : true,
"secure" : false,
"pathRewrite" : {

Loading…
Cancel
Save