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

app.tsx

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

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',
  };
} 
相关推荐
AIGC魔法师几秒前
AI 极客低代码平台快速上手 --生成Python代码
开发语言·harmonyos·低代码平台·ai极客
熊猫钓鱼>_>14 分钟前
对话即编程:如何用 Trae 的 @智能体 5 分钟修复一个复杂 Bug?
开发语言·python·bug
搏博17 分钟前
WPS JS宏实现去掉文档中的所有空行
开发语言·javascript·wps
hie9889423 分钟前
MATLAB中进行语音信号分析
开发语言·matlab·语音识别
前进的程序员37 分钟前
C++经典库介绍
开发语言·c++
郭尘帅6661 小时前
Vue3 父子组件传值, 跨组件传值,传函数
前端·javascript·vue.js
(・Д・)ノ1 小时前
python打卡day31
开发语言·人工智能·python
北漂老男孩1 小时前
JavaScript 性能优化实战指南
开发语言·javascript·性能优化
charlee441 小时前
使用Vite创建一个动态网页的前端项目
前端·javascript·vite
yorushika_2 小时前
python打卡训练营打卡记录day31
开发语言·python·机器学习