chore: remove patch client routes

This commit is contained in:
jialin
2024-06-24 09:35:11 +08:00
parent 9e471eafa7
commit de96c3d81d
6 changed files with 70 additions and 52 deletions
+1 -1
View File
@@ -103,7 +103,7 @@ export default defineConfig({
antd: true, antd: true,
baseNavigator: true, baseNavigator: true,
baseSeparator: '-', baseSeparator: '-',
default: 'zh-CN', default: 'en-US',
title: false, title: false,
useLocalStorage: true useLocalStorage: true
}, },
+16 -15
View File
@@ -1,4 +1,4 @@
import { Navigate, RequestConfig, history } from '@umijs/max'; import { 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';
@@ -40,21 +40,22 @@ export async function getInitialState() {
}; };
} }
export const patchClientRoutes = async (params: { routes: any[] }) => { // export const patchClientRoutes = async (params: { routes: any[] }) => {
const { routes } = params; // const { routes } = params;
const data = await queryCurrentUserState({ // console.log('routes============999', routes);
skipErrorHandler: true // const data = await queryCurrentUserState({
}); // skipErrorHandler: true
// });
routes.unshift({ // routes.unshift({
path: '/', // path: '/',
element: data?.is_admin ? ( // element: data?.is_admin ? (
<Navigate to="/dashboard" replace /> // <Navigate to="/dashboard" replace />
) : ( // ) : (
<Navigate to="/playground" replace /> // <Navigate to="/playground" replace />
) // )
}); // });
}; // };
export const request: RequestConfig = { export const request: RequestConfig = {
baseURL: ' /v1', baseURL: ' /v1',
+4
View File
@@ -10,6 +10,10 @@
margin-left: 5px; margin-left: 5px;
} }
.m-l-8 {
margin-left: 8px;
}
.flex { .flex {
display: flex; display: flex;
} }
+8 -7
View File
@@ -52,23 +52,23 @@ const filterRoutes = (
}; };
// 格式化路由 处理因 wrapper 导致的 菜单 path 不一致 // 格式化路由 处理因 wrapper 导致的 菜单 path 不一致
const mapRoutes = (routes: IRoute[]) => { const mapRoutes = (routes: IRoute[], role: string) => {
if (routes.length === 0) { if (routes.length === 0) {
return []; return [];
} }
return routes.map((route) => { return routes.map((route) => {
// 需要 copy 一份, 否则会污染原始数据 // 需要 copy 一份, 否则会污染原始数据
const newRoute = { ...route }; const newRoute = { ...route, role };
if (route.originPath) { if (route.originPath) {
newRoute.path = route.originPath; newRoute.path = route.originPath;
} }
if (Array.isArray(route.routes)) { if (Array.isArray(route.routes)) {
newRoute.routes = mapRoutes(route.routes); newRoute.routes = mapRoutes(route.routes, role);
} }
if (Array.isArray(route.children)) { if (Array.isArray(route.children)) {
newRoute.children = mapRoutes(route.children); newRoute.children = mapRoutes(route.children, role);
} }
return newRoute; return newRoute;
@@ -117,8 +117,9 @@ export default (props: any) => {
} }
); );
console.log('clientRoutes===========', clientRoutes, newRoutes); const role = initialState?.currentUser?.is_admin ? 'admin' : 'user';
const [route] = useAccessMarkedRoutes(mapRoutes(newRoutes)); const [route] = useAccessMarkedRoutes(mapRoutes(newRoutes, role));
console.log('clientRoutes===========', route, clientRoutes, newRoutes);
patchRoutes({ patchRoutes({
routes: route.children, routes: route.children,
@@ -150,7 +151,7 @@ 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 === '/') { } else if (location.pathname === '/') {
const pathname = initialState?.currentUser?.is_admin const pathname = initialState?.currentUser?.is_admin
? '/dashboard' ? '/dashboard'
: '/playground'; : '/playground';
+33 -27
View File
@@ -6,7 +6,7 @@ import {
LogoutOutlined, LogoutOutlined,
SettingOutlined SettingOutlined
} from '@ant-design/icons'; } from '@ant-design/icons';
import { history } from '@umijs/max'; import { SelectLang, history } from '@umijs/max';
import { Avatar, Dropdown, Menu, Spin, version } from 'antd'; import { Avatar, Dropdown, Menu, Spin, version } from 'antd';
export function getRightRenderContent(opts: { export function getRightRenderContent(opts: {
@@ -56,6 +56,21 @@ export function getRightRenderContent(opts: {
); );
} }
const renderExtraActions = () => {
return (
<SelectLang
style={{ padding: '10px 18px', marginBottom: '20px' }}
reload={false}
icon={
<span style={{ fontSize: '12px' }}>
<GlobalOutlined />
<span className="m-l-8"></span>
</span>
}
></SelectLang>
);
};
// 如果没有打开Locale,并且头像为空就取消掉这个返回的内容 // 如果没有打开Locale,并且头像为空就取消掉这个返回的内容
if (!avatar) return null; if (!avatar) return null;
@@ -87,18 +102,6 @@ export function getRightRenderContent(opts: {
// console.log('theme'); // console.log('theme');
// } // }
// }, // },
{
key: 'lang',
label: (
<>
<GlobalOutlined />
</>
),
onClick: () => {
console.log('lang');
}
},
{ {
key: 'logout', key: 'logout',
label: ( label: (
@@ -136,20 +139,23 @@ export function getRightRenderContent(opts: {
} }
return ( return (
<div className="umi-plugin-layout-right anticon"> <div style={{ width: '100%' }}>
{opts.runtimeConfig.logout ? ( {renderExtraActions()}
<> <div className="umi-plugin-layout-right anticon">
<Dropdown {opts.runtimeConfig.logout ? (
{...dropdownProps} <>
overlayClassName="umi-plugin-layout-container" <Dropdown
> {...dropdownProps}
{avatar} overlayClassName="umi-plugin-layout-container"
</Dropdown> >
<span></span> {avatar}
</> </Dropdown>
) : ( <span></span>
avatar </>
)} ) : (
avatar
)}
</div>
</div> </div>
); );
} }
+8 -2
View File
@@ -27,6 +27,10 @@ const Login = () => {
const [form] = Form.useForm(); const [form] = Form.useForm();
const gotoDefaultPage = (userInfo: any) => {
const pathname = userInfo?.is_admin ? '/dashboard' : '/playground';
history.push(pathname);
};
const fetchUserInfo = async () => { const fetchUserInfo = async () => {
const userInfo = await initialState?.fetchUserInfo?.(); const userInfo = await initialState?.fetchUserInfo?.();
@@ -38,6 +42,7 @@ const Login = () => {
})); }));
}); });
} }
return userInfo;
}; };
const handleLogin = async (values: any) => { const handleLogin = async (values: any) => {
@@ -47,8 +52,9 @@ const Login = () => {
username: values.username, username: values.username,
password: values.password password: values.password
}); });
await fetchUserInfo(); const userInfo = await fetchUserInfo();
history.push('/');
gotoDefaultPage(userInfo);
} catch (error) { } catch (error) {
console.log('error====', error); console.log('error====', error);
} }