【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',
  };
} 
相关推荐
C++ 老炮儿的技术栈9 分钟前
文本文件与二进制文件的区别
大数据·c语言·开发语言·c++·git·算法·visual studio
君鼎24 分钟前
C++标准库大全(STL)
开发语言·c++·stl
vvilkim1 小时前
Flutter 导航与路由管理:Navigator 的深入解析与实践
前端·javascript·flutter
小白探索世界欧耶!~1 小时前
react 使用 postcss-px-to-viewport 实现 px 自动转 vw 自适应
前端·javascript·vue.js·程序人生·react.js·postcss
ryipei2 小时前
vue纯前端根据页面或者后台数据,读取本地文档模板,填充数据后并导出
前端·javascript·vue.js
委婉待续2 小时前
Qt的学习(三)
开发语言·qt·学习
白总Server2 小时前
Golang实现分布式Masscan任务调度系统
java·运维·服务器·开发语言·分布式·后端·golang
leo03082 小时前
新一代python管理工具--uv
开发语言·python·uv
熊猫钓鱼>_>2 小时前
Python小工具开发实战:从零构建自动化文件管理器的心得与体悟
开发语言·python·自动化
老李笔记2 小时前
VUE element table 列合并
javascript·vue.js·ecmascript