ResetPassword.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /**
  2. * 忘记密码-重置密码页面
  3. * @邠心vbe on 2021/10/18
  4. */
  5. import React, { Component } from 'react';
  6. import { View, Text, StyleSheet, ScrollView, Image, TextInput } from 'react-native';
  7. import apiUser from '../../api/apiUser';
  8. import Button from '../../components/Button';
  9. import Dialog from '../../components/Dialog';
  10. import { BackIcon } from '../../components/Toolbar';
  11. import utils from '../../utils/utils';
  12. import { StrengthView } from './Register';
  13. export default class ResetPassword extends Component {
  14. constructor(props) {
  15. super(props);
  16. this.state = {
  17. strength: 0,
  18. password: '',
  19. wrongCount: 0,
  20. sendMinutes: 0
  21. };
  22. this.formInfo = {
  23. email: '',
  24. password: '',
  25. verificationCode: ''
  26. }
  27. }
  28. componentWillUnmount() {
  29. this.setState({
  30. sendMinutes: 0
  31. })
  32. }
  33. getBackTopPosition() {
  34. return isIOS ? statusHeight - 18 : 12;
  35. }
  36. changeInfo(key, value) {
  37. this.formInfo[key] = value;
  38. }
  39. applyStrength(text) {
  40. var strength = 0;
  41. if (text.length >= 8) {
  42. strength += 1;
  43. }
  44. if (/\d{1,}/.test(text)) {
  45. strength += 1;
  46. }
  47. if (/[A-z]{1,}/.test(text)) {
  48. strength += 1;
  49. }
  50. /*if (/[A-Z]{1,}/.test(text)) {
  51. strength += 1;
  52. }
  53. if (/\W{1,}/.test(text)) {
  54. strength += 1;
  55. }*/
  56. if (this.state.strength != strength) {
  57. this.setState({
  58. password: text,
  59. strength: strength
  60. });
  61. } else {
  62. this.setState({
  63. password: text
  64. });
  65. }
  66. }
  67. sendVerification() {
  68. var info = this.formInfo;
  69. if (!info.email) {
  70. toastShort($t('sign.plsInputEmail'));
  71. return;
  72. }
  73. if (!utils.isValidEmail(info.email)) {
  74. toastShort($t('sign.errEmailFormat'));
  75. return;
  76. }
  77. Dialog.showProgressDialog()
  78. apiUser.sendVerificationCode(info.email).then(res => {
  79. Dialog.dismissLoading()
  80. this.state.sendMinutes = 60;
  81. toastShort('Send verification code successfully');
  82. this.contdownTime();
  83. }).catch(err => {
  84. Dialog.dismissLoading()
  85. toastShort(err);
  86. });
  87. }
  88. contdownTime() {
  89. if (this.state.sendMinutes > 0) {
  90. this.setState({
  91. sendMinutes: this.state.sendMinutes - 1
  92. })
  93. setTimeout(() => {
  94. this.contdownTime();
  95. }, 1000);
  96. }
  97. }
  98. onResetPassword() {
  99. var info = this.formInfo;
  100. console.log('reset info', info);
  101. if (!info.email) {
  102. toastShort($t('sign.plsInputEmail'));
  103. return;
  104. }
  105. if (!utils.isValidEmail(info.email)) {
  106. toastShort($t('sign.errEmailFormat'));
  107. return;
  108. }
  109. if (!this.state.password) {
  110. toastShort('Please enter password');
  111. return;
  112. }
  113. if (this.state.strength < 3) {
  114. toastShort('Password is not strong');
  115. return;
  116. }
  117. if (!info.password) {
  118. toastShort('Please enter confirm password');
  119. return;
  120. }
  121. if (info.password != this.state.password) {
  122. toastShort('The twice passwords are inconsistent');
  123. return;
  124. }
  125. if (!info.verificationCode) {
  126. toastShort('Please enter verification code');
  127. return;
  128. }
  129. Dialog.showProgressDialog()
  130. apiUser.updatePassword(this.formInfo).then(res => {
  131. Dialog.dismissLoading()
  132. toastShort('Reset password successfully');
  133. goBack();
  134. }).catch(err => {
  135. Dialog.dismissLoading()
  136. toastShort(err);
  137. });
  138. }
  139. render() {
  140. return (
  141. <View style={StyleSheet.absoluteFillObject}>
  142. <ScrollView
  143. style={styles.scollView}
  144. keyboardShouldPersistTaps={isIOS ? 'never' : 'handled'}>
  145. <View style={styles.header}>
  146. <View style={styles.logoView}>
  147. <Image
  148. style={styles.logoImg}
  149. resizeMode="contain"
  150. source={require('../../images/app-logo.png')}/>
  151. </View>
  152. </View>
  153. <View style={{...styles.backView, top: this.getBackTopPosition()}}>
  154. <Button
  155. style={styles.backButton}
  156. viewStyle={styles.backButtonView}
  157. onClick={() => goBack()}>
  158. <BackIcon/>
  159. <Text style={{color: textPrimary,fontSize: 16,paddingLeft: 8}}>Back to Login</Text>
  160. </Button>
  161. </View>
  162. <View style={styles.resetView}>
  163. <Text style={styles.title}>Reset Password</Text>
  164. <View style={styles.signInput}>
  165. <Text style={styles.inputLabel}>Email Address</Text>
  166. <TextInput
  167. style={styles.inputView}
  168. placeholder='Email Address'
  169. placeholderTextColor={textPlacehoder}
  170. maxLength={50}
  171. onChangeText={v => this.changeInfo('email', v)}
  172. />
  173. </View>
  174. <View style={styles.signInput}>
  175. <Text style={styles.inputLabel}>New Password</Text>
  176. <TextInput
  177. secureTextEntry={this.state.wrongCount < 3}
  178. style={styles.inputView}
  179. placeholder='Password'
  180. placeholderTextColor={textPlacehoder}
  181. maxLength={20}
  182. onChangeText={(value) => {
  183. this.applyStrength(value);
  184. }}
  185. />
  186. </View>
  187. <View style={styles.signInput}>
  188. <Text style={styles.inputLabel}></Text>
  189. <View style={styles.passwordView}>
  190. <View style={styles.strengthView}>
  191. <Text style={{fontSize:12, paddingRight: 4, color: textPrimary}}>Password Strength</Text>
  192. <StrengthView {...this.state}/>
  193. </View>
  194. <View>
  195. <Text style={styles.passwordRole}>Your Password Must Have:</Text>
  196. <Text style={styles.passwordRole}>- 8 or more characters</Text>
  197. {/* <Text style={styles.passwordRole}>- upper and lower case letters</Text> */}
  198. <Text style={styles.passwordRole}>- at least one number</Text>
  199. </View>
  200. </View>
  201. </View>
  202. <View style={styles.signInput}>
  203. <Text style={styles.inputLabel}>Confirm Password</Text>
  204. <TextInput
  205. secureTextEntry={this.state.wrongCount < 3}
  206. style={styles.inputView}
  207. placeholder='Password'
  208. placeholderTextColor={textPlacehoder}
  209. maxLength={20}
  210. onChangeText={v => this.changeInfo('password', v)}
  211. />
  212. </View>
  213. <View style={styles.signInput}>
  214. <Text style={[styles.inputLabel, ui.flex2]}>Verification Code</Text>
  215. <TextInput
  216. style={[styles.inputView, {flex: 2.6, marginLeft: 2}]}
  217. placeholder='Verification Code'
  218. placeholderTextColor={textPlacehoder}
  219. maxLength={6}
  220. onChangeText={v => this.changeInfo('verificationCode', v)}
  221. />
  222. <Button
  223. text={this.state.sendMinutes > 0 ? this.state.sendMinutes : 'Get Code'}
  224. style={styles.sendBtn}
  225. disabled={this.state.sendMinutes > 0}
  226. viewStyle={styles.sendBtnView}
  227. textStyle={styles.sendBtnText}
  228. onClick={() => this.sendVerification()}
  229. />
  230. </View>
  231. </View>
  232. <Text style={styles.divideText}>We will send you an email with the verification code.</Text>
  233. <Button
  234. text='Reset Password'
  235. style={styles.resetButton}
  236. onClick={() => this.onResetPassword()}
  237. />
  238. </ScrollView>
  239. </View>
  240. );
  241. }
  242. }
  243. const styles = StyleSheet.create({
  244. header: {
  245. paddingTop: 56,
  246. backgroundColor: colorAccent
  247. },
  248. backView: {
  249. top: 12,
  250. zIndex: 5,
  251. padding: 16,
  252. position: 'absolute',
  253. flexDirection: 'row'
  254. },
  255. backButton: {
  256. borderRadius: 60,
  257. backgroundColor: colorLight
  258. },
  259. backButtonView: {
  260. height: 43,
  261. paddingLeft: 16,
  262. paddingRight: 16,
  263. alignItems: 'center',
  264. flexDirection: 'row'
  265. },
  266. scollView: {
  267. flex: 1,
  268. backgroundColor: pageBackground
  269. },
  270. logoView: {
  271. paddingTop: 40,
  272. paddingBottom: 56,
  273. alignItems: 'center'
  274. },
  275. logoImg: {
  276. width:165.09,
  277. height: 51.94,
  278. },
  279. resetView: {
  280. flex: 1,
  281. padding: 16,
  282. marginTop: -30,
  283. borderTopLeftRadius: 20,
  284. borderTopRightRadius: 20,
  285. backgroundColor: colorLight
  286. },
  287. title: {
  288. color: textPrimary,
  289. fontSize: 18,
  290. fontWeight: '700',
  291. paddingTop: 4,
  292. paddingBottom: 6,
  293. },
  294. signInput: {
  295. marginTop: 16,
  296. alignItems: 'center',
  297. flexDirection: 'row'
  298. },
  299. inputLabel: {
  300. flex: 1,
  301. color: textPrimary,
  302. fontSize: 14
  303. },
  304. inputView: {
  305. flex: 2,
  306. color: textPrimary,
  307. fontSize: 14,
  308. borderRadius: 5,
  309. minHeight: 40,
  310. paddingTop: 6,
  311. paddingLeft: 12,
  312. paddingRight: 12,
  313. paddingBottom: 6,
  314. backgroundColor: '#F5F5F5'
  315. },
  316. passwordView: {
  317. flex: 2,
  318. marginTop: -8,
  319. },
  320. strengthView: {
  321. marginTop: -4,
  322. alignItems: 'center',
  323. paddingBottom: 4,
  324. flexDirection: 'row'
  325. },
  326. passwordRole: {
  327. color: '#999',
  328. fontSize: 10,
  329. lineHeight: 13
  330. },
  331. sendBtn: {
  332. flex: 1.4,
  333. marginLeft: 6
  334. },
  335. sendBtnView: {
  336. flex: 1,
  337. height: 40,
  338. paddingLeft: 4,
  339. paddingRight: 4,
  340. alignItems: 'center',
  341. justifyContent: 'center'
  342. },
  343. sendBtnText: {
  344. color: textPrimary,
  345. fontSize: 13,
  346. fontWeight: 'bold'
  347. },
  348. divideText: {
  349. color: textPrimary,
  350. fontSize: 14,
  351. padding: 58,
  352. textAlign: 'center'
  353. },
  354. resetButton: {
  355. margin: 16
  356. }
  357. })