ResetPassword.js 9.3 KB

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