li.shaoyi 1 年之前
父节点
当前提交
91c8d39ec9
共有 4 个文件被更改,包括 23 次插入13 次删除
  1. 1 1
      src/business/common/index.ts
  2. 1 3
      src/business/login/index.ts
  3. 2 2
      src/services/http/index.ts
  4. 19 7
      src/services/websocket/trade.ts

+ 1 - 1
src/business/common/index.ts

@@ -11,7 +11,7 @@ export async function checkToken() {
     } catch (err) {
         // 临时解决方案,有可能出现网络问题导致超时后账号被登出
         if (err !== '业务超时') {
-            eventBus.$emit('LogoutNotify')
+            eventBus.$emit('LogoutNotify', '登录失效,请重新登录')
         }
         return Promise.reject(err)
     }

+ 1 - 3
src/business/login/index.ts

@@ -8,7 +8,7 @@ import quoteSocket from '@/services/websocket/quote'
 import tradeSocket from '@/services/websocket/trade'
 import tradeV2Socket from '@/services/websocket/tradeV2'
 import { encryptBody } from '@/services/websocket/package/crypto'
-import { checkToken, checkTokenLoop, stopCheckToken } from '@/business/common'
+import { checkToken, checkTokenLoop } from '@/business/common'
 import eventBus from '@/services/bus'
 import cryptojs from 'crypto-js'
 
@@ -134,7 +134,6 @@ export function useLogin(persist = false) {
             return forcedPasswordChange
         } catch (err) {
             // 登录失败断开所有链路
-            stopCheckToken()
             tradeSocket.close()
             tradeV2Socket.close()
             quoteSocket.close()
@@ -147,7 +146,6 @@ export function useLogin(persist = false) {
 
     // 用户登出
     const userLogout = (callback?: () => void) => {
-        stopCheckToken()
         tradeSocket.close()
         tradeV2Socket.close()
         quoteSocket.close()

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

@@ -140,7 +140,7 @@ export default new (class {
         const res = await this.request<CommonResult<T>>(config, errMsg)
         switch (res.code) {
             case ResultCode.InvalidToken: {
-                return Promise.reject('令牌无效')
+                return Promise.reject('登录失效')
             }
             case ResultCode.Success: {
                 return res
@@ -163,7 +163,7 @@ export default new (class {
         const res = await this.request<CommonResult<T>>(config, errMsg)
         switch (res.code) {
             case ResultCode.InvalidToken: {
-                return Promise.reject('令牌无效')
+                return Promise.reject('登录失效')
             }
             case 0: {
                 return res

+ 19 - 7
src/services/websocket/trade.ts

@@ -8,10 +8,12 @@ import { pushMessage50 } from './message'
 import { encodeProto, decodeProto } from './package/package50/proto'
 
 export default new (class {
-    /** 交易链路 */
-    private socket = new MTP2WebSocket(Package50);
-
     constructor() {
+        this.socket.onClose = () => {
+            // 停止令牌校验
+            stopCheckToken()
+        }
+
         this.socket.onBeforeReconnect = (count) => {
             // 停止令牌校验
             stopCheckToken()
@@ -21,14 +23,19 @@ export default new (class {
 
         this.socket.onReconnect = () => {
             // 重新进行令牌校验
-            checkToken().then(() => checkTokenLoop())
-            // 交易服务重连成功通知
-            eventBus.$emit('TradeServerReconnectSuccessNotify')
+            checkToken().then(() => {
+                checkTokenLoop()
+                // 交易服务重连成功通知
+                eventBus.$emit('TradeServerReconnectSuccessNotify')
+            })
         }
 
         this.socket.onPush = (pkg) => pushMessage50(pkg, 'encrypted')
     }
 
+    /** 交易链路 */
+    private socket = new MTP2WebSocket(Package50);
+
     async connect() {
         const res = await service.onReady()
         await this.socket.connect(res.tradeUrl)
@@ -39,7 +46,7 @@ export default new (class {
      * @param params
      * @returns 
      */
-    send<T>({ data, requestCode, responseCode }: {
+    async send<T>({ data, requestCode, responseCode }: {
         data: unknown;
         requestCode: keyof typeof FunCode;
         responseCode: keyof typeof FunCode;
@@ -47,6 +54,11 @@ export default new (class {
         const requestId = FunCode[requestCode]
         const responseId = FunCode[responseCode]
 
+        // 请求之前确保重连后 token 是有效的
+        if (requestCode !== 'TokenCheckReq' && requestCode !== 'LoginReq' && this.socket.connState !== 'Connected') {
+            await checkToken()
+        }
+
         return new Promise<T>((resolve, reject) => {
             encodeProto(requestCode, data).then((res) => {
                 this.connect().then(() => {