From 60ccbf659c53e3f586dad6a9f29db66d7b60b295 Mon Sep 17 00:00:00 2001 From: linxd <544554903@qq.com> Date: Mon, 14 Jul 2025 15:58:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AE=A4=E8=AF=81=E6=9D=A5?= =?UTF-8?q?=E6=8E=A5=EF=BC=8C=E4=BF=AE=E6=94=B9tabbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/router.config.ts | 1 + src/hooks/useUser.ts | 24 +++++++++++++++++++ src/models/tab.ts | 21 +++++++++++----- .../userQuestionManage/QuestionModal.tsx | 6 ++--- src/wrappers/auth.tsx | 16 +++++++++++++ 5 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 src/hooks/useUser.ts create mode 100644 src/wrappers/auth.tsx diff --git a/config/router.config.ts b/config/router.config.ts index d5b3716..26fd961 100644 --- a/config/router.config.ts +++ b/config/router.config.ts @@ -20,6 +20,7 @@ export default [ path: '/', component: '@/layouts/BasicLayout', flatMenu: true, + wrappers: ['@/wrappers/auth'], routes: [ { path: '/', diff --git a/src/hooks/useUser.ts b/src/hooks/useUser.ts new file mode 100644 index 0000000..52900df --- /dev/null +++ b/src/hooks/useUser.ts @@ -0,0 +1,24 @@ +import { useState } from "react"; + +// 获取用户信息 这里没用dva, 因为包装组件写法复杂(当前框架),尤其是有form表单组件时 +export const useUser = () => { + const [user, setUser] = useState({ + role: 'admin1', // 模拟用户权限 + }); + const getUserInfo = ()=>{ + return user; + } + const setUserInfo = (userInfo: any) => { + setUser(userInfo); + } + const getUserRole = ()=>{ + return user.role; + } + const setToken = (token: string) => { + sessionStorage.setItem('token', token); + } + const getToken = () => { + return sessionStorage.getItem('token'); + } + return { user, getUserInfo, setUserInfo, getUserRole, setToken, getToken }; +}; diff --git a/src/models/tab.ts b/src/models/tab.ts index 8588929..e64e989 100644 --- a/src/models/tab.ts +++ b/src/models/tab.ts @@ -96,12 +96,21 @@ const TabModel: TabModelType = { let newActiveKey = activeKey; if (key === activeKey) { const index = tabList.findIndex((item: TabItem) => item.key === key); - newActiveKey = index === 0 ? (newTabList[0] || {}).key : tabList[index - 1].key; - // 切换路由 - const targetTab = newTabList.find((item: TabItem) => item.key === newActiveKey); - if (targetTab) { - history.push(targetTab.path); + // 判断关闭后是否还有tab + if (newTabList.length === 0) { + // 没有剩余tab时,导航到首页 + history.push('/'); + newActiveKey = '/'; + } else { + // 有剩余tab时,选择适当的tab作为活动tab + newActiveKey = index === 0 ? newTabList[0].key : tabList[index - 1].key; + + // 切换路由 + const targetTab = newTabList.find((item: TabItem) => item.key === newActiveKey); + if (targetTab) { + history.push(targetTab.path); + } } } @@ -149,7 +158,7 @@ const TabModel: TabModelType = { // 需要排除的路由 const excludeRoutes = ['/login', '/register']; - // 首页不做特殊处理 + // 首页特殊处理 if (pathname === '/') { dispatch({ type: 'updateState', diff --git a/src/pages/userQuestionManage/QuestionModal.tsx b/src/pages/userQuestionManage/QuestionModal.tsx index ce3ad7e..ac063cd 100644 --- a/src/pages/userQuestionManage/QuestionModal.tsx +++ b/src/pages/userQuestionManage/QuestionModal.tsx @@ -299,12 +299,12 @@ const QuestionModal: React.FC = ({ /> -
- +
+ - +
diff --git a/src/wrappers/auth.tsx b/src/wrappers/auth.tsx new file mode 100644 index 0000000..f72eb76 --- /dev/null +++ b/src/wrappers/auth.tsx @@ -0,0 +1,16 @@ +import React from 'react'; +import { Redirect } from 'umi'; +import { message } from 'antd'; +import { useUser } from '@/hooks/useUser'; +// 权限校验 +export default (props: any) => { + const { getToken } = useUser(); + if (getToken()) { + + return
{props.children}
; + } else { + // 提示后跳转 + message.error('请先登录'); + return ; + } +};