Commit ed5a975d authored by 铃兰's avatar 铃兰

增加修改用户名/邮箱/密码的验证页面

parent 4bd1fd78
This diff is collapsed.
import { Form, Input } from 'antd'; import { Button, Form, Input } from 'antd';
import React from 'react'; import React from 'react';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import SubmitButton from './SubmitButton'; import SubmitButton from './SubmitButton';
...@@ -11,13 +11,15 @@ const formItemLayout = { ...@@ -11,13 +11,15 @@ const formItemLayout = {
class EmailForm extends React.Component { class EmailForm extends React.Component {
state = {
countdown: 0,
inCountdown: false, // Whether the countdown is active
};
// static contextTypes = { // static contextTypes = {
// intl: PropTypes.object.isRequired, // intl: PropTypes.object.isRequired,
// }; // };
onSubmit = (e) => { onSubmit = (e) => {
const { form, dispatch, user: { id } } = this.props; const { form, dispatch, user: { id } } = this.props;
if (e) { if (e) {
e.preventDefault(); e.preventDefault();
} }
...@@ -28,17 +30,48 @@ class EmailForm extends React.Component { ...@@ -28,17 +30,48 @@ class EmailForm extends React.Component {
const { email, password } = values; const { email, password } = values;
dispatch({ type: 'user/updateEmail', payload: { email, password, user_id: id } }); dispatch({ type: 'user/updateEmail', payload: { email, password, user_id: id } });
this.setState({ countdown: 5, inCountdown: true });
this.countdownTimer = setInterval(this.handleCountdown, 1000);
} }
}); });
}; };
handleCountdown = () => {
// 倒计时减1
this.setState(prevState => ({ countdown: prevState.countdown - 1 }));
if (this.state.countdown === 0) {
// 清除倒计时
clearInterval(this.countdownTimer);
// 设置倒计时状态为false
this.setState({ inCountdown: false });
// 倒计时结束后调用退出
this.handleLogout();
}
};
handleLogout = () => {
clearInterval(this.countdownTimer);
window.localStorage.removeItem('token');
if (window.ygopro) {
window.ygopro.logoutUser('YGOMobile萌卡已登出');
}
const url = new URL(window.location.href);
const redirect = url.searchParams.get('redirect') || '/';
window.location.href = redirect;
};
handleLogoutNow = () => {
this.setState({ countdown: 0 }, () => {
this.handleLogout();
});
};
render() { render() {
const { form, dispatch, user, checkEmail, isEmailExists, messages } = this.props; const { form, dispatch, user, checkEmail, isEmailExists, messages } = this.props;
const { getFieldDecorator } = form; const { getFieldDecorator } = form;
const { id, email } = user; const { id, email } = user;
// const { intl: { messages } } = this.context; // const { intl: { messages } } = this.context;
const logoutNowText = this.props.messages.LogoutNow;
const loggingOutInText = this.props.messages.LoggingOutIn;
const secondsText = this.props.messages.Seconds;
const ChangeSuccessText = this.props.messages.EmailChangeSuccess;
const emailProps = { const emailProps = {
fromItem: { fromItem: {
label: messages.email, label: messages.email,
...@@ -74,8 +107,25 @@ class EmailForm extends React.Component { ...@@ -74,8 +107,25 @@ class EmailForm extends React.Component {
type: 'password', type: 'password',
}, },
}; };
const { inCountdown, countdown } = this.state;
return ( return (
<div>
{inCountdown ? (
<div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', height: '100vh' }}>
<div style={{ flex: 9 / 10, textAlign: 'center' }}>
<h3>{ChangeSuccessText}</h3>
<h3>
{loggingOutInText}
<span style={{ color: '#4da9ee', fontSize: '2em' }}>&nbsp;{`${countdown}`}</span>
<span>&nbsp;{secondsText}</span>
</h3>
<br/>
<Button type="link" onClick={this.handleLogoutNow}>
{logoutNowText}
</Button>
</div>
</div>
) : (
<Form onSubmit={this.onSubmit}> <Form onSubmit={this.onSubmit}>
<FormItem {...emailProps.fromItem}> <FormItem {...emailProps.fromItem}>
{getFieldDecorator('email', { ...emailProps.decorator })( {getFieldDecorator('email', { ...emailProps.decorator })(
...@@ -96,6 +146,8 @@ class EmailForm extends React.Component { ...@@ -96,6 +146,8 @@ class EmailForm extends React.Component {
<SubmitButton /> <SubmitButton />
</FormItem> </FormItem>
</Form> </Form>
)}
</div>
); );
} }
} }
......
import { Form, Input } from 'antd'; import { Form, Input, Button } from 'antd';
import React from 'react'; import React from 'react';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import SubmitButton from './SubmitButton'; import SubmitButton from './SubmitButton';
const FormItem = Form.Item; const FormItem = Form.Item;
const formItemLayout = { const formItemLayout = {
labelCol: { span: 4 }, labelCol: { span: 4 },
wrapperCol: { span: 15 }, wrapperCol: { span: 15 },
...@@ -14,7 +12,10 @@ const formItemLayout = { ...@@ -14,7 +12,10 @@ const formItemLayout = {
class EmailForm extends React.Component { class EmailForm extends React.Component {
state = {
countdown: 0,
inCountdown: false, // 是否处于倒计时状态
};
onSubmit = (e) => { onSubmit = (e) => {
const { form, dispatch, user: { id } } = this.props; const { form, dispatch, user: { id } } = this.props;
...@@ -28,13 +29,47 @@ class EmailForm extends React.Component { ...@@ -28,13 +29,47 @@ class EmailForm extends React.Component {
const { new_password, password } = values; const { new_password, password } = values;
dispatch({ type: 'user/updateAccount', payload: { new_password, password, user_id: id } }); dispatch({ type: 'user/updateAccount', payload: { new_password, password, user_id: id } });
// 设置倒计时状态为5秒
this.setState({ countdown: 5, inCountdown: true });
// 启动倒计时
this.countdownTimer = setInterval(this.handleCountdown, 1000);
} }
}); });
}; };
handleCountdown = () => {
// 倒计时减1
this.setState((prevState) => ({ countdown: prevState.countdown - 1 }));
if (this.state.countdown === 0) {
// 清除倒计时
clearInterval(this.countdownTimer);
// 设置倒计时状态为false
this.setState({ inCountdown: false });
// 倒计时结束后调用退出
this.handleLogout();
}
};
handleLogout = () => {
clearInterval(this.countdownTimer);
window.localStorage.removeItem('token');
if (window.ygopro) {
window.ygopro.logoutUser('YGOMobile萌卡已登出');
}
const url = new URL(window.location.href);
const redirect = url.searchParams.get('redirect') || '/';
window.location.href = redirect;
};
handleLogoutNow = () => {
this.setState({ countdown: 0 }, () => {
this.handleLogout();
});
};
checkPassword = (rule, value, callback) => { checkPassword = (rule, value, callback) => {
const { form, messages } = this.props; const { form, messages } = this.props;
//const { intl: { messages } } = this.context; // const { intl: { messages } } = this.context;
if (value && value !== form.getFieldValue('new_password')) { if (value && value !== form.getFieldValue('new_password')) {
callback(messages['Incorrect-password.2']); callback(messages['Incorrect-password.2']);
} else { } else {
...@@ -54,7 +89,10 @@ class EmailForm extends React.Component { ...@@ -54,7 +89,10 @@ class EmailForm extends React.Component {
render() { render() {
const { form, messages } = this.props; const { form, messages } = this.props;
const { getFieldDecorator } = form; const { getFieldDecorator } = form;
const logoutNowText = this.props.messages.LogoutNow;
const loggingOutInText = this.props.messages.LoggingOutIn;
const secondsText = this.props.messages.Seconds;
const ChangeSuccessText = this.props.messages.ChangeSuccess;
const passwordProps = { const passwordProps = {
fromItem: { fromItem: {
label: messages['old-password'], label: messages['old-password'],
...@@ -100,8 +138,25 @@ class EmailForm extends React.Component { ...@@ -100,8 +138,25 @@ class EmailForm extends React.Component {
type: 'password', type: 'password',
}, },
}; };
const { inCountdown, countdown } = this.state;
return ( return (
<div>
{inCountdown ? (
<div style={{display: 'flex', flexDirection: 'column', alignItems: 'center', height: '100vh'}}>
<div style={{flex: 9 / 10, textAlign: 'center'}}>
<h3>{ChangeSuccessText}</h3>
<h3>
{loggingOutInText}
<span style={{ color: '#4da9ee', fontSize: '2em'}}>&nbsp;{`${countdown}`}</span>
<span>&nbsp;{secondsText}</span>
</h3>
<br/>
<Button type="link" onClick={this.handleLogoutNow}>
{logoutNowText}
</Button>
</div>
</div>
) : (
<Form onSubmit={this.onSubmit}> <Form onSubmit={this.onSubmit}>
<FormItem {...passwordProps.fromItem} label={messages['old-password']}> <FormItem {...passwordProps.fromItem} label={messages['old-password']}>
{getFieldDecorator('password')( {getFieldDecorator('password')(
...@@ -125,6 +180,8 @@ class EmailForm extends React.Component { ...@@ -125,6 +180,8 @@ class EmailForm extends React.Component {
<SubmitButton /> <SubmitButton />
</FormItem> </FormItem>
</Form> </Form>
)}
</div>
); );
} }
} }
...@@ -135,6 +192,7 @@ function mapStateToProps(state) { ...@@ -135,6 +192,7 @@ function mapStateToProps(state) {
user: { user }, user: { user },
common: { messages }, common: { messages },
} = state; } = state;
return { return {
messages, messages,
user, user,
......
import { Form, Input } from 'antd'; import {Button, Form, Input} from 'antd';
import React from 'react'; import React from 'react';
import { connect } from 'react-redux'; import {connect} from 'react-redux';
import SubmitButton from './SubmitButton'; import SubmitButton from './SubmitButton';
const FormItem = Form.Item; const FormItem = Form.Item;
const formItemLayout = { const formItemLayout = {
labelCol: { span: 4 }, labelCol: {span: 4},
wrapperCol: { span: 15 }, wrapperCol: {span: 15},
}; };
class EmailForm extends React.Component { class EmailForm extends React.Component {
state = {
countdown: 0,
inCountdown: false, // Whether the countdown is active
};
onSubmit = (e) => { onSubmit = (e) => {
const { form, dispatch, user: { id } } = this.props; const {form, dispatch, user: {id}} = this.props;
if (e) { if (e) {
e.preventDefault(); e.preventDefault();
...@@ -21,19 +24,68 @@ class EmailForm extends React.Component { ...@@ -21,19 +24,68 @@ class EmailForm extends React.Component {
form.validateFieldsAndScroll((err, values) => { form.validateFieldsAndScroll((err, values) => {
if (!err) { if (!err) {
console.log('Received values of form: ', values); console.log('Received values of form: ', values);
const {username, password} = values;
dispatch({type: 'user/updateAccount', payload: {username, password, user_id: id}});
this.setState({countdown: 5, inCountdown: true});
this.countdownTimer = setInterval(this.handleCountdown, 1000);
}
});
};
handleCountdown = () => {
// 倒计时减1
this.setState((prevState) => ({countdown: prevState.countdown - 1}));
if (this.state.countdown === 0) {
// 清除倒计时
clearInterval(this.countdownTimer);
// 设置倒计时状态为false
this.setState({inCountdown: false});
// 倒计时结束后调用退出
this.handleLogout();
}
};
handleLogout = () => {
clearInterval(this.countdownTimer);
window.localStorage.removeItem('token');
if (window.ygopro) {
window.ygopro.logoutUser('YGOMobile萌卡已登出');
}
const url = new URL(window.location.href);
const redirect = url.searchParams.get('redirect') || '/';
window.location.href = redirect;
};
const { username, password } = values; handleLogoutNow = () => {
this.setState({countdown: 0}, () => {
this.handleLogout();
});
};
checkPassword = (rule, value, callback) => {
const {form, messages} = this.props;
// const { intl: { messages } } = this.context;
if (value && value !== form.getFieldValue('new_password')) {
callback(messages['Incorrect-password.2']);
} else {
callback();
}
};
dispatch({ type: 'user/updateAccount', payload: { username, password, user_id: id } }); checkConfirm = (rule, value, callback) => {
const form = this.props.form;
if (value) {
form.validateFields(['confirm'], {force: true});
} }
}); callback();
}; };
render() { render() {
const { form, dispatch, user, checkUsername, isUserNameExists, messages } = this.props; const {form, dispatch, user, checkUsername, isUserNameExists, messages} = this.props;
const { getFieldDecorator } = form; const {getFieldDecorator} = form;
const { id, username } = user; const {id, username} = user;
const logoutNowText = this.props.messages.LogoutNow;
const loggingOutInText = this.props.messages.LoggingOutIn;
const secondsText = this.props.messages.Seconds;
const temporaryPromptText = this.props.messages.TemporaryPrompt;
const ChangeSuccessText = this.props.messages.ChangeSuccess;
const usernameProps = { const usernameProps = {
fromItem: { fromItem: {
label: messages.username, label: messages.username,
...@@ -47,7 +99,7 @@ class EmailForm extends React.Component { ...@@ -47,7 +99,7 @@ class EmailForm extends React.Component {
}, },
input: { input: {
placeholder: messages.username, placeholder: messages.username,
onBlur: () => dispatch({ type: 'auth/checkUsername', payload: { ...form.getFieldsValue(), user_id: id } }), onBlur: () => dispatch({type: 'auth/checkUsername', payload: {...form.getFieldsValue(), user_id: id}}),
}, },
}; };
...@@ -70,39 +122,57 @@ class EmailForm extends React.Component { ...@@ -70,39 +122,57 @@ class EmailForm extends React.Component {
type: 'password', type: 'password',
}, },
}; };
const {inCountdown, countdown} = this.state;
return ( return (
<div>
{inCountdown ? (
<div style={{display: 'flex', flexDirection: 'column', alignItems: 'center', height: '100vh'}}>
<div style={{flex: 9 / 10, textAlign: 'center'}}>
<h3>{ChangeSuccessText}</h3>
<h3>
{loggingOutInText}
<span style={{color: '#4da9ee', fontSize: '2em'}}>&nbsp;{`${countdown}`}</span>
<span>&nbsp;{secondsText}</span>
</h3>
<br/>
<Button type="link" onClick={this.handleLogoutNow}>
{logoutNowText}
</Button>
</div>
</div>
) : (
<Form onSubmit={this.onSubmit}> <Form onSubmit={this.onSubmit}>
<FormItem {...usernameProps.fromItem}> <FormItem {...usernameProps.fromItem}>
{getFieldDecorator('username', { ...usernameProps.decorator })( {getFieldDecorator('username', {...usernameProps.decorator})(
<Input {...usernameProps.input} disabled/>, <Input {...usernameProps.input} disabled/>,
)} )}
{ {
<div class="alert alert-warning" role="alert">改名已关闭。下次将在101日开启。</div> <div class="alert alert-warning" role="alert">{temporaryPromptText}</div>
} }
</FormItem> </FormItem>
<FormItem {...passwordProps.fromItem}> <FormItem {...passwordProps.fromItem}>
{getFieldDecorator('password', { ...passwordProps.decorator })( {getFieldDecorator('password', {...passwordProps.decorator})(
<Input {...passwordProps.input} />, <Input {...passwordProps.input} />,
)} )}
</FormItem> </FormItem>
<FormItem> <FormItem>
<SubmitButton /> <SubmitButton/>
</FormItem> </FormItem>
</Form> </Form>
)}
</div>
); );
} }
} }
function mapStateToProps(state) { function mapStateToProps(state) {
const { const {
user: { user }, user: {user},
auth: { isUserNameExists, checkUsername }, auth: {isUserNameExists, checkUsername},
common: { messages }, common: {messages},
} = state; } = state;
return { return {
user, user,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment