import React, { useState, useEffect } from "react"; import { Modal, Image, Form, message, Upload, Button } from "antd"; import styles from "./index.module.less"; import { user } from "../../api/index"; import { useDispatch } from "react-redux"; import { loginAction } from "../../store/user/loginUserSlice"; import type { UploadProps } from "antd"; import config from "../../js/config"; import { getToken } from "../../utils/index"; interface PropInterface { open: boolean; onCancel: () => void; } export const UserInfoModel: React.FC = ({ open, onCancel }) => { const dispatch = useDispatch(); const [form] = Form.useForm(); const [loading, setLoading] = useState(true); const [avatar, setAvatar] = useState(""); const [name, setName] = useState(""); const [idCard, setIdCard] = useState(""); useEffect(() => { getUser(); }, [form, open]); const getUser = () => { user.detail().then((res: any) => { setAvatar(res.data.user.avatar); setName(res.data.user.name); setIdCard(res.data.user.id_card); dispatch(loginAction(res.data)); }); }; const props: UploadProps = { name: "file", multiple: false, method: "PUT", action: config.app_url + "api/v1/user/avatar", headers: { Accept: "application/json", authorization: "Bearer " + getToken(), }, beforeUpload: (file) => { const isPNG = file.type === "image/png" || file.type === "image/jpg" || file.type === "image/jpeg"; if (!isPNG) { message.error(`${file.name}不是图片文件`); } const isLt2M = file.size / 1024 / 1024 < 2; if (!isLt2M) { message.error("超过2M限制,不允许上传"); } return (isPNG && isLt2M) || Upload.LIST_IGNORE; }, onChange(info: any) { const { status, response } = info.file; if (status === "done") { if (response.code === 0) { getUser(); } else { message.error(response.msg); } } else if (status === "error") { message.error(`${info.file.name} 上传失败`); } }, }; return ( <> onCancel()} maskClosable={false} footer={null} >
{avatar && ( )}
{name && (
{name}
)} {idCard && (
{idCard}
)}
); };