AccountPopover.jsx 6.1 KB
Newer Older
wuhao's avatar
wuhao committed
1 2 3 4
import DraggableDialog from "@/components/DraggableDialog";
import InitForm from "@/components/InitForm";
import { doFetch } from "@/utils/doFetch";
import SettingsIcon from "@mui/icons-material/Settings";
wuhao's avatar
wuhao committed
5 6 7 8
import {
  Box,
  Divider,
  IconButton,
wuhao's avatar
wuhao committed
9
  MenuItem,
wuhao's avatar
wuhao committed
10
  Popover,
wuhao's avatar
wuhao committed
11 12
  Stack,
  Typography,
wuhao's avatar
wuhao committed
13
} from "@mui/material";
wuhao's avatar
wuhao committed
14 15 16 17 18 19 20 21 22
import { useModel, useNavigate } from "@umijs/max";
import { useRequest } from "ahooks";
import AES from "crypto-js/aes";
import Utf8 from "crypto-js/enc-utf8";
import ECB from "crypto-js/mode-ecb";
import Pkcs7 from "crypto-js/pad-pkcs7";
import dayjs from "dayjs";
import { useState } from "react";
import { message } from 'antd';
wuhao's avatar
wuhao committed
23 24 25 26 27

// ----------------------------------------------------------------------

const MENU_OPTIONS = [
  {
wuhao's avatar
wuhao committed
28 29
    label: "修改密码",
    type: "pwd",
wuhao's avatar
wuhao committed
30 31
  },
  {
wuhao's avatar
wuhao committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    label: "个人信息",
    type: "info",
  },
];

const columnes = [
  {
    title: "旧密码",
    dataIndex: "password",
    key: "password",
    valueType: "password",
    colProps: {
      span: 24,
    },
    formItemProps: {
      rules: [
        {
          required: true,
          message: "此项为必填项",
        },
      ],
    },
wuhao's avatar
wuhao committed
54 55
  },
  {
wuhao's avatar
wuhao committed
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
    title: "新密码",
    dataIndex: "newPassword",
    key: "newPassword",
    valueType: "password",
    colProps: {
      span: 24,
    },
    formItemProps: {
      rules: [
        {
          required: true,
          message: "此项为必填项",
        },
      ],
    },
  },
  {
    title: "确认新密码",
    dataIndex: "confirmNewPassword",
    key: "confirmNewPassword",
    valueType: "password",
    colProps: {
      span: 24,
    },
    formItemProps: {
      dependencies: ["newPassword"],
      rules: [
        {
          required: true,
          message: "此项为必填项",
        },
        ({ getFieldValue }) => ({
          validator(_, value) {
            if (!value || getFieldValue("newPassword") === value) {
              return Promise.resolve();
            }
            return Promise.reject(new Error("两次密码不一致!"));
          },
        }),
      ],
    },
wuhao's avatar
wuhao committed
97 98 99 100 101
  },
];

