app.jsx 1.76 KB
Newer Older
wuhao's avatar
wuhao committed
1 2 3 4
/*
 * @Author: wuhao930406 1148547900@qq.com
 * @Date: 2023-05-17 16:16:06
 * @LastEditors: wuhao930406 1148547900@qq.com
wuhao's avatar
wuhao committed
5
 * @LastEditTime: 2023-08-18 11:26:10
wuhao's avatar
wuhao committed
6 7 8 9 10
 * @FilePath: /cs_vsofpm/src/app.jsx
 * @Description:
 *
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved.
 */
wuhao's avatar
wuhao committed
11 12 13 14
/* eslint-disable @typescript-eslint/no-unused-vars */
import { history } from "@umijs/max";
import { doFetch } from "./utils/doFetch";
const loginPath = "/user/login";
wuhao's avatar
wuhao committed
15

wuhao's avatar
wuhao committed
16 17 18 19 20 21 22 23 24
/**
 * @see  https://umijs.org/zh-CN/plugins/plugin-initial-state
 * */
export async function getInitialState() {
  const fetchUserInfo = async () => {
    try {
      const msg = await doFetch({
        url: "/system/me",
      });
wuhao's avatar
wuhao committed
25
      localStorage.setItem("ID", msg?.data?.data?.id);
wuhao's avatar
wuhao committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
      return msg.data.data;
    } catch (error) {
      history.push(loginPath);
    }
    return undefined;
  };
  // 如果不是登录页面,执行
  const { location } = history;
  if (!location.pathname.includes("user")) {
    const currentUser = await fetchUserInfo();
    localStorage.setItem("ID", currentUser?.id);
    return {
      fetchUserInfo,
      currentUser,
      activeUserIdList: [],
      vs: false,
      nav: 280,
wuhao's avatar
wuhao committed
43
      menuNum: {},
wuhao's avatar
wuhao committed
44
      message: {
wuhao's avatar
wuhao committed
45
        open: false,
wuhao's avatar
wuhao committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
        snackbar: {
          autoHideDuration: 2000,
          anchorOrigin: {
            vertical: "bottom",
            horizontal: "right",
          },
        },
        type: "success",
        content: "",
      },
    };
  }
  return {
    fetchUserInfo,
    activeUserIdList: [],
    vs: false,
    message: {
      open: false,
      snackbar: {
        autoHideDuration: 2000,
        anchorOrigin: {
          vertical: "bottom",
          horizontal: "right",
        },
      },
      type: "success",
      content: "",
    },
    nav: 280,
wuhao's avatar
wuhao committed
75
  };
wuhao's avatar
wuhao committed
76
}
wuhao's avatar
wuhao committed
77