From 732c30b33f782c2d2ebb62eacda2fb7a453a7ecd Mon Sep 17 00:00:00 2001
From: admin <344137771@qq.com>
Date: Sat, 31 Jan 2026 11:01:45 +0800
Subject: [PATCH] 1
---
src/components/stock-list.vue | 67 ++++++++++++++++++++++++++++++---
1 files changed, 61 insertions(+), 6 deletions(-)
diff --git a/src/components/stock-list.vue b/src/components/stock-list.vue
index c418f9b..27ef13a 100644
--- a/src/components/stock-list.vue
+++ b/src/components/stock-list.vue
@@ -77,7 +77,8 @@
pageNum: 1,
pageSize: 10,
total: 1,
- stockList: []
+ stockList: [],
+ premarketTimer: null // 盘前数据轮询定时器
};
},
props: {
@@ -103,8 +104,8 @@
handler(val) {
// 根据当前股票类型连接对应的ws
if (val.stockType == "US")
- this.initWebSocket("wss://ws.isusstock.com/websocket-server");
- else this.initWebSocket("wss://ws.acapl.net/websocket-server");
+ this.initWebSocket("wss://ws.sceazy.com/websocket-server");
+ else this.initWebSocket("wss://ws.jafco1.cc/websocket-server");
this.pageNum = 1;
this.getStockList();
@@ -122,6 +123,8 @@
mounted() {
this.getStockList();
// this.initWebSocket();
+ this.getPremarketStock();
+ this.startPremarketPolling();
},
methods: {
// 获取数据
@@ -140,6 +143,51 @@
let data = await this.listApi(opt);
this.stockList = data.data.list;
this.total = data.data.total || 1;
+ // 列表更新后,重新应用盘前数据(如果轮询已启动)
+ if (this.premarketTimer) {
+ this.getPremarketStock();
+ }
+ },
+ // 获取后台设置的盘前数据
+ async getPremarketStock() {
+ try {
+ let data = await api.getPremarketStock({});
+ if (data.status === 0) {
+ let list = data.data || [];
+ // 通过code匹配,更新stockList中的nowPrice
+ list.forEach((premarketItem) => {
+ const stockItem = this.stockList.find(
+ (item) => item.code === premarketItem.code
+ );
+ if (stockItem && premarketItem.price) {
+ // 更新价格
+ stockItem.nowPrice = premarketItem.price;
+ stockItem.hcrate = premarketItem.hcrate;
+ stockItem.hcrateP = premarketItem.hcrateP;
+ // 标记该价格已被盘前数据更新,防止WebSocket覆盖
+ this.$set(stockItem, 'isPremarketUpdated', true);
+ }
+ });
+ }
+ } catch (error) {
+ console.error('获取盘前数据失败:', error);
+ }
+ },
+ // 启动盘前数据轮询
+ startPremarketPolling() {
+ // 清除已有定时器
+ this.stopPremarketPolling();
+ // 每3秒轮询一次(可根据需要调整间隔)
+ this.premarketTimer = setInterval(() => {
+ this.getPremarketStock();
+ }, 3000);
+ },
+ // 停止盘前数据轮询
+ stopPremarketPolling() {
+ if (this.premarketTimer) {
+ clearInterval(this.premarketTimer);
+ this.premarketTimer = null;
+ }
},
// 点击进入详情
toDetails(item) {
@@ -189,17 +237,24 @@
let pid = result.pid;
let userToUpdate = this.stockList.find(item => item.code == pid);
if (userToUpdate) {
- // 更新对象数据
- userToUpdate.nowPrice = result.last;
- userToUpdate.hcrateP = result.pcp;
+ // 如果该股票的价格已被盘前数据更新,则不再通过WebSocket覆盖
+ if (!userToUpdate.isPremarketUpdated) {
+ // 更新对象数据
+ userToUpdate.nowPrice = result.last;
+ userToUpdate.hcrateP = result.pcp;
+ userToUpdate.hcrate = result.pc;
+ }
}
}
},
beforeDestroy() {
+ // 清除WebSocket连接
if (this.Trade) {
this.Trade.close();
console.log("WebSocket disconnected");
}
+ // 清除盘前数据轮询定时器
+ this.stopPremarketPolling();
}
};
</script>
--
Gitblit v1.9.3