newsmy_recorder/src/components/DynamicCodeForm.js

115 lines
3.8 KiB
JavaScript
Raw Normal View History

import { Container, TextField, InputAdornment, Link, Button } from "@mui/material";
2023-06-15 19:41:31 +08:00
import React, { useState, useEffect, useRef } from 'react';
2023-06-07 16:31:18 +08:00
import PhoneIphoneIcon from '@mui/icons-material/PhoneIphone';
import LockIcon from '@mui/icons-material/Lock';
2023-06-07 19:40:54 +08:00
import { useSelector, useDispatch } from 'react-redux'
2023-06-15 19:41:31 +08:00
import yzs from "../business/request.js";
import Agreement from "./Agreement.js";
import { validatePhoneNumber, textHintOfValidatePhoneNumber } from "../business/utilities.js"
2023-06-07 16:31:18 +08:00
2023-06-26 10:13:59 +08:00
export default function ({ udid, firstEnter, onChange, agreeAgreement, onAgreeChange }) {
2023-06-15 19:41:31 +08:00
const code = useRef(null);
const [seconds, setSeconds] = useState(0); // 倒计时
2023-06-07 19:40:54 +08:00
const account = useSelector(state => state.user.account)
const verificationCode = useSelector(state => state.user.verificationCode)
2023-06-07 16:31:18 +08:00
2023-06-15 19:41:31 +08:00
useEffect(() => {
const timer = window.setInterval(() => {
if (seconds > 0) {
setSeconds(seconds - 1);
} else {
code.current.disabled = false;
}
}, 1000);
return () => {
window.clearInterval(timer);
};
}, [seconds]);
const onClick = (event) => {
event.preventDefault();
code.current.disabled = true;
yzs.send_phone_code(udid, account)
setSeconds(60);
};
2023-06-07 16:31:18 +08:00
return <Container disableGutters={true}
sx={{
width: 300,
height: 200,
}}
>
<TextField
name="username"
2023-06-16 16:01:06 +08:00
autoComplete="username"
hiddenLabel
placeholder="请输入手机号码"
2023-06-07 16:31:18 +08:00
variant="outlined"
2023-06-07 19:40:54 +08:00
value={account}
2023-06-07 16:31:18 +08:00
color="primary"
size="small"
2023-06-07 16:31:18 +08:00
fullWidth
error={firstEnter ? false : !validatePhoneNumber(account)}
helperText={firstEnter ? "" : textHintOfValidatePhoneNumber(account)}
2023-06-26 10:13:59 +08:00
onChange={onChange}
2023-06-07 16:31:18 +08:00
InputProps={{
startAdornment: (
<InputAdornment position="start">
<PhoneIphoneIcon />
</InputAdornment>
),
}}
sx={{
minHeight: 64,
}}
2023-06-07 16:31:18 +08:00
/>
<TextField
sx={{
minHeight: 50,
}}
hiddenLabel
2023-06-07 16:31:18 +08:00
fullWidth
2023-06-26 10:13:59 +08:00
name="verification_code"
placeholder="请输入验证码"
type="text"
2023-06-16 16:01:06 +08:00
autoComplete="current-password"
2023-06-07 16:31:18 +08:00
variant="outlined"
size="small"
2023-06-07 19:40:54 +08:00
value={verificationCode}
2023-06-26 10:13:59 +08:00
onChange={onChange}
2023-06-07 16:31:18 +08:00
InputProps={{
startAdornment: (
<InputAdornment position="start">
<LockIcon />
</InputAdornment>
),
endAdornment: <InputAdornment position="end">
2023-06-15 19:41:31 +08:00
<Link ref={code}
color={seconds > 0 ? "#FFB3B3" : "red"}
component="button" underline="none" onClick={onClick}>{seconds > 0 ? `重新发送(${seconds})` : "发送动态码"}</Link>
2023-06-07 16:31:18 +08:00
</InputAdornment>
}}
/>
<Button
type="submit"
variant="contained"
color="primary"
fullWidth
disabled={!agreeAgreement}
2023-06-07 16:31:18 +08:00
sx={{
marginTop: 1.5,
2023-06-07 16:31:18 +08:00
backgroundColor: "#FF595A",
'&:hover': {
backgroundColor: '#FF595A',
},
'&:active': {
backgroundColor: '#FF595A',
},
}}
>
2023-06-26 10:13:59 +08:00
登录
2023-06-07 16:31:18 +08:00
</Button>
<Agreement agree={agreeAgreement} onChange={onAgreeChange} />
2023-06-07 16:31:18 +08:00
</Container>
}