Prechádzať zdrojové kódy

修改行情商品订阅

huangbin 4 rokov pred
rodič
commit
339d2a695a

+ 1 - 1
public/config/app.config.json

@@ -1,3 +1,3 @@
 {
-    "apiUrl": "http://192.168.31.204:8080/cfg?key=test_204"
+    "apiUrl": "http://192.168.31.139:8080/cfg?key=test_139"
 }

+ 2 - 2
src/services/bus/index.ts

@@ -3,7 +3,7 @@ import { TradeMode } from '@/common/constants/enumCommon';
 import APP from '@/services';
 import { isAllEnum } from '@/services/bus/allEnum';
 import { isLogin } from '@/services/bus/login';
-import { checkTokenLoop, handleTokenCheck } from '@/services/bus/token';
+import { checkTokenAction, handleTokenCheck } from '@/services/bus/token';
 import { getAllEnums, GetPCMenus, getServerTime, queryErrorInfos, QueryTableDefine } from '@/services/go/commonService/index';
 import { getAppConfig, getCommonApiUrl } from '@/services/go/config';
 import { LoginQuery } from '@/services/go/useInfo/index';
@@ -38,7 +38,7 @@ export async function globalDataRefresh(): Promise<string> {
                 return Promise.reject('刷新时token校验失败');
             } else {
                 // token校验成功 马上轮询
-                checkTokenLoop();
+                checkTokenAction();
                 await Promise.all([GetErmcpGoods(), GetPCMenus(), QueryTableDefine(), LoginQuery(), QueryDeliveryGoodsDetail(), getTaAccount(), queryMarketRun()])
                 const marketInfo = getMarketByTradeMode(TradeMode.WarehouseReceiptTrade);
                 if (marketInfo) {

+ 2 - 2
src/services/bus/login.ts

@@ -1,7 +1,7 @@
 import { TradeMode } from '@/common/constants/enumCommon';
 import APP from '@/services';
 import { isAllEnum } from '@/services/bus/allEnum';
-import { checkTokenLoop } from '@/services/bus/token';
+import { checkTokenAction } from '@/services/bus/token';
 import { getAllEnums, GetPCMenus, getServerTime, queryErrorInfos, QueryTableDefine } from '@/services/go/commonService/index';
 import { GetErmcpGoods, QueryDeliveryGoodsDetail } from '@/services/go/ermcp/goodsInfo';
 import { GetLoginID, LoginQuery } from '@/services/go/useInfo/index';
@@ -45,7 +45,7 @@ export const login = async (logidCode: string, password: string, byteArr: Uint8A
         }
 
         // 校验token
-        checkTokenLoop();
+        checkTokenAction();
         // await LoginQuery();
         return loginData;
     } catch (err) {

+ 10 - 1
src/services/bus/token.ts

@@ -23,6 +23,7 @@ export function getToken(): string {
  * 处理token校验
  */
 export function handleTokenCheck() {
+    console.log('校验token中==========')
     return tokenCheck().catch((err) => {
         Router.push({ path: '/login' }); // 回到登录页面
         console.error(`token校验失败:${err}`);
@@ -33,9 +34,17 @@ export function handleTokenCheck() {
 /**
  * 轮询校验token
  */
-export function checkTokenLoop(): void {
+export function checkTokenAction(): void {
     const timeDiff = APP.get('checkTokenTimeDiff');
     timerUtil.setInterval(() => {
         handleTokenCheck()
     }, timeDiff, 'tokenCheck');
 }
+
+/**
+ * 停止校验token
+ */
+export function stopCheckToken() {
+    console.log('停止校验token')
+    timerUtil.clearInterval('tokenCheck')
+}

+ 2 - 2
src/services/dataCenter/index.ts

@@ -22,8 +22,8 @@ const needClearSourceData: NeedClearSourceDataType = {
     DeliveryGoodsList: [],
     marketRun: new MarketrunModal(), // 市场运行信息
 
-    // checkTokenTimeDiff: Math.floor(Math.random() * 6 + 5) * 60 * 1000,
-    checkTokenTimeDiff: 1 * 60 * 1000,
+    checkTokenTimeDiff: Math.floor(Math.random() * 6 + 5) * 60 * 1000,
+    // checkTokenTimeDiff: 1 * 60 * 1000,
     accountList: [],
     selectedAccount: new SelectedAccountModel(),
     quoteDayInfo: [],

+ 15 - 5
src/services/index.ts

@@ -1,4 +1,4 @@
-import { checkTokenLoop } from '@/services/bus/token';
+import { checkTokenAction, stopCheckToken } from '@/services/bus/token';
 import { NeedClearSourceDataType, NoClearSourceDataType } from '@/services/dataCenter/interface';
 import { funCode } from '@/services/funcode/index';
 import { serviceURL } from '@/services/request/index';
@@ -153,8 +153,8 @@ export default new (class LifeCycleCtr {
             switch (state) {
                 /** 开始重连 */
                 case ReconnectChangeState.BeginReconnect:
-                    // 重连过程中无需校验token 清除token
-                    timerUtil.clearInterval('tokenCheck');
+                    // 停止 token 校验
+                    // stopCheckToken()
                     console.log(obj.host, '开始尝试重连服务端');
                     break;
                 /** 尝试重连失败,将在下一个周期后再次尝试重连 */
@@ -163,15 +163,20 @@ export default new (class LifeCycleCtr {
                     break;
                 /** 重连成功,将进行业务操作 */
                 case ReconnectChangeState.ReconnectSuccessed:
-                    // 重连成功继续校验token
-                    checkTokenLoop();
                     console.log(obj.host, '重连成功,可开始进行业务操作');
+                    // 通知上层 重新订阅商品
+                    eventBus.$emit('quoteReconnectSucess', true);
                     break;
                 /** 重连成功后业务操作失败并将再次重试,由业务模块发起 */
                 case ReconnectChangeState.LoginFail:
                     break;
                 /** 重连成功后业务操作成功(包括交易服务的账户登录状态更新或行情服务的行情订阅等 */
                 case ReconnectChangeState.Logined:
+                    // 重连成功继续校验token
+                    // checkTokenAction();
+                    console.log(obj.host, '重连成功后业务操作成功(包括交易服务的账户登录状态更新或行情服务的行情订阅等');
+                    // 行情链路重连成功后,需要通知上层 重新订阅商品
+
                     break;
             }
         };
@@ -212,6 +217,8 @@ export default new (class LifeCycleCtr {
             switch (state) {
                 /** 开始重连 */
                 case ReconnectChangeState.BeginReconnect:
+                    // 停止 token 校验
+                    stopCheckToken()
                     console.log(obj.host, '开始尝试重连服务端');
                     break;
                 /** 尝试重连失败,将在下一个周期后再次尝试重连 */
@@ -220,6 +227,8 @@ export default new (class LifeCycleCtr {
                     break;
                 /** 重连成功,将进行业务操作 */
                 case ReconnectChangeState.ReconnectSuccessed:
+                    // 重新启动定时 token 校验
+                    checkTokenAction()
                     console.log(obj.host, '重连成功,将进行业务操作');
                     break;
                 /** 重连成功后业务操作失败并将再次重试,由业务模块发起 */
@@ -227,6 +236,7 @@ export default new (class LifeCycleCtr {
                     break;
                 /** 重连成功后业务操作成功(包括交易服务的账户登录状态更新或行情服务的行情订阅等 */
                 case ReconnectChangeState.Logined:
+
                     break;
             }
         };

+ 1 - 1
src/services/socket/login/index.ts

@@ -64,7 +64,7 @@ export const tokenCheck = (): Promise<string> => {
         if (isLogin()) {
             const loginId = geLoginID_number();
             const token = getToken();
-            console.log('token::::::', token)
+            // console.log('token::::::', token)
             const systemDate = APP.get('systemDate');
             const targetDate = Date.parse(systemDate) + Date.now() - preDate;
             const params = {

+ 1 - 1
src/services/socket/protobuf/buildReq.ts

@@ -184,7 +184,7 @@ function parseSoleProtoRsp(rspPackage: any, funCodeName: string): ParseRsp {
     isErrer(rspPackage, funCodeName);
     const reqPackage = ProtobufCtr.generateProtoContent(funCodeName, rspPackage.content);
     const { RetCode, Status, RetDesc } = reqPackage;
-    console.warn(`解析${funCodeName}对应的报文错误:RetCode: ${RetCode}: RetDesc${RetDesc}`)
+    // console.warn(`解析${funCodeName}对应的报文错误:RetCode: ${RetCode}: RetDesc${RetDesc}`)
     if (RetCode !== null) {
         if (RetCode === 0) {
             return { isSuccess: true, result: reqPackage };

+ 42 - 6
src/services/socket/quota/index.ts

@@ -1,11 +1,18 @@
 import APP from '@/services';
 import { funCode } from '@/services/funcode/index';
 import { buildSubscribePeq, parseSubscribeRsp } from '@/services/socket/quota/adapter/index';
+import Bus from '@/utils/eventBus/index';
+import timerUtil from '@/utils/timer/timerUtil';
 import { Callback } from '@/utils/websocket/index';
 import { SubscribeInfoType } from './interface';
 
+
 const subscribeGoodsList = new Map<string, SubscribeInfoType[]>(); // 订阅商品数据中心池
 
+let isSubscribeSucess = false; // 订阅是否成功
+
+const timeDiff = 2 * 1000 // 没有订阅成功,下次发起订阅的间隔时间
+
 /**
  * 重组订阅商品(包括去重,组合)
  */
@@ -23,25 +30,48 @@ function recombinationSubscribeGoods(): SubscribeInfoType[] {
 }
 
 /**
+ * 尝试商品订阅,如果订阅不成功,则 timeDiff 后续继续发起订阅请求,直到订阅成功后为止
+ */
+function trySubcribe() {
+    const timer = 'quoteSucribe'
+    actionSubcribe()
+    timerUtil.setInterval(() => {
+        if (isSubscribeSucess) {
+            // 订阅成功后,不在继续尝试订阅了
+            timerUtil.clearInterval(timer)
+        } else {
+            // 订阅失败,重新发起订阅
+            actionSubcribe()
+        }
+    }, timeDiff, timer)
+}
+
+/**
  * 开始商品订阅
  * @param subscribeInfos
  */
 function actionSubcribe() {
     const arr = recombinationSubscribeGoods();
     const req = buildSubscribePeq(arr);
+    isSubscribeSucess = false
     APP.sendQuoteServer(req, funCode.MainClassNumber_Quota_SubscriptRsp, {
         onSuccess: (res: any) => {
             parseSubscribeRsp(res)
                 .then((value) => {
-                    if (value) {
-                        console.log('订阅成功!', value);
-                    }
+                    console.log('订阅成功!', value);
+                    isSubscribeSucess = true
                 })
                 .catch((err) => {
+                    isSubscribeSucess = false
                     console.log('订阅失败:', err);
+                    // 失败原因:行情断
+                    // 重新 走行情链接逻辑
                 });
         },
-        onFail: (err) => console.log('订阅失败:', err),
+        onFail: (err) => {
+            isSubscribeSucess = false
+            console.log('订阅失败:', err)
+        },
     } as Callback);
 }
 
@@ -52,7 +82,7 @@ function actionSubcribe() {
 export function addSubscribeQuotation(uuid: string, subscribeInfos: SubscribeInfoType[]) {
     APP.connectQuote().then(() => {
         subscribeGoodsList.set(uuid, subscribeInfos);
-        actionSubcribe();
+        trySubcribe();
     });
 }
 
@@ -63,10 +93,16 @@ export function removeSubscribeQuotation(uuid: string) {
     if (subscribeGoodsList.has(uuid)) {
         subscribeGoodsList.delete(uuid);
         if (subscribeGoodsList.size) {
-            actionSubcribe();
+            trySubcribe();
         } else {
             // 没有订阅商品时候,主动断开行情链路
             APP.closeQuote();
         }
     }
 }
+
+// 行情链路 重连成功后,重新订阅商品
+Bus.$onOnly('quoteReconnectSucess', () => {
+    console.log('======== 行情链路 重连成功后,开启重新订阅商品')
+    trySubcribe()
+})

+ 1 - 0
src/utils/eventBus/index.ts

@@ -27,6 +27,7 @@ interface EnentNamesOnlyOneValue {
 
     orderCanceledNtf: string; //接收到委托单撤单通知
     quoteReceiveNtf: string; // 接收实时行情推送
+    quoteReconnectSucess: string; // 行情链路重连成功通知
 }
 // 事件触发器
 export default new (class Bus {

+ 1 - 0
src/utils/timer/timerUtil.ts

@@ -10,6 +10,7 @@ export interface IntervalTimerNames {
     buyAndSellMartet: string; // 买卖大厅
     realTime: string; // 实时敞口监控
     countdown: string; // 倒计时
+    quoteSucribe: string; // 行情订阅
 }
 export interface TimeoutTimerNames {
     logoutTimer: string; //登出1s延时器

+ 2 - 1
src/utils/websocket/index.ts

@@ -1,5 +1,5 @@
-import { Package40, Package50 } from './package';
 import timerUtil from '@/utils/timer/timerUtil';
+import { Package40, Package50 } from './package';
 /** 重连状态 */
 export enum ReconnectChangeState {
     /** 开始重连 */
@@ -219,6 +219,7 @@ export class MTP2WebSocket<T extends Package40 | Package50> {
      */
     send(p: T, rsp?: number, callback?: Callback) {
         if (this.connState != 2) {
+            debugger
             if (callback && callback.onFail) callback.onFail(new Error('连接状态错误'));
             return;
         }