ResetPasswordV2.js 11 KB

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