1
PC-20250623MANY\Administrator
2025-08-24 f6eb2c4e1b1392576a6592aa4cce19f2efd3a978
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<template>
  <div class="home_ltl">
    <div class="tabs_box">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="United States" name="US"></el-tab-pane>
        <el-tab-pane label="Hong Kong" name="HK"></el-tab-pane>
        <el-tab-pane label="Taiwan" name="TW"></el-tab-pane>
        <el-tab-pane label="India" name="IN"></el-tab-pane>
      </el-tabs>
    </div>
 
    <div class="search_box">
      <el-input
        :placeholder="$t('请输入')"
        v-model="keyWords"
        clearable
        size="small"
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="Search"
        ></el-button>
      </el-input>
    </div>
 
    <div class="resoult_list">
      <el-table
        height="100%"
        :data="tableData"
        style="width: 100%"
        size="small"
        empty-text="No Data"
        @row-click="Choice"
      >
        <el-table-column prop="spell" :label="$t('hj313')">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.stock_type != $mc ? 'success' : ''"
              size="small"
              style="margin-right: 8px"
            >
              {{ scope.row.stock_type }}
            </el-tag>
            <span>{{ scope.row.spell }}</span>
          </template>
        </el-table-column>
 
        <el-table-column prop="nowPrice" :label="$t('Price')" width="100">
        </el-table-column>
 
        <el-table-column
          prop="hcrateP"
          :label="$t('Change')"
          align="right"
          width="80"
        >
          <template slot-scope="scope">
            <div
              :class="{
                red: scope.row.hcrate < 0,
                green: scope.row.hcrate > 0,
              }"
            >
              {{ scope.row.hcrateP }}
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
 
    <div class="pagination_box">
      <el-pagination
        layout="prev, next"
        :total="total"
        :current-page="pageNum"
        :page-size="pageSize"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>
  </div>
</template>
 
<script>
import * as api from "@/axios/api";
// import handleDt from "@/utils/deTh";
import { WhrWebSocket } from "@/utils/WhrWebSocket";
import mixins from "@/mixins/myMixins"; // 混入
export default {
  mixins: [mixins],
  data() {
    return {
      activeName: "US",
      keyWords: "",
      // 列表参数,必须是opt和myMixins混入配合使用
      opt: {},
    };
  },
  created() {
    this.pageSize = 50;
    this.opt.stockType = this.activeName; // 赋值类型
    this.apiInterface = api.getStockByType; // 赋值接口
    this.init(); // 获取记录列表
    // this.initWebSocket(); // 连接ws实时监控变动
  },
  watch: {
    activeName: {
      handler(val) {
        // 根据当前股票类型连接对应的ws
        if (val == "US")
          this.initWebSocket("wss://usws.yanshiz.com/websocket-server");
        else this.initWebSocket("wss://ws.acapl.net/websocket-server");
      },
      immediate: true,
      deep: true,
    },
  },
  computed: {
    tableData() {
      return this.list;
    },
  },
  props: {
    total: {
      type: Number,
      default: 0,
    },
    pageNum: {
      type: Number,
      default: 1,
    },
  },
  components: {},
  mounted() {},
  beforeDestroy() {
    if (this.Trade) {
      this.Trade.close();
      console.log("WebSocket disconnected");
    }
  },
  methods: {
    handleClick() {
      this.opt.stockType = this.activeName;
      this.init();
    },
    Search() {
      this.opt.keyWords = this.keyWords;
      this.init();
    },
    // 股票选择
    Choice(val) {
      this.$emit("choice", val.code);
    },
    // 连接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.tableData.find((item) => item.code == pid);
      if (userToUpdate) {
        // 更新对象数据
        userToUpdate.nowPrice = result.last;
        userToUpdate.hcrateP = result.pcp;
      }
    },
    getListAfter() {
      if (this.once) return;
      this.Choice(this.tableData[0]);
      this.once = true;
    },
  },
  beforeDestroy() {
    if (this.Trade) {
      this.Trade.close();
      console.log("WebSocket disconnected");
    }
  },
};
</script>
 
<style lang="scss" scoped>
.home_ltl {
  width: 360px;
  border-right: 1px solid #ebeef5;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  .red {
    color: #ee0a24 !important;
  }
  .green {
    color: #07c160 !important;
  }
  .resoult_list {
    height: 0;
    flex: 1;
  }
  .pagination_box {
    display: flex;
    justify-content: flex-end;
    // border-top: 1px solid #ebeef5;
  }
  .search_box {
    padding: 10px;
    border-bottom: 1px solid #ebeef5;
  }
 
  .tabs_box {
    padding: 0 16px;
 
    ::v-deep .el-tabs__header {
      margin-bottom: 0;
    }
  }
}
</style>