/*
 * @Author: wuhao930406 1148547900@qq.com
 * @Date: 2023-05-17 16:16:06
 * @LastEditors: wuhao930406 1148547900@qq.com
 * @LastEditTime: 2023-08-18 11:26:10
 * @FilePath: /cs_vsofpm/src/app.jsx
 * @Description:
 *
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved.
 */
/* eslint-disable @typescript-eslint/no-unused-vars */
import { history } from "@umijs/max";
import { doFetch } from "./utils/doFetch";
const loginPath = "/user/login";

/**
 * @see  https://umijs.org/zh-CN/plugins/plugin-initial-state
 * */
export async function getInitialState() {
  let token = localStorage.getItem('TOKENES');
  const fetchUserInfo = async () => {
    try {
      const msg = await doFetch({
        url: "/system/me",
      });
      localStorage.setItem("ID", msg?.data?.data?.id);
      return msg.data.data;
    } catch (error) {
      history.push(loginPath);
    }
    return undefined;
  };
  // 如果不是登录页面，执行
  const { location } = history;
  if (!location.pathname.includes("user") && token) {
    const currentUser = await fetchUserInfo();
    localStorage.setItem("ID", currentUser?.id);
    return {
      fetchUserInfo,
      currentUser,
      activeUserIdList: [],
      vs: false,
      nav: 280,
      menuNum: {},
      message: {
        open: false,
        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,
  };
}

