fix: redirect default path after login

This commit is contained in:
jialin
2024-06-23 21:30:20 +08:00
parent ff1cf01edf
commit 9e471eafa7
7 changed files with 67 additions and 38 deletions
+8 -8
View File
@@ -1,17 +1,16 @@
export default [ export default [
{ // {
path: '/', // path: '/',
key: 'dashboard', // key: 'root',
layout: false, // icon: 'home',
icon: 'home', // redirect: ''
redirect: '/dashboard', // },
access: 'canLogin'
},
{ {
name: 'Dashboard', name: 'Dashboard',
path: '/dashboard', path: '/dashboard',
key: 'dashboard', key: 'dashboard',
icon: 'home', icon: 'home',
access: 'canSeeAdmin',
component: './dashboard' component: './dashboard'
}, },
{ {
@@ -48,6 +47,7 @@ export default [
path: '/users', path: '/users',
key: 'users', key: 'users',
icon: 'Team', icon: 'Team',
access: 'canSeeAdmin',
component: './users' component: './users'
}, },
{ {
+4 -2
View File
@@ -1,8 +1,10 @@
export default (initialState: API.UserInfo) => { export default (initialState: { currentUser?: Global.UserInfo }) => {
// 在这里按照初始化数据定义项目中的权限,统一管理 // 在这里按照初始化数据定义项目中的权限,统一管理
// 参考文档 https://umijs.org/docs/max/access // 参考文档 https://umijs.org/docs/max/access
const canSeeAdmin = !!( const canSeeAdmin = !!(
initialState && initialState.name !== 'dontHaveAccess' initialState &&
initialState.currentUser &&
initialState.currentUser.is_admin
); );
return { return {
canSeeAdmin, canSeeAdmin,
+22 -1
View File
@@ -1,8 +1,10 @@
import { RequestConfig, history } from '@umijs/max'; import { Navigate, RequestConfig, history } from '@umijs/max';
import { requestConfig } from './request-config'; import { requestConfig } from './request-config';
import { queryCurrentUserState } from './services/profile/apis'; import { queryCurrentUserState } from './services/profile/apis';
const loginPath = '/login'; const loginPath = '/login';
let currentUserInfo: any = {};
// 运行时配置 // 运行时配置
// 全局初始化数据配置,用于 Layout 用户信息和权限初始化 // 全局初始化数据配置,用于 Layout 用户信息和权限初始化
@@ -25,6 +27,9 @@ export async function getInitialState() {
if (![loginPath].includes(location.pathname)) { if (![loginPath].includes(location.pathname)) {
const userInfo = await fetchUserInfo(); const userInfo = await fetchUserInfo();
currentUserInfo = {
...userInfo
};
return { return {
fetchUserInfo, fetchUserInfo,
currentUser: userInfo 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 = { export const request: RequestConfig = {
baseURL: ' /v1', baseURL: ' /v1',
...requestConfig ...requestConfig
+7
View File
@@ -13,4 +13,11 @@ declare namespace Global {
perPage: number; perPage: number;
}; };
} }
interface UserInfo {
username: string;
is_admin: boolean;
full_name: string;
id: number;
}
} }
+12 -17
View File
@@ -96,15 +96,6 @@ export default (props: any) => {
const formatMessage = undefined; const formatMessage = undefined;
// const runtimeConfig = pluginManager.applyPlugins({
// key: 'layout',
// type: 'modify',
// logout: true,
// initialValue: {
// ...initialInfo,
// notFound: <div>not found</div>
// }
// });
const runtimeConfig = { const runtimeConfig = {
...initialInfo, ...initialInfo,
logout: async (userInfo) => { logout: async (userInfo) => {
@@ -114,12 +105,6 @@ export default (props: any) => {
}, },
notFound: <span>404 not found</span> notFound: <span>404 not found</span>
}; };
console.log('clientRoute==========2=', {
props,
clientRoutes,
runtimeConfig,
initialInfo
});
// 现在的 layout 及 wrapper 实现是通过父路由的形式实现的, 会导致路由数据多了冗余层级, proLayout 消费时, 无法正确展示菜单, 这里对冗余数据进行过滤操作 // 现在的 layout 及 wrapper 实现是通过父路由的形式实现的, 会导致路由数据多了冗余层级, proLayout 消费时, 无法正确展示菜单, 这里对冗余数据进行过滤操作
const newRoutes = filterRoutes( const newRoutes = filterRoutes(
@@ -131,15 +116,20 @@ export default (props: any) => {
); );
} }
); );
console.log('clientRoutes===========', clientRoutes, newRoutes); console.log('clientRoutes===========', clientRoutes, newRoutes);
const [route] = useAccessMarkedRoutes(mapRoutes(newRoutes)); const [route] = useAccessMarkedRoutes(mapRoutes(newRoutes));
patchRoutes({ routes: route?.children || [] });
patchRoutes({
routes: route.children,
initialState: initialInfo.initialState
});
const matchedRoute = useMemo( const matchedRoute = useMemo(
() => matchRoutes(route?.children || [], location.pathname)?.pop?.()?.route, () => matchRoutes(route?.children || [], location.pathname)?.pop?.()?.route,
[location.pathname] [location.pathname]
); );
console.log('route===========', route); console.log('route===========', matchedRoute, route);
return ( return (
<div> <div>
<div className="background"></div> <div className="background"></div>
@@ -160,6 +150,11 @@ export default (props: any) => {
// 如果没有登录,重定向到 login // 如果没有登录,重定向到 login
if (!initialState?.currentUser && location.pathname !== loginPath) { if (!initialState?.currentUser && location.pathname !== loginPath) {
history.push(loginPath); history.push(loginPath);
} else if (location.path === '/') {
const pathname = initialState?.currentUser?.is_admin
? '/dashboard'
: '/playground';
history.push(pathname);
} }
}} }}
formatMessage={userConfig.formatMessage || formatMessage} formatMessage={userConfig.formatMessage || formatMessage}
+13 -10
View File
@@ -7,30 +7,33 @@ import icons from './icons';
function formatIcon(name: string) { function formatIcon(name: string) {
return name return name
.replace(name[0], name[0].toUpperCase()) .replace(name[0], name[0].toUpperCase())
.replace(/-(w)/g, function(all, letter) { .replace(/-(w)/g, function (all, letter) {
return letter.toUpperCase(); return letter.toUpperCase();
}); });
} }
export function patchRoutes({ routes }) { export function patchRoutes({ routes, initialState }) {
console.log('patchRoutes====',routes) console.log('patchRoutes99999', routes);
Object.keys(routes).forEach(key => { Object.keys(routes).forEach((key) => {
const { icon } = routes[key]; const { icon } = routes[key];
if (icon && typeof icon === 'string') { if (icon && typeof icon === 'string') {
const upperIcon = formatIcon(icon); const upperIcon = formatIcon(icon);
if (icons[upperIcon] || icons[upperIcon + 'Outlined']) { 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); const upperIcon = formatIcon(icon);
console.log('upperIcon',upperIcon) console.log('upperIcon', upperIcon);
if (icons[upperIcon] || icons[upperIcon + 'Outlined']) { if (icons[upperIcon] || icons[upperIcon + 'Outlined']) {
return React.createElement(icons[upperIcon] || icons[upperIcon + 'Outlined']); return React.createElement(
icons[upperIcon] || icons[upperIcon + 'Outlined']
);
} }
return null; return null;
}; }
+1
View File
@@ -117,6 +117,7 @@ const Models: React.FC = () => {
} else { } else {
await createUser({ data: params }); await createUser({ data: params });
} }
fetchData();
setOpenAddModal(false); setOpenAddModal(false);
message.success('successfully!'); message.success('successfully!');
} catch (error) { } catch (error) {