ResetPassword.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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={isIOS ? 'never' : '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. placeholderTextColor={textPlacehoder}
  169. maxLength={50}
  170. onChangeText={v => this.changeInfo('email', v)}
  171. />
  172. </View>
  173. <View style={styles.signInput}>
  174. <Text style={styles.inputLabel}>New Password</Text>
  175. <TextInput
  176. secureTextEntry={this.state.wrongCount < 3}
  177. style={styles.inputView}
  178. placeholder='Password'
  179. placeholderTextColor={textPlacehoder}
  180. maxLength={20}
  181. onChangeText={(value) => {
  182. this.applyStrength(value);
  183. }}
  184. />
  185. </View>
  186. <View style={styles.signInput}>
  187. <Text style={styles.inputLabel}></Text>
  188. <View style={styles.passwordView}>
  189. <View style={styles.strengthView}>
  190. <Text style={{fontSize:12, paddingRight: 4, color: textPrimary}}>Password Strength</Text>
  191. <StrengthView {...this.state}/>
  192. </View>
  193. <View>
  194. <Text style={styles.passwordRole}>Your Password Must Have:</Text>
  195. <Text style={styles.passwordRole}>- 8 or more characters</Text>
  196. {/* <Text style={styles.passwordRole}>- upper and lower case letters</Text> */}
  197. <Text style={styles.passwordRole}>- at least one number</Text>
  198. </View>
  199. </View>
  200. </View>
  201. <View style={styles.signInput}>
  202. <Text style={styles.inputLabel}>Confirm Password</Text>
  203. <TextInput
  204. secureTextEntry={this.state.wrongCount < 3}
  205. style={styles.inputView}
  206. placeholder='Password'
  207. placeholderTextColor={textPlacehoder}
  208. maxLength={20}
  209. onChangeText={v => this.changeInfo('password', v)}
  210. />
  211. </View>
  212. <View style={styles.signInput}>
  213. <Text style={[styles.inputLabel, ui.flex2]}>Verification Code</Text>
  214. <TextInput
  215. style={[styles.inputView, {flex: 2.6, marginLeft: 2}]}
  216. placeholder='Verification Code'
  217. placeholderTextColor={textPlacehoder}
  218. maxLength={6}
  219. onChangeText={v => this.changeInfo('verificationCode', v)}
  220. />
  221. <Button
  222. text={this.state.sendMinutes > 0 ? this.state.sendMinutes : 'Get Code'}
  223. style={styles.sendBtn}
  224. disabled={this.state.sendMinutes > 0}
  225. viewStyle={styles.sendBtnView}
  226. textStyle={styles.sendBtnText}
  227. onClick={() => this.sendVerification()}
  228. />
  229. </View>
  230. </View>
  231. <Text style={styles.divideText}>We will send you an email with the verification code.</Text>
  232. <Button
  233. text='Reset Password'
  234. style={styles.resetButton}
  235. onClick={() => this.onResetPassword()}
  236. />
  237. </ScrollView>
  238. </View>
  239. );
  240. }
  241. }
  242. const styles = StyleSheet.create({
  243. header: {
  244. paddingTop: 56,
  245. backgroundColor: colorAccent
  246. },
  247. backView: {
  248. top: 12,
  249. zIndex: 5,
  250. padding: 16,
  251. position: 'absolute',
  252. flexDirection: 'row'
  253. },
  254. backButton: {
  255. borderRadius: 60,
  256. backgroundColor: colorLight
  257. },
  258. backButtonView: {
  259. height: 43,
  260. paddingLeft: 16,
  261. paddingRight: 16,
  262. alignItems: 'center',
  263. flexDirection: 'row'
  264. },
  265. scollView: {
  266. flex: 1,
  267. backgroundColor: pageBackground
  268. },
  269. logoView: {
  270. paddingTop: 40,
  271. paddingBottom: 56,
  272. alignItems: 'center'
  273. },
  274. logoImg: {
  275. width:165.09,
  276. height: 51.94,
  277. },
  278. resetView: {
  279. flex: 1,
  280. padding: 16,
  281. marginTop: -30,
  282. borderTopLeftRadius: 20,
  283. borderTopRightRadius: 20,
  284. backgroundColor: colorLight
  285. },
  286. title: {
  287. color: textPrimary,
  288. fontSize: 18,
  289. fontWeight: '700',
  290. paddingTop: 4,
  291. paddingBottom: 6,
  292. },
  293. signInput: {
  294. marginTop: 16,
  295. alignItems: 'center',
  296. flexDirection: 'row'
  297. },
  298. inputLabel: {
  299. flex: 1,
  300. color: textPrimary,
  301. fontSize: 14
  302. },
  303. inputView: {
  304. flex: 2,
  305. color: textPrimary,
  306. fontSize: 14,
  307. borderRadius: 5,
  308. minHeight: 40,
  309. paddingTop: 6,
  310. paddingLeft: 12,
  311. paddingRight: 12,
  312. paddingBottom: 6,
  313. backgroundColor: '#F5F5F5'
  314. },
  315. passwordView: {
  316. flex: 2,
  317. marginTop: -8,
  318. },
  319. strengthView: {
  320. marginTop: -4,
  321. alignItems: 'center',
  322. paddingBottom: 4,
  323. flexDirection: 'row'
  324. },
  325. passwordRole: {
  326. color: '#999',
  327. fontSize: 10,
  328. lineHeight: 13
  329. },
  330. sendBtn: {
  331. flex: 1.4,
  332. marginLeft: 6
  333. },
  334. sendBtnView: {
  335. flex: 1,
  336. height: 40,
  337. paddingLeft: 4,
  338. paddingRight: 4,
  339. alignItems: 'center',
  340. justifyContent: 'center'
  341. },
  342. sendBtnText: {
  343. color: textPrimary,
  344. fontSize: 13,
  345. fontWeight: 'bold'
  346. },
  347. divideText: {
  348. color: textPrimary,
  349. fontSize: 14,
  350. padding: 58,
  351. textAlign: 'center'
  352. },
  353. resetButton: {
  354. margin: 16
  355. }
  356. })