添加认证来接,修改tabbug

This commit is contained in:
linxd
2025-07-14 15:58:05 +08:00
parent aa8349d549
commit 60ccbf659c
5 changed files with 59 additions and 9 deletions

View File

@ -20,6 +20,7 @@ export default [
path: '/',
component: '@/layouts/BasicLayout',
flatMenu: true,
wrappers: ['@/wrappers/auth'],
routes: [
{
path: '/',

24
src/hooks/useUser.ts Normal file
View File

@ -0,0 +1,24 @@
import { useState } from "react";
// 获取用户信息 这里没用dva, 因为包装组件写法复杂(当前框架),尤其是有form表单组件时
export const useUser = () => {
const [user, setUser] = useState<any>({
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 };
};

View File

@ -96,7 +96,15 @@ 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;
// 判断关闭后是否还有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);
@ -104,6 +112,7 @@ const TabModel: TabModelType = {
history.push(targetTab.path);
}
}
}
yield put({
type: 'updateState',
@ -149,7 +158,7 @@ const TabModel: TabModelType = {
// 需要排除的路由
const excludeRoutes = ['/login', '/register'];
// 首页不做特殊处理
// 首页特殊处理
if (pathname === '/') {
dispatch({
type: 'updateState',

View File

@ -299,12 +299,12 @@ const QuestionModal: React.FC<QuestionModalProps> = ({
/>
</Form.Item>
<div className={styles.questionSettings}>
<Form.Item name="isPublished" valuePropName="checked">
<div>
<Form.Item name="isPublished" valuePropName="checked" label="是否发布">
<Switch checkedChildren={intl.formatMessage({ id: 'questionModal.publishSwitch' })} />
</Form.Item>
<Form.Item name="isTop" valuePropName="checked">
<Form.Item name="isTop" valuePropName="checked" label="是否置顶">
<Switch checkedChildren={intl.formatMessage({ id: 'questionModal.topSwitch' })} />
</Form.Item>
</div>

16
src/wrappers/auth.tsx Normal file
View File

@ -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 <div>{props.children}</div>;
} else {
// 提示后跳转
message.error('请先登录');
return <Redirect to="/login" />;
}
};