123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- import { AvatarDropdown, AvatarName, Question, SelectLang } from '@/components';
- import { LinkOutlined } from '@ant-design/icons';
- import type { Settings as LayoutSettings } from '@ant-design/pro-components';
- import { SettingDrawer } from '@ant-design/pro-components';
- import { history, Link, RequestConfig, RunTimeLayoutConfig } from '@umijs/max';
- import defaultSettings from '../config/defaultSettings';
- import { AxiosResponse } from 'axios';
- import { message } from 'antd';
- import { Response } from '@/core/network';
- import { treeFormatBySymmetry } from './utils/tree';
- import { getStaffMeApi } from './services/swagger/staffAdmin';
- const isDev = process.env.NODE_ENV === 'development';
- const loginPath = '/user/login';
- /**
- * @see https://umijs.org/zh-CN/plugins/plugin-initial-state
- * */
- export async function getInitialState(): Promise<{
- settings?: Partial<LayoutSettings>;
- currentUser?: SectApi.StaffVO;
- loading?: boolean;
- fetchUserInfo?: (...arg: any[]) => Promise<SectApi.StaffVO | undefined>;
- }> {
- const fetchUserInfo = async () => {
- try {
- const rs = await getStaffMeApi({
- skipErrorHandler: true
- });
- if (!rs.success) {
- message.warning(rs.errorMessage);
- history.push(loginPath);
- return undefined;
- }
- return rs.data
- } catch (error) {
- history.push(loginPath);
- }
- return undefined;
- };
- // 如果不是登录页面,执行
- const { location } = history;
- if (location.pathname !== loginPath) {
- const currentUser = await fetchUserInfo();
- return {
- fetchUserInfo,
- currentUser,
- settings: defaultSettings as Partial<LayoutSettings>,
- };
- }
- return {
- fetchUserInfo,
- settings: defaultSettings as Partial<LayoutSettings>,
- };
- }
- // ProLayout 支持的api https://procomponents.ant.design/components/layout
- export const layout: RunTimeLayoutConfig = ({ initialState, setInitialState }) => {
- return {
- actionsRender: () => [<Question key="doc" />, <SelectLang key="SelectLang" />],
- avatarProps: {
- src: initialState?.currentUser?.thumbAvatar,
- title: <AvatarName />,
- render: (_, avatarChildren) => {
- return <AvatarDropdown>{avatarChildren}</AvatarDropdown>;
- },
- },
- menu: {
- params:{
- },
- request: async (params, defaultMenuData) => {
- const authMenus = initialState?.currentUser?.menus ?? []
- const routes = treeFormatBySymmetry(defaultMenuData,authMenus, (treeNode, referTreeNode) => {
- const treeNodePath = treeNode.path?.toLocaleLowerCase()
- const referTreeNodePath = (referTreeNode?.url || '').toLocaleLowerCase()
- const isHasAuth = treeNodePath && referTreeNodePath && treeNodePath === referTreeNodePath
- if(isHasAuth) {
- return {
- ...treeNode,
- }
- }
- } )
- return routes
- },
- },
- // waterMarkProps: {
- // content: initialState?.currentUser?.name,
- // },
- //footerRender: () => <Footer />,
- onPageChange: () => {
- const { location } = history;
- // 如果没有登录,重定向到 login
- if (!initialState?.currentUser && location.pathname !== loginPath) {
- history.push(loginPath);
- }
- },
- bgLayoutImgList: [
- {
- src: 'https://mdn.alipayobjects.com/yuyan_qk0oxh/afts/img/D2LWSqNny4sAAAAAAAAAAAAAFl94AQBr',
- left: 85,
- bottom: 100,
- height: '303px',
- },
- {
- src: 'https://mdn.alipayobjects.com/yuyan_qk0oxh/afts/img/C2TWRpJpiC0AAAAAAAAAAAAAFl94AQBr',
- bottom: -68,
- right: -45,
- height: '303px',
- },
- {
- src: 'https://mdn.alipayobjects.com/yuyan_qk0oxh/afts/img/F6vSTbj8KpYAAAAAAAAAAAAAFl94AQBr',
- bottom: 0,
- left: 0,
- width: '331px',
- },
- ],
- links: isDev
- ? [
- <Link key="openapi" to="/umi/plugin/openapi" target="_blank">
- <LinkOutlined />
- <span>OpenAPI 文档</span>
- </Link>,
- ]
- : [],
- menuHeaderRender: undefined,
- // 自定义 403 页面
- // unAccessible: <div>unAccessible</div>,
- // 增加一个 loading 的状态
- childrenRender: (children) => {
- // if (initialState?.loading) return <PageLoading />;
- return (
- <>
- {children}
- {isDev && (
- <SettingDrawer
- disableUrlParams
- enableDarkTheme
- settings={initialState?.settings}
- onSettingChange={(settings) => {
- setInitialState((preInitialState) => ({
- ...preInitialState,
- settings,
- }));
- }}
- />
- )}
- </>
- );
- },
- ...initialState?.settings,
- };
- };
- /**
- * @name request 配置,可以配置错误处理
- * 它基于 axios 和 ahooks 的 useRequest 提供了一套统一的网络请求和错误处理方案。
- * @doc https://umijs.org/docs/max/request#配置
- *
- *
- */
- export const request: RequestConfig = {
- errorConfig: {},
- requestInterceptors: [
- (url, options) => {
- const token = localStorage.getItem('token');
- if (token) {
- options.headers = {
- ...options.headers,
- Authorization: token,
- };
- }
- return {
- url,
- options,
- };
- },
- ],
- responseInterceptors: [
- // @ts-ignore
- <T extends any>(response: AxiosResponse<Response<T>>) => {
- console.log('response', response);
- console.log('res', response.status);
- if (response.status === 200) {
- const data = response.data as any;
- if (data.code !== undefined && data.msg !== undefined) {
- if (data.code === 0) {
- response.data = {
- success: true,
- data: data.data,
- errorCode: data.code,
- errorMessage: data.msg,
- };
- } else {
- response.data = {
- success: false,
- data: data,
- errorCode: data.code,
- errorMessage: data.msg,
- };
- }
- } else {
- response.data = {
- success: false,
- errorCode: -1,
- errorMessage: '返回结构错误!',
- data: undefined as T,
- };
- }
- } else {
- response.data = {
- success: false,
- errorCode: response.status,
- errorMessage: response.statusText,
- data: undefined as T,
- };
- }
- return response;
- },
- ],
- };
|