lanjianrong 4 лет назад
Родитель
Сommit
3ca3b501f6

+ 1 - 1
src/components/RightContent/AvatarDropdown.tsx

@@ -49,10 +49,10 @@ const AvatarDropdown: React.FC<GlobalHeaderRightProps> = ({ menu }) => {
           title: '温馨提示',
           content: '是否确认退出系统?',
           onOk: () => {
-            setInitialState({ ...initialState, currentUser: undefined, roles: [], permData: {} })
             // 清除AuthCache
             removeToken()
             loginOut()
+            setInitialState({ ...initialState, currentUser: undefined, roles: [], permData: {} })
             return
           }
         })

+ 14 - 15
src/components/RightContent/Book/index.tsx

@@ -8,24 +8,23 @@ const Book = () => {
   const [onlineList, setOnlineList] = useState([])
   const { initialState } = useModel('@@initialState')
 
-  const { username, staffId: id, wsToken: token } = initialState?.currentUser
-  // if (!currentUser) return null
-  // eslint-disable-next-line react-hooks/rules-of-hooks
   useEffect(() => {
-    id &&
-      ws.init(`ws://cld2qa.com/summon/v1/chat/link?username=${username}&id=${id}&token=${token}`, {
-        onMessage: msg => {
-          onMessage(msg)
-          if (msg?.cmd === CmdType.OnlineBook) {
-            setOnlineList(msg.onlineStaff)
+    if (initialState?.currentUser) {
+      const { username, staffId: id, wsToken: token } = initialState?.currentUser
+      id &&
+        ws.init(
+          `ws://cld2qa.com/summon/v1/chat/link?username=${username}&id=${id}&token=${token}`,
+          {
+            onMessage: msg => {
+              onMessage(msg)
+              if (msg?.cmd === CmdType.OnlineBook) {
+                setOnlineList(msg.onlineStaff)
+              }
+            }
           }
-        }
-      })
-
-    return () => {
-      ws.disconnect()
+        )
     }
-  }, [])
+  }, [initialState?.currentUser])
   return (
     <div className="fixed top-12 right-0">
       <div className="relative flex flex-row items-center">

+ 4 - 5
src/pages/user/login/index.jsx

@@ -8,7 +8,6 @@ import styles from './index.less'
 import { apiLogin } from '@/services/login'
 import { setAuthCache } from '@/utils/auth'
 import { TOKEN_KEY } from '@/utils/cache/cacheEnum'
-import ws, { onMessage } from '@/utils/ws'
 
 const LoginMessage = ({ content }) => (
   <Alert
@@ -44,10 +43,10 @@ const Login = () => {
     const permData = await initialState?.fetchPermData?.()
     if (userInfo) {
       setInitialState({ ...initialState, ...userInfo, permData })
-      const { username, staffId: id, wsToken: token } = userInfo?.currentUser
-      ws.init(`ws://cld2qa.com/summon/v1/chat/link?username=${username}&id=${id}&token=${token}`, {
-        onMessage
-      })
+      // const { username, staffId: id, wsToken: token } = userInfo?.currentUser
+      // ws.init(`ws://cld2qa.com/summon/v1/chat/link?username=${username}&id=${id}&token=${token}`, {
+      //   onMessage
+      // })
     }
   }
 

+ 3 - 1
src/utils/ws.ts

@@ -39,7 +39,9 @@ class Ws {
   public init(uri: string, opts?: Options) {
     this.uri = uri
     this.opts = opts
-    this.connectWs()
+    if (!this.wsIns || this.wsIns?.readyState === ReadyState.Closed) {
+      this.connectWs()
+    }
   }
 
   // 利用promise封装,解决ws可能没有实例化