欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 时评 > 【Umi】项目初始化配置和用户权限

【Umi】项目初始化配置和用户权限

2025/5/21 4:47:12 来源:https://blog.csdn.net/XiugongHao/article/details/148009021  浏览:    关键词:【Umi】项目初始化配置和用户权限

app.tsx

import { RunTimeLayoutConfig } from '@umijs/max';
import { history, RequestConfig } from 'umi';
import { getCurrentUser } from './services/auth';
import { message } from 'antd';// 获取用户信息
export async function getInitialState(): Promise<{currentUser?: API.CurrentUser;fetchUserInfo?: () => Promise<API.CurrentUser | undefined>;
}> {const fetchUserInfo = async () => {try {const token = localStorage.getItem('token');if (!token) {return undefined;}const currentUser = await getCurrentUser();return currentUser;} catch (error) {history.push('/login');return undefined;}};// 如果不是登录页面,执行const { location } = history;if (location.pathname !== '/login' && location.pathname !== '/register') {const currentUser = await fetchUserInfo();return {currentUser,fetchUserInfo,};}return {fetchUserInfo,};
}// 布局配置
export const layout: RunTimeLayoutConfig = ({ initialState }) => {return {logo: 'https://img.ixintu.com/download/jpg/20200909/a27a8cb1fb9d6dbaf2bbaff07b6e4800_512_512.jpg!bg',menu: {locale: false,},rightContentRender: () => <div></div>,avatarProps: {src: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',title: initialState?.currentUser?.name || initialState?.currentUser?.username,render: (_, avatarChildren) => {return <div>{avatarChildren}</div>;},},footerRender: () => <div style={{ textAlign: 'center', padding: '20px' }}>霹雳舞比赛管理系统 ©2025</div>,};
};// 请求配置
export const request: RequestConfig = {timeout: 10000,errorConfig: {adaptor: (resData: any) => {return {...resData,success: resData.success,errorMessage: resData.message,};},},requestInterceptors: [(config: any) => {const token = localStorage.getItem('token');if (token) {config.headers['Authorization'] = `Bearer ${token}`;}return config;},],responseInterceptors: [(response) => {return response;},],
};

access.ts

export default function access(initialState: { currentUser?: API.CurrentUser }) {const { currentUser } = initialState || {};return {isAdmin: currentUser && currentUser.role === 'admin',isJudge: currentUser && currentUser.role === 'judge',isCompetitor: currentUser && currentUser.role === 'competitor',isStaff: currentUser && currentUser.role === 'staff',};
} 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词