index.jsx 10.2 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 30 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 145 146 147 148 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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
import { doFetch } from "@/utils/doFetch";
import { ProDescriptions } from "@ant-design/pro-components";
import CheckIcon from "@mui/icons-material/Check";
import CloseIcon from "@mui/icons-material/Close";
import EditIcon from "@mui/icons-material/Edit";
import EmailIcon from "@mui/icons-material/Email";
import PhoneIcon from "@mui/icons-material/Phone";
import PriorityHighIcon from "@mui/icons-material/PriorityHigh";
import {
  Box,
  Card,
  Container,
  Stack,
  Tooltip,
  Typography,
} from "@mui/material";
import IconButton from "@mui/material/IconButton";
import { useModel } from "@umijs/max";
import * as Antd from "antd";
import { useState } from "react";
import Fade from "react-reveal/Fade";
import Head from "./head";
import "./index.less";

const { Col, Row, Input } = Antd;

function isValidChinesePhoneNumber(phoneNumber) {
  const regEx = /^1[3-9]\d{9}$/;
  return regEx.test(phoneNumber);
}

function isChinaEmail(email) {
  const regExp = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
  return regExp.test(email);
}

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

  const dofetchUserInfo = async () => {
    const userInfo = await fetchUserInfo();
    if (userInfo) {
      await setInitialState((s) => {
        return { ...s, currentUser: userInfo };
      });
    }
  };

  const [edit, setedit] = useState(false);
  const [telephone, settelephone] = useState(currentUser?.telephone);
  const [email, setemail] = useState(currentUser?.email);

  return (
    <Container maxWidth={false} className="diystyles">
      <Box
        display={"flex"}
        justifyContent={"space-between"}
        alignItems={"center"}
        sx={{ mb: 2.5 }}
        mt={0}
      >
        <Typography variant="h5">个人信息</Typography>
      </Box>
      <Row gutter={20} wrap={false}>
        <Col flex="420px">
          <Card
            style={{
              height: 488,
              flexDirection: "column",
              display: "flex",
              alignItems: "center",
              paddingTop: "18%",
            }}
          >
            <Head
              defaultImg={currentUser?.picUrl}
              dofetchUserInfo={dofetchUserInfo}
            ></Head>
            <Typography variant="span" align="center" mt={1} mb={3} color={"#999"}>
              支持的格式 *.jpeg, *.jpg, *.png, *.gif <br /> 文件不大于 2 MB
            </Typography>
            <div style={{ height: 48, position: "relative", width: 192 }}>
              <div style={{ position: "absolute", top: 0, left: 0 }}>
                <Fade top when={!edit}>
                  <>
                    <Stack
                      direction={"row"}
                      alignItems={"center"}
                      justifyContent={"center"}
                      gap={1}
                    >
                      <PhoneIcon style={{ fontSize: 16 }}></PhoneIcon>
                      <Typography>
                        {currentUser?.telephone || "未填写手机号"}
                      </Typography>
                    </Stack>
                    <Stack
                      justifyContent={"center"}
                      direction={"row"}
                      alignItems={"center"}
                      gap={1}
                      mt={1}
                    >
                      <EmailIcon style={{ fontSize: 16 }}></EmailIcon>
                      <Typography>
                        {currentUser?.email || "未填写邮箱"}
                      </Typography>
                    </Stack>
                  </>
                </Fade>
              </div>
              <div style={{ position: "absolute", top: 0 }}>
                <Fade top when={edit}>
                  <>
                    <Input
                      placeholder="请输入手机号"
                      allowClear
                      prefix={
                        <PhoneIcon
                          style={{
                            fontSize: 14,
                            color: !isValidChinesePhoneNumber(telephone)
                              ? "red"
                              : "#333",
                          }}
                        />
                      }
                      value={telephone}
                      onChange={(e) => {
                        settelephone(e.target.value);
                      }}
                    ></Input>
                    <Input
                      placeholder="请输入邮箱"
                      allowClear
                      prefix={
                        <EmailIcon
                          style={{
                            fontSize: 14,
                            color: !isChinaEmail(email) ? "red" : "#333",
                          }}
                        />
                      }
                      value={email}
                      onChange={(e) => {
                        setemail(e.target.value);
                      }}
                      style={{ marginTop: 12 }}
                    ></Input>
                  </>
                </Fade>
              </div>

              <div
                style={{
                  position: "absolute",
                  bottom: -72,
                  right: 0,
                  left: 0,
                  margin: "auto",
                  width: "100%",
                  height: 32,
                  zIndex: !edit ? 9 : -1,
                }}
                className="center"
              >
                <Fade bottom when={!edit}>
                  <Tooltip title="编辑">
                    <IconButton
                      sx={{ backgroundColor: "rgba(0,0,0,0.1)" }}
                      onClick={() => {
                        setedit(true);
                        settelephone(currentUser?.telephone);
                        setemail(currentUser?.email);
                      }}
                    >
                      <EditIcon
                        style={{
                          fontSize: 18,
                          cursor: "pointer",
                          color: "#3d87df",
                        }}
                      ></EditIcon>
                    </IconButton>
                  </Tooltip>
                </Fade>
              </div>

              <div
                style={{
                  position: "absolute",
                  bottom: -88,
                  right: 0,
                  left: 0,
                  margin: "auto",
                  width: "100%",
                  height: 32,
                }}
                className="center"
              >
                <Fade bottom when={edit}>
                  <Stack direction={"row"} gap={2}>
                    <Tooltip title="取消">
                      <IconButton
                        onClick={() => {
                          setedit(false);
                        }}
                        sx={{ backgroundColor: "rgba(0,0,0,0.05)" }}
                      >
                        <CloseIcon
                          style={{
                            fontSize: 18,
                            cursor: "pointer",
                            color: "#000",
                            fontWeight: "blod",
                          }}
                        ></CloseIcon>
                      </IconButton>
                    </Tooltip>
                    <Tooltip
                      title={
                        !isValidChinesePhoneNumber(telephone)
                          ? "手机号格式错误"
                          : !isChinaEmail(email)
                          ? "邮箱格式错误"
                          : "提交"
                      }
                    >
                      <IconButton
                        style={{ boxShadow: " 0 0 12px rgba(0,0,0,0.2)" }}
                        onClick={async () => {
                          let res = await doFetch({
                            url: "/user/updateTelephoneAndEmail",
                            params: { telephone, email },
                          });
                          if (res?.code === "0000") {
                            setedit(false);
                            await dofetchUserInfo();
                          }
                        }}
                      >
                        {isValidChinesePhoneNumber(telephone) &&
                        isChinaEmail(email) ? (
                          <CheckIcon
                            style={{
                              fontSize: 18,
                              cursor: "pointer",
                              color: "#3d87df",
                              fontWeight: "blod",
                            }}
                          ></CheckIcon>
                        ) : (
                          <PriorityHighIcon
                            style={{
                              fontSize: 18,
                              cursor: "pointer",
                              color: "red",
                              fontWeight: "blod",
                            }}
                          ></PriorityHighIcon>
                        )}
                      </IconButton>
                    </Tooltip>
                  </Stack>
                </Fade>
              </div>
            </div>
          </Card>
        </Col>
        <Col flex={1}>
          <Card style={{ height: 488, flexDirection: "column", padding: 24 }}>
            <ProDescriptions
              column={1}
              columns={[
                { title: "用户类型", dataIndex: "typeName", key: "typeName" },
                {
                  title: "用户账号",
                  dataIndex: "userAccount",
                  key: "userAccount",
                },
                // { title: "邮箱", dataIndex: "email", key: "email" },
                { title: "学校", dataIndex: "schoolName", key: "schoolName" },
                {
                  title: "院系",
                  dataIndex: "departmentName",
                  key: "departmentName",
                },
                { title: "班级", dataIndex: "className", key: "className" },
              ]}
              dataSource={currentUser}
            />
          </Card>
        </Col>
      </Row>
    </Container>
  );
}

export default Usercenter;