1
PC-20250623MANY\Administrator
2025-09-30 9ddad348a549154e7b328f9d7dd9517d2abd330c
src/components/stock-list.vue
@@ -66,6 +66,7 @@
import nPagination from "@/components/nPagination.vue";
import * as api from "@/axios/api";
import { Toast } from "vant";
import { WhrWebSocket } from "@/utils/WhrWebSocket";
export default {
  name: "stock_list",
  components: {
@@ -100,9 +101,16 @@
  watch: {
    propOption: {
      handler(val) {
        // 根据当前股票类型连接对应的ws
        if (val.stockType == "US")
          this.initWebSocket("wss://usws.yanshiz.com/websocket-server");
        else this.initWebSocket("wss://ws.acapl.net/websocket-server");
        this.pageNum = 1;
        this.getStockList();
      }
      },
      deep: true,
      immediate: true
    },
    pageNum: {
      handler(val) {
@@ -113,6 +121,7 @@
  },
  mounted() {
    this.getStockList();
    // this.initWebSocket();
  },
  methods: {
    // 获取数据
@@ -161,6 +170,35 @@
      } else {
        Toast.fail(data.msg);
      }
    },
    // 连接ws实时监控变动
    initWebSocket(url) {
      console.log("initWebSocket");
      if (this.Trade) {
        this.Trade.close();
      }
      this.Trade = new WhrWebSocket({
        path: url,
        onmessage: this.getTradeMessage
      });
      this.Trade.init();
    },
    getTradeMessage({ data }) {
      let result = JSON.parse(data);
      let pid = result.pid;
      let userToUpdate = this.stockList.find(item => item.code == pid);
      if (userToUpdate) {
        // 更新对象数据
        userToUpdate.nowPrice = result.last;
        userToUpdate.hcrateP = result.pcp;
      }
    }
  },
  beforeDestroy() {
    if (this.Trade) {
      this.Trade.close();
      console.log("WebSocket disconnected");
    }
  }
};