ResetPasswordV2.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. wrongCount: true,
  26. sendMinutes: 0,
  27. confirmStatusColor: "#F5F5F5"
  28. };
  29. this.formInfo = {
  30. email: '',
  31. password: '',
  32. verificationCode: ''
  33. }
  34. this.isChange = false;
  35. }
  36. componentDidMount() {
  37. const action = this.props.route?.params?.action ?? "";
  38. if (action == "change") {
  39. this.isChange = true;
  40. /*setTimeout(() => {
  41. this.requestLogout();
  42. }, 1000);*/
  43. }
  44. const email = userInfo.email;
  45. this.formInfo.email = email;
  46. this.setState({
  47. email: email
  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 (!/^[a-zA-Z0-9]+[\S]+@[a-zA-Z0-9_-]+[\.][\Sa-zA-Z]+$/.test(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 (!/^[a-zA-Z0-9]+[\S]+@[a-zA-Z0-9_-]+[\.][\Sa-zA-Z]+$/.test(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.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. <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. keyboardType="email-address"
  222. textContentType='emailAddress'
  223. clearButtonMode='while-editing'
  224. onChangeText={v => this.changeInfo('email', v)}
  225. />
  226. </View>
  227. <View style={styles.signInput}>
  228. {/* <Text style={[styles.inputLabel, ui.flex2]}>Verification Code</Text> */}
  229. <Image
  230. style={styles.inputIcon}
  231. source={require('../../images/user/sign-otp.png')}
  232. />
  233. <TextInput
  234. style={[styles.inputView, {flex: 2.6, marginLeft: 2}]}
  235. placeholder={$t('sign.labelOtp')}
  236. placeholderTextColor={textPlacehoder}
  237. maxLength={6}
  238. keyboardType="number-pad"
  239. textContentType="telephoneNumber"
  240. onChangeText={v => this.changeInfo('verificationCode', v)}
  241. />
  242. <Button
  243. text={this.state.sendMinutes > 0 ? (this.state.sendMinutes + " s") : $t('sign.btnSendOTP')}
  244. style={styles.sendBtn}
  245. disabled={this.state.sendMinutes > 0}
  246. viewStyle={styles.sendBtnView}
  247. textStyle={styles.sendBtnText}
  248. onClick={() => this.sendVerification()}
  249. />
  250. </View>
  251. <View style={styles.signInput}>
  252. {/* <Text style={styles.inputLabel}>New Password</Text> */}
  253. <Image
  254. style={styles.inputIcon}
  255. source={require('../../images/user/sign-password.png')}
  256. />
  257. <TextInput
  258. secureTextEntry={this.state.wrongCount}
  259. style={styles.inputView}
  260. placeholder={$t('sign.labelNewPassword')}
  261. placeholderTextColor={textPlacehoder}
  262. maxLength={20}
  263. onChangeText={(value) => this.applyStrength(value)}
  264. />
  265. <Pressable
  266. style={[Styles.backIcon, {position: 'absolute', right: 0}]}
  267. android_ripple={rippleLess}
  268. onPress={() => this.changeSecurety()}>
  269. <MaterialCommunityIcons
  270. name={this.state.wrongCount ? "eye-off" : "eye"}
  271. size={20}
  272. color={textCancel}/>
  273. </Pressable>
  274. <View style={this.getStrengthStyle()}></View>
  275. </View>
  276. {/* <View style={styles.signInput}>
  277. <Text style={styles.inputLabel}></Text>
  278. <View style={styles.passwordView}>
  279. <View style={styles.strengthView}>
  280. <Text style={{fontSize:12, paddingRight: 4, color: textPrimary}}>Password Strength</Text>
  281. <StrengthView {...this.state}/>
  282. </View>
  283. <View>
  284. <Text style={styles.passwordRole}>Your Password Must Have:</Text>
  285. <Text style={styles.passwordRole}>- 8 or more characters</Text>
  286. {/* <Text style={styles.passwordRole}>- upper and lower case letters</Text> *}
  287. <Text style={styles.passwordRole}>- at least one number</Text>
  288. </View>
  289. </View>
  290. </View> */}
  291. <View style={[styles.signInput, {borderBottomColor: this.state.confirmStatusColor}]}>
  292. {/* <Text style={styles.inputLabel}>Confirm Password</Text> */}
  293. <Image
  294. style={styles.inputIcon}
  295. source={require('../../images/user/sign-password.png')}
  296. />
  297. <TextInput
  298. secureTextEntry={this.state.wrongCount}
  299. style={styles.inputView}
  300. placeholder={$t('sign.labelConfirmPassword')}
  301. placeholderTextColor={textPlacehoder}
  302. maxLength={20}
  303. onChangeText={v => this.changeInfo('password', v)}
  304. />
  305. </View>
  306. <Button
  307. text={$t('common.confirm')}
  308. style={styles.resetButton}
  309. onClick={() => this.onResetPassword()}
  310. />
  311. </View>
  312. {/* <Text style={styles.divideText}>We will send you an email with the verification code.</Text> */}
  313. </ScrollView>
  314. </View>
  315. );
  316. }
  317. }
  318. const styles = StyleSheet.create({
  319. header: {
  320. paddingTop: 56,
  321. backgroundColor: colorThemes
  322. },
  323. backView: {
  324. top: 12,
  325. zIndex: 5,
  326. padding: 16,
  327. position: 'absolute',
  328. flexDirection: 'row'
  329. },
  330. backButton: {
  331. borderRadius: 60,
  332. backgroundColor: colorLight
  333. },
  334. backButtonView: {
  335. height: 43,
  336. paddingLeft: 16,
  337. paddingRight: 16,
  338. alignItems: 'center',
  339. flexDirection: 'row'
  340. },
  341. scollView: {
  342. flex: 1,
  343. backgroundColor: colorLight
  344. },
  345. logoView: {
  346. paddingTop: 40,
  347. paddingBottom: 56,
  348. alignItems: 'center'
  349. },
  350. logoImg: {
  351. width:165.09,
  352. height: 51.94,
  353. },
  354. resetView: {
  355. flex: 1,
  356. padding: 16,
  357. marginTop: -12,
  358. borderTopLeftRadius: 20,
  359. borderTopRightRadius: 20,
  360. backgroundColor: colorLight
  361. },
  362. inputIcon: {
  363. width: 24,
  364. height: 24
  365. },
  366. title: {
  367. color: textPrimary,
  368. fontSize: 18,
  369. fontWeight: '700',
  370. paddingTop: 4,
  371. paddingBottom: 6,
  372. },
  373. signInput: {
  374. marginTop: 16,
  375. ...$padding(6, 16),
  376. alignItems: 'center',
  377. flexDirection: 'row',
  378. borderBottomWidth: 1,
  379. borderBottomColor: '#F5F5F5'
  380. },
  381. inputLabel: {
  382. flex: 1,
  383. color: textPrimary,
  384. fontSize: 14
  385. },
  386. inputView: {
  387. flex: 2,
  388. color: textPrimary,
  389. fontSize: 14,
  390. borderRadius: 5,
  391. minHeight: 40,
  392. ...$padding(6, 16),
  393. //backgroundColor: '#F5F5F5'
  394. },
  395. passwordView: {
  396. flex: 2,
  397. marginTop: -8,
  398. },
  399. passwordRole: {
  400. color: '#999',
  401. fontSize: 10,
  402. lineHeight: 13
  403. },
  404. sendBtn: {
  405. flex: 1.4,
  406. marginLeft: 6,
  407. marginRight: -6,
  408. borderRadius: 6
  409. },
  410. sendBtnView: {
  411. flex: 1,
  412. height: 40,
  413. paddingLeft: 4,
  414. paddingRight: 4,
  415. alignItems: 'center',
  416. justifyContent: 'center'
  417. },
  418. sendBtnText: {
  419. color: textButton,
  420. fontSize: 13,
  421. fontWeight: 'bold'
  422. },
  423. divideText: {
  424. color: textPrimary,
  425. fontSize: 14,
  426. padding: 58,
  427. textAlign: 'center'
  428. },
  429. resetButton: {
  430. marginTop: 32,
  431. marginBottom: 16
  432. }
  433. })