22
33import { useEffect , useRef } from 'react' ;
44import { useRouter , usePathname } from 'next/navigation' ;
5- import { App } from 'antd' ;
5+ import { App , Modal } from 'antd' ;
66import { ExclamationCircleOutlined } from '@ant-design/icons' ;
77import { authService } from '@/services/authService' ;
88import { EVENTS } from '@/types/auth' ;
@@ -28,13 +28,6 @@ export function SessionListeners() {
2828 const showSessionExpiredModal = ( ) => {
2929 // 若已显示过,则直接返回
3030 if ( modalShownRef . current ) return ;
31-
32- // 修复:在首页不显示过期弹窗
33- if ( pathname === '/' || pathname ?. startsWith ( '/?' ) ||
34- pathname ?. startsWith ( '/zh' ) || pathname ?. startsWith ( '/en' ) ) {
35- return ;
36- }
37-
3831 modalShownRef . current = true ;
3932
4033 modal . confirm ( {
@@ -50,6 +43,7 @@ export function SessionListeners() {
5043 } finally {
5144 // Mark the source as session expired
5245 setIsFromSessionExpired ( true ) ;
46+ Modal . destroyAll ( ) ;
5347 openLoginModal ( ) ;
5448 setTimeout ( ( ) => ( modalShownRef . current = false ) , 500 ) ;
5549 }
@@ -83,12 +77,6 @@ export function SessionListeners() {
8377 // 监听会话过期事件
8478 useEffect ( ( ) => {
8579 const handleSessionExpired = ( event : CustomEvent ) => {
86- // 修复:在首页不处理会话过期事件
87- if ( pathname === '/' || pathname ?. startsWith ( '/?' ) ||
88- pathname ?. startsWith ( '/zh' ) || pathname ?. startsWith ( '/en' ) ) {
89- return ;
90- }
91-
9280 // 直接调用封装函数
9381 showSessionExpiredModal ( ) ;
9482 } ;
@@ -106,19 +94,14 @@ export function SessionListeners() {
10694 // 组件初次挂载时,如果发现本地已经没有 session,也立即弹窗
10795 useEffect ( ( ) => {
10896 if ( typeof window !== 'undefined' ) {
109- const localSession = localStorage . getItem ( 'session' ) ;
110- // 修复:只在非首页且没有session时才弹窗
111- if ( ! localSession && pathname &&
112- pathname !== '/' &&
113- ! pathname . startsWith ( '/?' ) &&
114- ! pathname . startsWith ( '/zh' ) &&
115- ! pathname . startsWith ( '/en' ) ) {
97+ const localSession = localStorage . getItem ( 'session' ) ;
98+ if ( ! localSession ) {
11699 showSessionExpiredModal ( ) ;
117100 }
118101 }
119102 // 该副作用只需在首次渲染时执行一次
120103 // eslint-disable-next-line react-hooks/exhaustive-deps
121- } , [ pathname ] ) ;
104+ } , [ ] ) ;
122105
123106 // 会话状态检查
124107 useEffect ( ( ) => {
@@ -127,8 +110,7 @@ export function SessionListeners() {
127110 try {
128111 // 尝试获取当前会话
129112 const session = await authService . getSession ( ) ;
130- // 修复:只在chat路径且没有session时才触发过期事件
131- if ( ! session && pathname ?. startsWith ( '/chat' ) ) {
113+ if ( ! session ) {
132114 window . dispatchEvent ( new CustomEvent ( EVENTS . SESSION_EXPIRED , {
133115 detail : { message : "登录已过期,请重新登录" }
134116 } ) ) ;
0 commit comments