/** * 忘记密码-重置密码页面V2 * @邠心vbe on 2023/02/02 */ import React, { Component } from 'react'; import { View, StyleSheet, ScrollView, Image, TextInput, Pressable } from 'react-native'; import apiUser from '../../api/apiUser'; import { setAccessToken } from '../../api/http'; import Button from '../../components/Button'; import Dialog from '../../components/Dialog'; import { Styles } from '../../components/Toolbar'; import routeUtil from '../../utils/routeUtil'; import { getStorageJsonSync, setStorage, setStorageJson } from '../../utils/storage'; import { PageList } from '../Router'; import StrengthView from './StrengthView'; export default class ResetPassword extends Component { constructor(props) { super(props); this.StrengthView = StrengthView.V2 this.strengthColor = ["#F5F5F5", "#F7C4CD", "#F2F8AC", "#F8DBAC", "#ACF8F6", "#A6E782"] this.state = { strength: 0, password: '', wrongCount: true, sendMinutes: 0, confirmStatusColor: "#F5F5F5" }; this.formInfo = { email: '', password: '', verificationCode: '' } this.isChange = false; } componentDidMount() { const action = this.props.route?.params?.action ?? ""; if (action == "change") { this.isChange = true; /*setTimeout(() => { this.requestLogout(); }, 1000);*/ } } componentWillUnmount() { this.setState({ sendMinutes: 0 }) } getBackTopPosition() { return isIOS ? statusHeight - 18 : 12; } getConfirmStatusColor() { var color = "#F5F5F5"; if (this.formInfo.password) { if (this.state.password == this.formInfo.password) { color = "#A6E782"; } else { color = "#FA7B7B" } } this.setState({ confirmStatusColor: color }) } changeInfo(key, value) { this.formInfo[key] = value; if (key == "password") { this.getConfirmStatusColor(); } } applyStrength(text) { this.StrengthView.apply(text, strength => { if (this.state.strength != strength) { this.setState({ password: text, strength: strength }); } else { this.setState({ password: text }); } setTimeout(() => this.getConfirmStatusColor(), 300); }); } sendVerification() { var info = this.formInfo; if (!info.email) { toastShort('Please enter email address'); return; } if (!/^[a-zA-Z0-9]+[\S]+@[a-zA-Z0-9_-]+[\.][\Sa-zA-Z]+$/.test(info.email)) { toastShort('Email is incorrect format'); return; } Dialog.showProgressDialog() apiUser.sendVerificationCode(info.email).then(res => { Dialog.dismissLoading() this.state.sendMinutes = res.data?.resendTime ?? 60; toastShort('Send verification code successfully'); this.contdownTime(); }).catch(err => { Dialog.dismissLoading() toastShort(err); }); } contdownTime() { if (this.state.sendMinutes > 0) { this.setState({ sendMinutes: this.state.sendMinutes - 1 }) setTimeout(() => { this.contdownTime(); }, 1000); } } onResetPassword() { var info = this.formInfo; console.log('reset info', info); if (!info.email) { toastShort('Please enter email address'); return; } if (!/^[a-zA-Z0-9]+[\S]+@[a-zA-Z0-9_-]+[\.][\Sa-zA-Z]+$/.test(info.email)) { toastShort('Email is incorrect format'); return; } if (!this.state.password) { toastShort('Please enter password'); return; } if (this.state.strength < this.StrengthView.maxStrength) { toastShort('Password is not strong'); return; } if (!info.password) { toastShort('Please enter confirm password'); return; } if (info.password != this.state.password) { toastShort('The twice passwords are inconsistent'); return; } if (!info.verificationCode) { toastShort('Please enter verification code'); return; } Dialog.showProgressDialog() apiUser.updatePassword(this.formInfo).then(res => { Dialog.dismissLoading() toastShort('Reset password successfully'); if (this.isChange) { this.requestLogout(); } else { goBack(); } }).catch(err => { Dialog.dismissLoading() toastShort(err); }); } async requestLogout() { const data = await getStorageJsonSync('loginData'); if (data && data.email) { delete data.password setStorageJson('loginData', data); setStorage('RegisterTokenDate', ""); } global.userInfo = {} /*this.setState({ isLogin: false, userInfo: {} });*/ setAccessToken(''); routeUtil.bridge2Page(PageList.login); } getStrengthStyle() { const persent = ((this.StrengthView.maxStrength - this.state.strength) / this.StrengthView.maxStrength) * 100 return { left: 0, right: 0, height: 1, bottom: -1, marginRight: persent + "%", position: 'absolute', backgroundColor: "#A6E782" } } changeSecurety() { this.setState({ wrongCount: !this.state.wrongCount }) } render() { return ( {/* Reset Password */} {/* Email Address */} this.changeInfo('email', v)} /> {/* Verification Code */} this.changeInfo('verificationCode', v)} />