|
|
@@ -5,18 +5,18 @@ import moment from 'moment'
|
|
|
import eventBus from '@/services/bus'
|
|
|
import socket from '@/services/socket'
|
|
|
|
|
|
-export default new (class {
|
|
|
+/**
|
|
|
+ * 行情订阅
|
|
|
+ */
|
|
|
+export const quote = new (class {
|
|
|
/** 订阅列表 */
|
|
|
- private subscribeMap = new Map<string, string[]>();
|
|
|
+ subscribeMap = new Map<string, string[]>();
|
|
|
+ quotePushNotify;
|
|
|
+ quoteServerReconnectNotify;
|
|
|
|
|
|
constructor() {
|
|
|
- // 接收行情服务断线重连成功通知
|
|
|
- eventBus.$on('quoteServerReconnectNotify', () => {
|
|
|
- this.startSubscribe();
|
|
|
- })
|
|
|
-
|
|
|
// 接收行情推送通知
|
|
|
- eventBus.$on('quotePushNotify', (res) => {
|
|
|
+ this.quotePushNotify = eventBus.$on('quotePushNotify', (res) => {
|
|
|
const tmpList = <Proto.GoodsQuote[]>res;
|
|
|
|
|
|
tmpList.forEach((item) => {
|
|
|
@@ -181,81 +181,86 @@ export default new (class {
|
|
|
}
|
|
|
})
|
|
|
})
|
|
|
+
|
|
|
+ // 接收行情服务断线重连成功通知
|
|
|
+ this.quoteServerReconnectNotify = eventBus.$on('quoteServerReconnectNotify', () => {
|
|
|
+ startSubscribe();
|
|
|
+ })
|
|
|
}
|
|
|
+})
|
|
|
|
|
|
- /**
|
|
|
- * 开始订阅
|
|
|
- */
|
|
|
- startSubscribe() {
|
|
|
- const subscribeData: Proto.GoodsQuoteReq[] = [];
|
|
|
+/**
|
|
|
+ * 开始订阅
|
|
|
+ */
|
|
|
+export function startSubscribe() {
|
|
|
+ const subscribeData: Proto.GoodsQuoteReq[] = [];
|
|
|
|
|
|
- this.subscribeMap.forEach((value) => {
|
|
|
- const item = value.map((code) => ({
|
|
|
- goodsCode: code,
|
|
|
- exchangeCode: 250,
|
|
|
- subState: 0
|
|
|
- }))
|
|
|
- subscribeData.push(...item);
|
|
|
- })
|
|
|
+ quote.subscribeMap.forEach((value) => {
|
|
|
+ const item = value.map((code) => ({
|
|
|
+ goodsCode: code,
|
|
|
+ exchangeCode: 250,
|
|
|
+ subState: 0
|
|
|
+ }))
|
|
|
+ subscribeData.push(...item);
|
|
|
+ })
|
|
|
|
|
|
- if (subscribeData.length) {
|
|
|
- console.log('开始行情订阅', subscribeData)
|
|
|
- quoteServerRequest({
|
|
|
- data: subscribeData,
|
|
|
- success: (res) => {
|
|
|
- if (res.length) {
|
|
|
- console.log('行情订阅成功', res)
|
|
|
- } else {
|
|
|
- console.error('行情订阅失败')
|
|
|
- }
|
|
|
- },
|
|
|
- fail: (err) => {
|
|
|
- console.error('行情订阅失败', err)
|
|
|
+ if (subscribeData.length) {
|
|
|
+ console.log('开始行情订阅', subscribeData)
|
|
|
+ quoteServerRequest({
|
|
|
+ data: subscribeData,
|
|
|
+ success: (res) => {
|
|
|
+ if (res.length) {
|
|
|
+ console.log('行情订阅成功', res)
|
|
|
+ } else {
|
|
|
+ console.error('行情订阅失败')
|
|
|
}
|
|
|
- })
|
|
|
- } else {
|
|
|
- // 没有订阅商品的时候,断开连接
|
|
|
- socket.closeQuoteServer();
|
|
|
- }
|
|
|
+ },
|
|
|
+ fail: (err) => {
|
|
|
+ console.error('行情订阅失败', err)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ // 没有订阅商品的时候,断开连接
|
|
|
+ socket.closeQuoteServer();
|
|
|
}
|
|
|
+}
|
|
|
|
|
|
- /**
|
|
|
- * 添加订阅
|
|
|
- * @param goodsCodes
|
|
|
- * @param key
|
|
|
- * @returns
|
|
|
- */
|
|
|
- addSubscribe(goodsCodes: string[], key?: string) {
|
|
|
- const uuid = key ?? v4();
|
|
|
- const value = this.subscribeMap.get(uuid) ?? [];
|
|
|
+/**
|
|
|
+ * 添加订阅
|
|
|
+ * @param goodsCodes
|
|
|
+ * @param key
|
|
|
+ * @returns
|
|
|
+ */
|
|
|
+export function addSubscribe(goodsCodes: string[], key?: string) {
|
|
|
+ const uuid = key ?? v4();
|
|
|
+ const value = quote.subscribeMap.get(uuid) ?? [];
|
|
|
|
|
|
- // 对相同 key 订阅的商品进行合并处理
|
|
|
- this.subscribeMap.set(uuid, [...value, ...goodsCodes]);
|
|
|
- this.startSubscribe();
|
|
|
+ // 对相同 key 订阅的商品进行合并处理
|
|
|
+ quote.subscribeMap.set(uuid, [...value, ...goodsCodes]);
|
|
|
+ startSubscribe();
|
|
|
|
|
|
- return {
|
|
|
- uuid,
|
|
|
- stop: () => {
|
|
|
- console.log('删除订阅', uuid);
|
|
|
- this.subscribeMap.delete(uuid);
|
|
|
- this.startSubscribe();
|
|
|
- },
|
|
|
- }
|
|
|
+ return {
|
|
|
+ uuid,
|
|
|
+ stop: () => {
|
|
|
+ console.log('删除订阅', uuid);
|
|
|
+ quote.subscribeMap.delete(uuid);
|
|
|
+ startSubscribe();
|
|
|
+ },
|
|
|
}
|
|
|
+}
|
|
|
|
|
|
- /**
|
|
|
- * 删除订阅
|
|
|
- * @param keys
|
|
|
- */
|
|
|
- removeSubscribe(...keys: string[]) {
|
|
|
- if (keys.length) {
|
|
|
- console.log('删除订阅', keys);
|
|
|
- keys.forEach((key) => {
|
|
|
- this.subscribeMap.delete(key);
|
|
|
- })
|
|
|
- } else {
|
|
|
- this.subscribeMap.clear();
|
|
|
- }
|
|
|
- this.startSubscribe();
|
|
|
+/**
|
|
|
+ * 删除订阅
|
|
|
+ * @param keys
|
|
|
+ */
|
|
|
+export function removeSubscribe(...keys: string[]) {
|
|
|
+ if (keys.length) {
|
|
|
+ console.log('删除订阅', keys);
|
|
|
+ keys.forEach((key) => {
|
|
|
+ quote.subscribeMap.delete(key);
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ quote.subscribeMap.clear();
|
|
|
}
|
|
|
-})
|
|
|
+ startSubscribe();
|
|
|
+}
|