diff --git a/pages/home/home.vue b/pages/home/home.vue index 3193d4e..5f75c8c 100644 --- a/pages/home/home.vue +++ b/pages/home/home.vue @@ -256,6 +256,9 @@ export default { type: '' }, + // 当前TCP使用的股票代码 + currentStockCodes: ['SH.000001', 'SH.000002', 'SH.000003'], + // 我的自选TCP股票数据存储 myStocksTcpData: { count: 0, @@ -341,10 +344,8 @@ export default { } }) }) - - // 初始化TCP连接监听器 - this.initTcpListeners() - + + // 检查用户登录状态,只有已登录并且不是游客才加载自选股数据 if (this.userStore.userInfo && !this.userStore.userInfo.isVisitor) { console.log('是用户登录,加载自选股数据', this.userStore.userInfo) @@ -355,23 +356,26 @@ export default { }else { console.log('用户未登录',this.userStore.userInfo) } + + // 初始化TCP连接监听器 + this.initTcpListeners() // 页面渲染完成后自动连接两个TCP连接 - // this.$nextTick(() => { - // console.log('页面渲染完成,开始自动连接TCP服务器...') - // // 延迟一小段时间确保页面完全加载后再连接 - // setTimeout(() => { - // // 连接今日市场概览TCP(channel 1) - // console.log('连接今日市场概览TCP(channel 1)...') - // this.connectTcp() + this.$nextTick(() => { + console.log('页面渲染完成,开始自动连接TCP服务器...') + // 延迟一小段时间确保页面完全加载后再连接 + setTimeout(() => { + // 连接今日市场概览TCP(channel 1) + console.log('连接今日市场概览TCP(channel 1)...') + this.connectTcp() - // // 稍微延迟后连接我的自选TCP(channel 2) - // setTimeout(() => { - // console.log('连接我的自选TCP(channel 2)...') - // this.connectMyStocksTcp() - // }, 500) - // }, 1000) - // }) + // 稍微延迟后连接我的自选TCP(channel 2) + setTimeout(() => { + console.log('连接我的自选TCP(channel 2)...') + this.connectMyStocksTcp() + }, 500) + }, 1000) + }) }, // 页面销毁前的清理工作 @@ -576,6 +580,28 @@ export default { } console.log('更新后的自选股数据:', this.myStocks) + + // 更新TCP消息中的股票代码 + this.updateTcpStockCodes() + }, + + // 更新TCP消息中的股票代码 + updateTcpStockCodes() { + // 从myStocks中提取前3个股票代码 + const stockCodes = this.myStocks.slice(0, 3).map(stock => stock.code).filter(code => code) + console.log('更新TCP消息股票代码:', stockCodes) + + // 如果有股票代码,更新TCP消息 + if (stockCodes.length > 0) { + // 补充到3个代码(如果不足3个,用默认代码补充) + while (stockCodes.length < 3) { + stockCodes.push('SH.000001') // 默认代码 + } + + // 保存到组件数据中,供TCP方法使用 + this.currentStockCodes = stockCodes + console.log('当前TCP使用的股票代码:', this.currentStockCodes) + } }, // TCP连接相关方法 @@ -1033,7 +1059,7 @@ export default { // } // {"command": "stock_list"} // {"command": "batch_real_time", "stock_codes": ["SH.000001"]} - {"command": "batch_real_time", "stock_codes": ["SH.000001","SH.000002","SH.000003"]} + {"command": "batch_real_time", "stock_codes": this.currentStockCodes} // 发送消息 const success = tcpConnection.send(messageData) @@ -1050,7 +1076,7 @@ export default { // 发送我的自选TCP消息 sendMyStocksTcpMessage() { // 构造要发送的消息对象 - 我的自选股票数据请求 - const messageData = {"command": "batch_real_time", "stock_codes": ["SH.000001","SH.000002","SH.000003"]} + const messageData = {"command": "batch_real_time", "stock_codes": this.currentStockCodes} // 发送消息到channel 2(我的自选TCP连接) const success = tcpConnection.send(messageData, { channel: '2' })