添加认证来接,修改tabbug
This commit is contained in:
@ -20,6 +20,7 @@ export default [
|
|||||||
path: '/',
|
path: '/',
|
||||||
component: '@/layouts/BasicLayout',
|
component: '@/layouts/BasicLayout',
|
||||||
flatMenu: true,
|
flatMenu: true,
|
||||||
|
wrappers: ['@/wrappers/auth'],
|
||||||
routes: [
|
routes: [
|
||||||
{
|
{
|
||||||
path: '/',
|
path: '/',
|
||||||
|
24
src/hooks/useUser.ts
Normal file
24
src/hooks/useUser.ts
Normal 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 };
|
||||||
|
};
|
@ -96,12 +96,21 @@ const TabModel: TabModelType = {
|
|||||||
let newActiveKey = activeKey;
|
let newActiveKey = activeKey;
|
||||||
if (key === activeKey) {
|
if (key === activeKey) {
|
||||||
const index = tabList.findIndex((item: TabItem) => item.key === key);
|
const index = tabList.findIndex((item: TabItem) => item.key === key);
|
||||||
newActiveKey = index === 0 ? (newTabList[0] || {}).key : tabList[index - 1].key;
|
|
||||||
|
|
||||||
// 切换路由
|
// 判断关闭后是否还有tab
|
||||||
const targetTab = newTabList.find((item: TabItem) => item.key === newActiveKey);
|
if (newTabList.length === 0) {
|
||||||
if (targetTab) {
|
// 没有剩余tab时,导航到首页
|
||||||
history.push(targetTab.path);
|
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'];
|
const excludeRoutes = ['/login', '/register'];
|
||||||
|
|
||||||
// 首页不做特殊处理
|
// 首页特殊处理
|
||||||
if (pathname === '/') {
|
if (pathname === '/') {
|
||||||
dispatch({
|
dispatch({
|
||||||
type: 'updateState',
|
type: 'updateState',
|
||||||
|
@ -299,12 +299,12 @@ const QuestionModal: React.FC<QuestionModalProps> = ({
|
|||||||
/>
|
/>
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
|
|
||||||
<div className={styles.questionSettings}>
|
<div>
|
||||||
<Form.Item name="isPublished" valuePropName="checked">
|
<Form.Item name="isPublished" valuePropName="checked" label="是否发布">
|
||||||
<Switch checkedChildren={intl.formatMessage({ id: 'questionModal.publishSwitch' })} />
|
<Switch checkedChildren={intl.formatMessage({ id: 'questionModal.publishSwitch' })} />
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
|
|
||||||
<Form.Item name="isTop" valuePropName="checked">
|
<Form.Item name="isTop" valuePropName="checked" label="是否置顶">
|
||||||
<Switch checkedChildren={intl.formatMessage({ id: 'questionModal.topSwitch' })} />
|
<Switch checkedChildren={intl.formatMessage({ id: 'questionModal.topSwitch' })} />
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
</div>
|
</div>
|
||||||
|
16
src/wrappers/auth.tsx
Normal file
16
src/wrappers/auth.tsx
Normal 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" />;
|
||||||
|
}
|
||||||
|
};
|
Reference in New Issue
Block a user