fix: redirect default path after login

main
jialin 2 years ago
parent ff1cf01edf
commit 9e471eafa7

@ -1,17 +1,16 @@
export default [
{
path: '/',
key: 'dashboard',
layout: false,
icon: 'home',
redirect: '/dashboard',
access: 'canLogin'
},
// {
// path: '/',
// key: 'root',
// icon: 'home',
// redirect: ''
// },
{
name: 'Dashboard',
path: '/dashboard',
key: 'dashboard',
icon: 'home',
access: 'canSeeAdmin',
component: './dashboard'
},
{
@ -48,6 +47,7 @@ export default [
path: '/users',
key: 'users',
icon: 'Team',
access: 'canSeeAdmin',
component: './users'
},
{

@ -1,8 +1,10 @@
export default (initialState: API.UserInfo) => {
export default (initialState: { currentUser?: Global.UserInfo }) => {
// 在这里按照初始化数据定义项目中的权限,统一管理
// 参考文档 https://umijs.org/docs/max/access
const canSeeAdmin = !!(
initialState && initialState.name !== 'dontHaveAccess'
initialState &&
initialState.currentUser &&
initialState.currentUser.is_admin
);
return {
canSeeAdmin,

@ -1,8 +1,10 @@
import { RequestConfig, history } from '@umijs/max';
import { Navigate, RequestConfig, history } from '@umijs/max';
import { requestConfig } from './request-config';
import { queryCurrentUserState } from './services/profile/apis';
const loginPath = '/login';
let currentUserInfo: any = {};
// 运行时配置
// 全局初始化数据配置,用于 Layout 用户信息和权限初始化
@ -25,6 +27,9 @@ export async function getInitialState() {
if (![loginPath].includes(location.pathname)) {
const userInfo = await fetchUserInfo();
currentUserInfo = {
...userInfo
};
return {
fetchUserInfo,
currentUser: userInfo
@ -35,6 +40,22 @@ export async function getInitialState() {
};
}
export const patchClientRoutes = async (params: { routes: any[] }) => {
const { routes } = params;
const data = await queryCurrentUserState({
skipErrorHandler: true
});
routes.unshift({
path: '/',
element: data?.is_admin ? (
<Navigate to="/dashboard" replace />
) : (
<Navigate to="/playground" replace />
)
});
};
export const request: RequestConfig = {
baseURL: ' /v1',
...requestConfig

@ -13,4 +13,11 @@ declare namespace Global {
perPage: number;
};
}
interface UserInfo {
username: string;
is_admin: boolean;
full_name: string;
id: number;
}
}

@ -96,15 +96,6 @@ export default (props: any) => {
const formatMessage = undefined;
// const runtimeConfig = pluginManager.applyPlugins({
// key: 'layout',
// type: 'modify',
// logout: true,
// initialValue: {
// ...initialInfo,
// notFound: <div>not found</div>
// }
// });
const runtimeConfig = {
...initialInfo,
logout: async (userInfo) => {
@ -114,12 +105,6 @@ export default (props: any) => {
},
notFound: <span>404 not found</span>
};
console.log('clientRoute==========2=', {
props,
clientRoutes,
runtimeConfig,
initialInfo
});
// 现在的 layout 及 wrapper 实现是通过父路由的形式实现的, 会导致路由数据多了冗余层级, proLayout 消费时, 无法正确展示菜单, 这里对冗余数据进行过滤操作
const newRoutes = filterRoutes(
@ -131,15 +116,20 @@ export default (props: any) => {
);
}
);
console.log('clientRoutes===========', clientRoutes, newRoutes);
const [route] = useAccessMarkedRoutes(mapRoutes(newRoutes));
patchRoutes({ routes: route?.children || [] });
patchRoutes({
routes: route.children,
initialState: initialInfo.initialState
});
const matchedRoute = useMemo(
() => matchRoutes(route?.children || [], location.pathname)?.pop?.()?.route,
[location.pathname]
);
console.log('route===========', route);
console.log('route===========', matchedRoute, route);
return (
<div>
<div className="background"></div>
@ -160,6 +150,11 @@ export default (props: any) => {
// 如果没有登录,重定向到 login
if (!initialState?.currentUser && location.pathname !== loginPath) {
history.push(loginPath);
} else if (location.path === '/') {
const pathname = initialState?.currentUser?.is_admin
? '/dashboard'
: '/playground';
history.push(pathname);
}
}}
formatMessage={userConfig.formatMessage || formatMessage}

@ -7,30 +7,33 @@ import icons from './icons';
function formatIcon(name: string) {
return name
.replace(name[0], name[0].toUpperCase())
.replace(/-(w)/g, function(all, letter) {
.replace(/-(w)/g, function (all, letter) {
return letter.toUpperCase();
});
}
export function patchRoutes({ routes }) {
console.log('patchRoutes====',routes)
Object.keys(routes).forEach(key => {
export function patchRoutes({ routes, initialState }) {
console.log('patchRoutes99999', routes);
Object.keys(routes).forEach((key) => {
const { icon } = routes[key];
if (icon && typeof icon === 'string') {
const upperIcon = formatIcon(icon);
if (icons[upperIcon] || icons[upperIcon + 'Outlined']) {
routes[key].icon = React.createElement(icons[upperIcon] || icons[upperIcon + 'Outlined']);
routes[key].icon = React.createElement(
icons[upperIcon] || icons[upperIcon + 'Outlined']
);
}
}
});
}
export function renderMenuIcon (icon: string){
export function renderMenuIcon(icon: string) {
const upperIcon = formatIcon(icon);
console.log('upperIcon',upperIcon)
console.log('upperIcon', upperIcon);
if (icons[upperIcon] || icons[upperIcon + 'Outlined']) {
return React.createElement(icons[upperIcon] || icons[upperIcon + 'Outlined']);
return React.createElement(
icons[upperIcon] || icons[upperIcon + 'Outlined']
);
}
return null;
};
}

@ -117,6 +117,7 @@ const Models: React.FC = () => {
} else {
await createUser({ data: params });
}
fetchData();
setOpenAddModal(false);
message.success('successfully!');
} catch (error) {

Loading…
Cancel
Save