app.jsx 4.45 KB
Newer Older
wuhao's avatar
wuhao committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
import { SettingDrawer } from "@ant-design/pro-layout";
import { PageLoading } from "@ant-design/pro-layout";
import { history, Link } from "umi";
import RightContent from "@/components/RightContent";
import TagView from "@/components/TagView";
import SiderMenu from "@/components/SiderMenu";
import { queryCurrentUser, getMenu } from "./services/login";
import defaultSettings from "../config/defaultSettings";
import { message } from "antd";
const loginPath = "/user/login";
/** 获取用户信息比较慢的时候会展示一个 loading */

export const initialStateConfig = {
  loading: <PageLoading />,
};
/**
 * @see  https://umijs.org/zh-CN/plugins/plugin-initial-state
 * */

export async function getInitialState() {
  let token = localStorage.getItem("TOKEN_MES");
  const fetchUserInfo = async () => {
    try {
      const msg = await queryCurrentUser();
      return msg.data;
    } catch (error) {
      history.push(loginPath);
    }
    return undefined;
  }; // 如果是登录页面,不执行

  const getmenuData = async () => {
    try {
      const res = await getMenu();
      return res.data;
    } catch (error) {
      message.error(res.msg);
    }
    return undefined;
  }; // 如果是登录页面,不执行

  if (history.location.pathname !== loginPath && token) {
    const currentUserData = await fetchUserInfo();
    let menuData;
    if (currentUserData?.data?.userName) {
      menuData = await getmenuData();
    }
    return {
      fetchUserInfo,
      currentUser: currentUserData?.data,
      settings: defaultSettings,
      newMenu: menuData,
      getmenuData,
wuhao's avatar
wuhao committed
54
      collapsed: false
wuhao's avatar
wuhao committed
55 56 57 58 59 60
    };
  }
  return {
    fetchUserInfo,
    settings: defaultSettings,
    getmenuData,
wuhao's avatar
wuhao committed
61 62
    collapsed: false,
    tagList: []
wuhao's avatar
wuhao committed
63 64 65 66 67 68
  };
} // ProLayout 支持的api https://procomponents.ant.design/components/layout
export const layout = async (props) => {
  let { initialState, setInitialState } = props;
  let token = localStorage.getItem("TOKEN_MES");
  return {
wuhao's avatar
wuhao committed
69
    headerContentRender: () => <RightContent />,
wuhao's avatar
wuhao committed
70
    rightContentRender: () => null,
wuhao's avatar
wuhao committed
71 72 73 74
    disableContentMargin: false,
    waterMarkProps: {
      content: initialState?.currentUser?.name,
    },
wuhao's avatar
wuhao committed
75 76 77 78
    collapsed: initialState.collapsed,
    onCollapse: (cols) => {
      setInitialState(s => ({ ...s, collapsed: cols }))
    },
wuhao's avatar
wuhao committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    onPageChange: () => {
      const { location } = history; // 如果没有登录,重定向到 login
      if (location.pathname != "/user/logon") {
        if (
          (!initialState?.currentUser && location.pathname !== loginPath) ||
          !token
        ) {
          setInitialState((s) => {
            return { ...s, currentUser: undefined, newMenu: [] };
          });
          history.replace(loginPath);
        }
      }
      if (location.pathname == loginPath) {
        setInitialState((s) => {
          return { ...s, currentUser: undefined, newMenu: [] };
        });
      }
    },
    contentStyle: {
wuhao's avatar
wuhao committed
99
      padding: "42px 6px 0px 6px",
wuhao's avatar
wuhao committed
100 101 102 103 104 105 106 107
      margin: 0,
    },
    //接口获取菜单数据
    menu: {
      // 每当 initialState?.currentUser?.userid 发生修改时重新执行 request
      params: {
        userId: initialState?.currentUser?.id,
      },
左玲玲's avatar
左玲玲 committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
      request: (params, defaultMenuData) => {
        let lastArr = initialState?.newMenu?.userHavePermList
          ? JSON.parse(
            JSON.stringify(initialState?.newMenu?.userHavePermList)
          )
          : [],
          newArr = [
            {
              path: "/welcome",
              name: "首页",
              icon: "smile",
              component: "./Welcome",
              haveChildren: false,
              key: "000000",
              parentKey: "0",
              routes: [],
              children: null,
              title: null,
            },
          ].concat(lastArr);
        return newArr;
      },
wuhao's avatar
wuhao committed
130 131
      locale: false,
    },
wuhao's avatar
wuhao committed
132

左玲玲's avatar
左玲玲 committed
133 134 135 136 137 138 139
    menuRender: (props, defaultDom) => {
      if (props.isMobile) {
        return defaultDom;
      } else {
        return <SiderMenu {...props}></SiderMenu>;
      }
    },
wuhao's avatar
wuhao committed
140 141


wuhao's avatar
wuhao committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
    // 自定义 403 页面
    // unAccessible: <div>unAccessible</div>,
    // 增加一个 loading 的状态
    childrenRender: (children, props) => {
      // if (initialState?.loading) return <PageLoading />;
      return (
        <>
          {initialState?.currentUser && location.pathname !== loginPath ? (
            <TagView home="/welcome">{children}</TagView>
          ) : (
            children
          )}
        </>
      );
    },
    ...initialState?.settings,
  };
};