ResetPasswordV2.js 12 KB

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