ResetPasswordV2.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /**
  2. * 忘记密码-重置密码页面V2
  3. * @邠心vbe on 2023/02/02
  4. */
  5. import React, { Component } from 'react';
  6. import { View, StyleSheet, ScrollView, Image, TextInput, Pressable } from 'react-native';
  7. import apiUser from '../../api/apiUser';
  8. import { setAccessToken } from '../../api/http';
  9. import Button from '../../components/Button';
  10. import Dialog from '../../components/Dialog';
  11. import { Styles } from '../../components/Toolbar';
  12. import routeUtil from '../../utils/routeUtil';
  13. import { getStorageJsonSync, setStorage, setStorageJson } from '../../utils/storage';
  14. import utils from '../../utils/utils';
  15. import { PageList } from '../Router';
  16. import StrengthView from './StrengthView';
  17. export default class ResetPassword extends Component {
  18. constructor(props) {
  19. super(props);
  20. this.StrengthView = StrengthView.V2
  21. this.strengthColor = ["#F5F5F5", "#F7C4CD", "#F2F8AC", "#F8DBAC", "#ACF8F6", "#A6E782"]
  22. this.state = {
  23. email: '',
  24. strength: 0,
  25. password: '',
  26. isChange: false,
  27. wrongCount: true,
  28. sendMinutes: 0,
  29. confirmStatusColor: "#F5F5F5"
  30. };
  31. this.formInfo = {
  32. email: '',
  33. password: '',
  34. verificationCode: ''
  35. }
  36. }
  37. componentDidMount() {
  38. const action = this.props.route?.params?.action ?? "";
  39. if (action == "change") {
  40. this.setState({
  41. isChange: true
  42. });
  43. const email = userInfo.email;
  44. this.formInfo.email = email;
  45. this.setState({
  46. email: email
  47. });
  48. }
  49. }
  50. componentWillUnmount() {
  51. this.setState({
  52. sendMinutes: 0
  53. })
  54. }
  55. getBackTopPosition() {
  56. return isIOS ? statusHeight - 18 : 12;
  57. }
  58. getConfirmStatusColor() {
  59. var color = "#F5F5F5";
  60. if (this.formInfo.password) {
  61. if (this.state.password == this.formInfo.password) {
  62. color = "#A6E782";
  63. } else {
  64. color = "#FA7B7B"
  65. }
  66. }
  67. this.setState({
  68. confirmStatusColor: color
  69. })
  70. }
  71. changeInfo(key, value) {
  72. this.formInfo[key] = value;
  73. if (key == "password") {
  74. this.getConfirmStatusColor();
  75. }
  76. }
  77. applyStrength(text) {
  78. this.StrengthView.apply(text, strength => {
  79. if (this.state.strength != strength) {
  80. this.setState({
  81. password: text,
  82. strength: strength
  83. });
  84. } else {
  85. this.setState({
  86. password: text
  87. });
  88. }
  89. setTimeout(() => this.getConfirmStatusColor(), 300);
  90. });
  91. }
  92. sendVerification() {
  93. var info = this.formInfo;
  94. if (!info.email) {
  95. toastShort($t('sign.plsInputEmail'));
  96. return;
  97. }
  98. if (!utils.isValidEmail(info.email)) {
  99. toastShort($t('sign.errEmailFormat'));
  100. return;
  101. }
  102. Dialog.showProgressDialog()
  103. apiUser.sendVerificationCode(info.email).then(res => {
  104. Dialog.dismissLoading()
  105. this.state.sendMinutes = res.data?.resendTime ?? 60;
  106. toastShort($t('sign.sendOTPSuccess'));
  107. this.contdownTime();
  108. }).catch(err => {
  109. Dialog.dismissLoading()
  110. toastShort(err);
  111. });
  112. }
  113. contdownTime() {
  114. if (this.state.sendMinutes > 0) {
  115. this.setState({
  116. sendMinutes: this.state.sendMinutes - 1
  117. })
  118. setTimeout(() => {
  119. this.contdownTime();
  120. }, 1000);
  121. }
  122. }
  123. onResetPassword() {
  124. var info = this.formInfo;
  125. console.log('reset info', info);
  126. if (!info.email) {
  127. toastShort($t('sign.plsInputEmail'));
  128. return;
  129. }
  130. if (!utils.isValidEmail(info.email)) {
  131. toastShort($t('sign.errEmailFormat'));
  132. return;
  133. }
  134. if (!info.verificationCode) {
  135. toastShort($t('sign.plsInputOTP'));
  136. return;
  137. }
  138. if (!this.state.password) {
  139. toastShort($t('sign.plsInputPassword'));
  140. return;
  141. }
  142. if (this.state.strength < this.StrengthView.maxStrength) {
  143. toastShort($t('sign.errPasswordStrong'));
  144. return;
  145. }
  146. if (!info.password) {
  147. toastShort($t('sign.plsInputPassword2'));
  148. return;
  149. }
  150. if (info.password != this.state.password) {
  151. toastShort($t('sign.errPasswordConfirm'));
  152. return;
  153. }
  154. Dialog.showProgressDialog()
  155. apiUser.updatePassword(this.formInfo).then(res => {
  156. Dialog.dismissLoading()
  157. toastShort($t('sign.resetPasswordSuccess'));
  158. if (this.state.isChange) {
  159. this.requestLogout();
  160. } else {
  161. goBack();
  162. }
  163. }).catch(err => {
  164. Dialog.dismissLoading()
  165. toastShort(err);
  166. });
  167. }
  168. async requestLogout() {
  169. const data = await getStorageJsonSync('loginData');
  170. if (data && data.email) {
  171. delete data.password
  172. setStorageJson('loginData', data);
  173. setStorage('RegisterTokenDate', "");
  174. }
  175. global.userInfo = {}
  176. /*this.setState({
  177. isLogin: false,
  178. userInfo: {}
  179. });*/
  180. setAccessToken('');
  181. routeUtil.bridge2Page(PageList.login);
  182. }
  183. getStrengthStyle() {
  184. const persent = ((this.StrengthView.maxStrength - this.state.strength) / this.StrengthView.maxStrength) * 100
  185. return {
  186. left: 0,
  187. right: 0,
  188. height: 1,
  189. bottom: -1,
  190. marginRight: persent + "%",
  191. position: 'absolute',
  192. backgroundColor: "#A6E782"
  193. }
  194. }
  195. changeSecurety() {
  196. this.setState({
  197. wrongCount: !this.state.wrongCount
  198. })
  199. }
  200. render() {
  201. return (
  202. <View style={StyleSheet.absoluteFillObject}>
  203. <ScrollView
  204. style={styles.scollView}
  205. keyboardShouldPersistTaps={isIOS ? 'never' : 'handled'}>
  206. <View style={styles.resetView}>
  207. {/* <Text style={styles.title}>Reset Password</Text> */}
  208. <View style={styles.signInput}>
  209. {/* <Text style={styles.inputLabel}>Email Address</Text> */}
  210. <Image
  211. style={styles.inputIcon}
  212. source={require('../../images/user/sign-email.png')}
  213. />
  214. { this.state.isChange
  215. ? <TextInput
  216. style={styles.inputView}
  217. placeholder={$t('sign.labelEmail')}
  218. placeholderTextColor={textPlacehoder}
  219. value={this.state.email}
  220. editable={false}
  221. maxLength={50}/>
  222. : <TextInput
  223. style={styles.inputView}
  224. placeholder={$t('sign.labelEmail')}
  225. placeholderTextColor={textPlacehoder}
  226. maxLength={50}
  227. keyboardType="email-address"
  228. textContentType='emailAddress'
  229. clearButtonMode='while-editing'
  230. onChangeText={v => this.changeInfo('email', v)}/>
  231. }
  232. </View>
  233. <View style={styles.signInput}>
  234. {/* <Text style={[styles.inputLabel, ui.flex2]}>Verification Code</Text> */}
  235. <Image
  236. style={styles.inputIcon}
  237. source={require('../../images/user/sign-otp.png')}
  238. />
  239. <TextInput
  240. style={[styles.inputView, {flex: 2.6, marginLeft: 2}]}
  241. placeholder={$t('sign.labelOtp')}
  242. placeholderTextColor={textPlacehoder}
  243. maxLength={6}
  244. keyboardType="number-pad"
  245. textContentType="telephoneNumber"
  246. onChangeText={v => this.changeInfo('verificationCode', v)}
  247. />
  248. <Button
  249. text={this.state.sendMinutes > 0 ? (this.state.sendMinutes + " s") : $t('sign.btnSendOTP')}
  250. style={styles.sendBtn}
  251. disabled={this.state.sendMinutes > 0}
  252. viewStyle={styles.sendBtnView}
  253. textStyle={styles.sendBtnText}
  254. onClick={() => this.sendVerification()}
  255. />
  256. </View>
  257. <View style={styles.signInput}>
  258. {/* <Text style={styles.inputLabel}>New Password</Text> */}
  259. <Image
  260. style={styles.inputIcon}
  261. source={require('../../images/user/sign-password.png')}
  262. />
  263. <TextInput
  264. secureTextEntry={this.state.wrongCount}
  265. style={styles.inputView}
  266. placeholder={$t('sign.labelNewPassword')}
  267. placeholderTextColor={textPlacehoder}
  268. maxLength={20}
  269. onChangeText={(value) => this.applyStrength(value)}
  270. />
  271. <Pressable
  272. style={[Styles.backIcon, {position: 'absolute', right: 0}]}
  273. android_ripple={rippleLess}
  274. onPress={() => this.changeSecurety()}>
  275. <MaterialCommunityIcons
  276. name={this.state.wrongCount ? "eye-off" : "eye"}
  277. size={20}
  278. color={textCancel}/>
  279. </Pressable>
  280. <View style={this.getStrengthStyle()}></View>
  281. </View>
  282. {/* <View style={styles.signInput}>
  283. <Text style={styles.inputLabel}></Text>
  284. <View style={styles.passwordView}>
  285. <View style={styles.strengthView}>
  286. <Text style={{fontSize:12, paddingRight: 4, color: textPrimary}}>Password Strength</Text>
  287. <StrengthView {...this.state}/>
  288. </View>
  289. <View>
  290. <Text style={styles.passwordRole}>Your Password Must Have:</Text>
  291. <Text style={styles.passwordRole}>- 8 or more characters</Text>
  292. {/* <Text style={styles.passwordRole}>- upper and lower case letters</Text> *}
  293. <Text style={styles.passwordRole}>- at least one number</Text>
  294. </View>
  295. </View>
  296. </View> */}
  297. <View style={[styles.signInput, {borderBottomColor: this.state.confirmStatusColor}]}>
  298. {/* <Text style={styles.inputLabel}>Confirm Password</Text> */}
  299. <Image
  300. style={styles.inputIcon}
  301. source={require('../../images/user/sign-password.png')}
  302. />
  303. <TextInput
  304. secureTextEntry={this.state.wrongCount}
  305. style={styles.inputView}
  306. placeholder={$t('sign.labelConfirmPassword')}
  307. placeholderTextColor={textPlacehoder}
  308. maxLength={20}
  309. onChangeText={v => this.changeInfo('password', v)}
  310. />
  311. </View>
  312. <Button
  313. text={$t('common.confirm')}
  314. style={styles.resetButton}
  315. onClick={() => this.onResetPassword()}
  316. />
  317. </View>
  318. {/* <Text style={styles.divideText}>We will send you an email with the verification code.</Text> */}
  319. </ScrollView>
  320. </View>
  321. );
  322. }
  323. }
  324. const styles = StyleSheet.create({
  325. header: {
  326. paddingTop: 56,
  327. backgroundColor: colorThemes
  328. },
  329. backView: {
  330. top: 12,
  331. zIndex: 5,
  332. padding: 16,
  333. position: 'absolute',
  334. flexDirection: 'row'
  335. },
  336. backButton: {
  337. borderRadius: 60,
  338. backgroundColor: colorLight
  339. },
  340. backButtonView: {
  341. height: 43,
  342. paddingLeft: 16,
  343. paddingRight: 16,
  344. alignItems: 'center',
  345. flexDirection: 'row'
  346. },
  347. scollView: {
  348. flex: 1,
  349. backgroundColor: colorLight
  350. },
  351. logoView: {
  352. paddingTop: 40,
  353. paddingBottom: 56,
  354. alignItems: 'center'
  355. },
  356. logoImg: {
  357. width:165.09,
  358. height: 51.94,
  359. },
  360. resetView: {
  361. flex: 1,
  362. padding: 16,
  363. marginTop: -12,
  364. borderTopLeftRadius: 20,
  365. borderTopRightRadius: 20,
  366. backgroundColor: colorLight
  367. },
  368. inputIcon: {
  369. width: 24,
  370. height: 24
  371. },
  372. title: {
  373. color: textPrimary,
  374. fontSize: 18,
  375. fontWeight: '700',
  376. paddingTop: 4,
  377. paddingBottom: 6,
  378. },
  379. signInput: {
  380. marginTop: 16,
  381. ...$padding(6, 16),
  382. alignItems: 'center',
  383. flexDirection: 'row',
  384. borderBottomWidth: 1,
  385. borderBottomColor: '#F5F5F5'
  386. },
  387. inputLabel: {
  388. flex: 1,
  389. color: textPrimary,
  390. fontSize: 14
  391. },
  392. inputView: {
  393. flex: 2,
  394. color: textPrimary,
  395. fontSize: 14,
  396. borderRadius: 5,
  397. minHeight: 40,
  398. ...$padding(6, 16),
  399. //backgroundColor: '#F5F5F5'
  400. },
  401. passwordView: {
  402. flex: 2,
  403. marginTop: -8,
  404. },
  405. passwordRole: {
  406. color: '#999',
  407. fontSize: 10,
  408. lineHeight: 13
  409. },
  410. sendBtn: {
  411. flex: 1.4,
  412. marginLeft: 6,
  413. marginRight: -6,
  414. borderRadius: 6
  415. },
  416. sendBtnView: {
  417. flex: 1,
  418. height: 40,
  419. paddingLeft: 4,
  420. paddingRight: 4,
  421. alignItems: 'center',
  422. justifyContent: 'center'
  423. },
  424. sendBtnText: {
  425. color: textButton,
  426. fontSize: 13,
  427. fontWeight: 'bold'
  428. },
  429. divideText: {
  430. color: textPrimary,
  431. fontSize: 14,
  432. padding: 58,
  433. textAlign: 'center'
  434. },
  435. resetButton: {
  436. marginTop: 32,
  437. marginBottom: 16
  438. }
  439. })