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,28 +107,47 @@ class EmailForm extends React.Component { ...@@ -74,28 +107,47 @@ class EmailForm extends React.Component {
type: 'password', type: 'password',
}, },
}; };
const { inCountdown, countdown } = this.state;
return ( return (
<Form onSubmit={this.onSubmit}> <div>
<FormItem {...emailProps.fromItem}> {inCountdown ? (
{getFieldDecorator('email', { ...emailProps.decorator })( <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', height: '100vh' }}>
<Input <div style={{ flex: 9 / 10, textAlign: 'center' }}>
{...emailProps.input} <h3>{ChangeSuccessText}</h3>
onBlur={() => dispatch({ type: 'auth/checkEmail', payload: { ...form.getFieldsValue(), user_id: id } })} <h3>
/>, {loggingOutInText}
)} <span style={{ color: '#4da9ee', fontSize: '2em' }}>&nbsp;{`${countdown}`}</span>
</FormItem> <span>&nbsp;{secondsText}</span>
</h3>
<FormItem {...passwordProps.fromItem}> <br/>
{getFieldDecorator('password', { ...passwordProps.decorator })( <Button type="link" onClick={this.handleLogoutNow}>
<Input {...passwordProps.input} />, {logoutNowText}
)} </Button>
</FormItem> </div>
</div>
<FormItem> ) : (
<SubmitButton /> <Form onSubmit={this.onSubmit}>
</FormItem> <FormItem {...emailProps.fromItem}>
</Form> {getFieldDecorator('email', { ...emailProps.decorator })(
<Input
{...emailProps.input}
onBlur={() => dispatch({ type: 'auth/checkEmail', payload: { ...form.getFieldsValue(), user_id: id } })}
/>,
)}
</FormItem>
<FormItem {...passwordProps.fromItem}>
{getFieldDecorator('password', { ...passwordProps.decorator })(
<Input {...passwordProps.input} />,
)}
</FormItem>
<FormItem>
<SubmitButton />
</FormItem>
</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,31 +138,50 @@ class EmailForm extends React.Component { ...@@ -100,31 +138,50 @@ class EmailForm extends React.Component {
type: 'password', type: 'password',
}, },
}; };
const { inCountdown, countdown } = this.state;
return ( return (
<Form onSubmit={this.onSubmit}> <div>
<FormItem {...passwordProps.fromItem} label={messages['old-password']}> {inCountdown ? (
{getFieldDecorator('password')( <div style={{display: 'flex', flexDirection: 'column', alignItems: 'center', height: '100vh'}}>
<Input {...passwordProps.input} />, <div style={{flex: 9 / 10, textAlign: 'center'}}>
)} <h3>{ChangeSuccessText}</h3>
</FormItem> <h3>
{loggingOutInText}
<FormItem {...passwordProps.fromItem} label={messages['new-password']}> <span style={{ color: '#4da9ee', fontSize: '2em'}}>&nbsp;{`${countdown}`}</span>
{getFieldDecorator('new_password', { ...passwordProps.decorator })( <span>&nbsp;{secondsText}</span>
<Input {...passwordProps.input2} />, </h3>
)} <br/>
</FormItem> <Button type="link" onClick={this.handleLogoutNow}>
{logoutNowText}
<FormItem {...confirmProps.fromItem}> </Button>
{getFieldDecorator('confirm', { ...confirmProps.decorator })( </div>
<Input {...confirmProps.input} />, </div>
)} ) : (
</FormItem> <Form onSubmit={this.onSubmit}>
<FormItem {...passwordProps.fromItem} label={messages['old-password']}>
<FormItem> {getFieldDecorator('password')(
<SubmitButton /> <Input {...passwordProps.input} />,
</FormItem> )}
</Form> </FormItem>
<FormItem {...passwordProps.fromItem} label={messages['new-password']}>
{getFieldDecorator('new_password', { ...passwordProps.decorator })(
<Input {...passwordProps.input2} />,
)}
</FormItem>
<FormItem {...confirmProps.fromItem}>
{getFieldDecorator('confirm', { ...confirmProps.decorator })(
<Input {...confirmProps.input} />,
)}
</FormItem>
<FormItem>
<SubmitButton />
</FormItem>
</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;
const { username, password } = values; dispatch({type: 'user/updateAccount', payload: {username, password, user_id: id}});
this.setState({countdown: 5, inCountdown: true});
dispatch({ type: 'user/updateAccount', payload: { username, password, user_id: id } }); 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;
};
render() { handleLogoutNow = () => {
const { form, dispatch, user, checkUsername, isUserNameExists, messages } = this.props; this.setState({countdown: 0}, () => {
const { getFieldDecorator } = form; this.handleLogout();
const { id, username } = user; });
};
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();
}
};
checkConfirm = (rule, value, callback) => {
const form = this.props.form;
if (value) {
form.validateFields(['confirm'], {force: true});
}
callback();
};
render() {
const {form, dispatch, user, checkUsername, isUserNameExists, messages} = this.props;
const {getFieldDecorator} = form;
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 (
<Form onSubmit={this.onSubmit}> <div>
<FormItem {...usernameProps.fromItem}> {inCountdown ? (
{getFieldDecorator('username', { ...usernameProps.decorator })( <div style={{display: 'flex', flexDirection: 'column', alignItems: 'center', height: '100vh'}}>
<Input {...usernameProps.input} disabled/>, <div style={{flex: 9 / 10, textAlign: 'center'}}>
)} <h3>{ChangeSuccessText}</h3>
{ <h3>
<div class="alert alert-warning" role="alert">改名已关闭。下次将在101日开启。</div> {loggingOutInText}
} <span style={{color: '#4da9ee', fontSize: '2em'}}>&nbsp;{`${countdown}`}</span>
<span>&nbsp;{secondsText}</span>
</FormItem> </h3>
<br/>
<FormItem {...passwordProps.fromItem}> <Button type="link" onClick={this.handleLogoutNow}>
{getFieldDecorator('password', { ...passwordProps.decorator })( {logoutNowText}
<Input {...passwordProps.input} />, </Button>
)} </div>
</FormItem> </div>
) : (
<FormItem> <Form onSubmit={this.onSubmit}>
<SubmitButton /> <FormItem {...usernameProps.fromItem}>
</FormItem> {getFieldDecorator('username', {...usernameProps.decorator})(
</Form> <Input {...usernameProps.input} disabled/>,
)}
{
<div class="alert alert-warning" role="alert">{temporaryPromptText}</div>
}
</FormItem>
<FormItem {...passwordProps.fromItem}>
{getFieldDecorator('password', {...passwordProps.decorator})(
<Input {...passwordProps.input} />,
)}
</FormItem>
<FormItem>
<SubmitButton/>
</FormItem>
</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,
......
...@@ -134,8 +134,8 @@ class Profiles extends React.Component { ...@@ -134,8 +134,8 @@ class Profiles extends React.Component {
{isUpload && <Format id="Reselect-Avatar"/>} {isUpload && <Format id="Reselect-Avatar"/>}
<input <input
type="file" onChange={this.onGetFile} ref={(file) => { type="file" onChange={this.onGetFile} ref={(file) => {
this.file = file; this.file = file;
}} style={{ display: 'none' }} }} style={{ display: 'none' }}
/> />
</label> </label>
</Button> </Button>
......
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