Welcome.jsx 4.33 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
import { useModel,history } from "@umijs/max";
import { useEffect, useState } from "react";
// @mui
import { Box, ListItemIcon, Typography } from "@mui/material";
// mock
import { styled } from "@mui/material/styles";
// components
import navConfig from "@/layouts/dashboard/nav/config";
import { doFetch } from "@/utils/doFetch";
import { useRequest } from "ahooks";
import { Badge, Card, Divider } from "antd";

const StyledNavItemIcon = styled(ListItemIcon)({
  width: 48,
  height: 48,
  color: "inherit",
  display: "flex",
  alignItems: "center",
  justifyContent: "center",
  color:"#448ee2"
});

export default function Welcome() {
  const [navConfigs, setNavConfigs] = useState(navConfig);

  const {
    initialState: { nav, currentUser, menuNum },
    setInitialState,
  } = useModel("@@initialState");
wuhao's avatar
wuhao committed
30

wuhao's avatar
wuhao committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
  const { data } = useRequest(async () => {
    let res = await doFetch({ url: "/system/message", params: {} });
    let result = {};
    res?.data?.dataList?.map((it) => {
      result[it?.menu] = it?.count;
    });
    return result;
  });

  useEffect(() => {
    if (!data) {
      return;
    }
    setNavConfigs(() => {
      let news = navConfig?.map((it) => {
        return {
          ...it,
          children: it?.children?.map((item) => {
            const info =
              menuNum?.[item?.key ?? ""] === 0
                ? {}
                : menuNum?.[item?.key ?? ""]
                ? {
                    info: (
                      <div
                        style={{
                          marginRight: 12,
                          backgroundColor: "#ff4800",
                          width: 20,
                          height: 20,
                          textAlign: "center",
                          lineHeight: "20px",
                          color: "#fff",
                          borderRadius: 12,
                          fontSize: 12,
                        }}
                      >
                        {menuNum[item?.key ?? ""]}
                      </div>
                    ),
                  }
                : data[item?.key ?? ""]
                ? {
                    info: (
                      <div
                        style={{
                          marginRight: 12,
                          backgroundColor: "#ff4800",
                          width: 20,
                          height: 20,
                          textAlign: "center",
                          lineHeight: "20px",
                          color: "#fff",
                          borderRadius: 12,
                          fontSize: 12,
                        }}
                      >
                        {data[item?.key]}
                      </div>
                    ),
                  }
                : {};
            return {
              ...item,
              ...info,
            };
          }),
        };
      });
      if (currentUser?.type == 1) {
        return news?.filter((it) => {
          return it?.title === "管理中心";
        });
      } else if (currentUser?.type == 2) {
        return news?.filter((it) => {
          return it?.title === "教学中心";
        });
      } else {
        return news?.filter((it) => {
          return it?.title === "学习中心";
        });
      }
    });
  }, [data, currentUser?.type, menuNum]);

  console.log(navConfigs, currentUser);

  return (
    <Box p={2}>
      <Typography component={"h1"} align="center" fontSize={24}>
        你好<b style={{ fontSize: 24, padding: 8 }}>{currentUser?.name}</b>{" "}
        欢迎使用
      </Typography>
      <Divider style={{marginTop:48}}></Divider>
      <div style={{ display: "flex", gap: 16, justifyContent: "center",marginTop:60,flexWrap:"wrap" }}>
        {navConfigs?.[0]?.children?.map?.((it) => {
          return (
            <Badge>
              <Card hoverable className="center" style={{width:240}} onClick={()=>{
                history.push(it?.path)
              }}>
                <div className="center" style={{ padding: "20px 0",gap:12 }}>
                  <StyledNavItemIcon>{it?.icon}</StyledNavItemIcon>
                  <Typography align="center" fontSize={18}>{it?.title}</Typography>
                  <div style={{ width: 20, height: 20}}>{it?.info}</div>
                </div>
              </Card>
            </Badge>
          );
        })}
      </div>
    </Box>
  );
}