export default function AccountPopover() {
  const [open, setOpen] = useState(null);
wuhao's avatar
wuhao committed
102 103 104 105 106 107 108 109 110 111

  const [dialogprops, setdialogprops] = useState({
    open: false,
  });

  const {
    initialState: { currentUser },
    setInitialState,
  } = useModel("@@initialState");

wuhao's avatar
wuhao committed
112 113 114 115 116 117 118 119
  const navigate = useNavigate();

  const handleOpen = (event) => {
    setOpen(event.currentTarget);
  };

  const handleClose = (path) => {
    setOpen(null);
wuhao's avatar
wuhao committed
120 121 122 123 124 125 126 127
    if (path === "/user/login") {
      doFetch({ url: "/system/logout", params: {} }).then((res) => {
        if (res?.code === "0000") {
          path && navigate(path);
        }
      });
      return;
    }
wuhao's avatar
wuhao committed
128 129
    path && navigate(path);
  };
wuhao's avatar
wuhao committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
  const handleClosed = () => {
    setdialogprops({
      open: false,
    });
  };

  const { runAsync, loading } = useRequest(doFetch, {
    manual: true,
    onSuccess: (res, parames) => {
      if (res?.code == "0000") {
        handleClosed();
        handleClose("/user/login");
        message.success("操作成功");
      }
    },
  });
wuhao's avatar
wuhao committed
146 147 148

  return (
    <>
wuhao's avatar
wuhao committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
      <DraggableDialog
        handleClose={handleClosed}
        loading={loading}
        dialogprops={dialogprops}
      >
        <InitForm
          fields={columnes}
          onFinish={(val, extra) => {
            const { password, newPassword } = val;
            let timestamp = dayjs().valueOf().toString() + "acb";
            let newtimestamp = AES.encrypt(
              timestamp,
              Utf8.parse("NANGAODEAESKEY--"),
              {
                mode: ECB,
                padding: Pkcs7,
              }
            ).toString();
            let passwordsrc = AES.encrypt(password, Utf8.parse(timestamp), {
              mode: ECB,
              padding: Pkcs7,
            }).toString();
            let newPasswordsrc = AES.encrypt(
              newPassword,
              Utf8.parse(timestamp),
              {
                mode: ECB,
                padding: Pkcs7,
              }
            ).toString();

            let postdata = {
              encryptKey: newtimestamp,
              password: passwordsrc,
              newPassword: newPasswordsrc,
            };
            runAsync({
              url: "/system/updatePassword",
              params: postdata,
            });
          }}
        ></InitForm>
      </DraggableDialog>
      <IconButton onClick={handleOpen}>
wuhao's avatar
wuhao committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
        <SettingsIcon></SettingsIcon>
      </IconButton>

      <Popover
        open={Boolean(open)}
        anchorEl={open}
        onClose={handleClose}
        anchorOrigin={{ vertical: "bottom", horizontal: "right" }}
        transformOrigin={{ vertical: "top", horizontal: "right" }}
        PaperProps={{
          sx: {
            p: 0,
            mt: 1.5,
            ml: 0.75,
            width: 180,
            "& .MuiMenuItem-root": {
              typography: "body2",
              borderRadius: 0.75,
            },
          },
        }}
      >
        <Box sx={{ my: 1.5, px: 2.5 }}>
          <Typography variant="subtitle2" noWrap>
wuhao's avatar
wuhao committed
217
            {currentUser?.name}
wuhao's avatar
wuhao committed
218 219
          </Typography>
          <Typography variant="body2" sx={{ color: "text.secondary" }} noWrap>
wuhao's avatar
wuhao committed
220
            {currentUser?.email}
wuhao's avatar
wuhao committed
221 222 223 224 225 226 227
          </Typography>
        </Box>

        <Divider sx={{ borderStyle: "dashed" }} />

        <Stack sx={{ p: 1 }}>
          {MENU_OPTIONS.map((option) => (
wuhao's avatar
wuhao committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
            <MenuItem
              key={option.label}
              onClick={() => {
                if (option.type === "pwd") {
                  setOpen(null);
                  setdialogprops((s) => ({
                    ...s,
                    open: true,
                    title: "修改密码",
                  }));
                } else {
                  handleClose("/work/usercenter");
                }
              }}
            >
wuhao's avatar
wuhao committed
243 244 245 246 247 248 249 250 251
              {option.label}
            </MenuItem>
          ))}
        </Stack>

        <Divider sx={{ borderStyle: "dashed" }} />

        <MenuItem
          onClick={() => {
wuhao's avatar
wuhao committed
252
            handleClose("/user/login");
wuhao's avatar
wuhao committed
253 254 255
          }}
          sx={{ m: 1 }}
        >
wuhao's avatar
wuhao committed
256
          退出登录
wuhao's avatar
wuhao committed
257 258 259 260 261
        </MenuItem>
      </Popover>
    </>
  );
}