ResetPasswordV2.js 12 KB

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