ResetPasswordV2.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. maxLength={50}
  179. keyboardType="email-address"
  180. textContentType='emailAddress'
  181. clearButtonMode='while-editing'
  182. onChangeText={v => this.changeInfo('email', v)}
  183. />
  184. </View>
  185. <View style={styles.signInput}>
  186. {/* <Text style={[styles.inputLabel, ui.flex2]}>Verification Code</Text> */}
  187. <Image
  188. style={styles.inputIcon}
  189. source={require('../../images/user/sign-otp.png')}
  190. />
  191. <TextInput
  192. style={[styles.inputView, {flex: 2.6, marginLeft: 2}]}
  193. placeholder='OTP'
  194. maxLength={6}
  195. keyboardType="number-pad"
  196. textContentType="telephoneNumber"
  197. onChangeText={v => this.changeInfo('verificationCode', v)}
  198. />
  199. <Button
  200. text={this.state.sendMinutes > 0 ? (this.state.sendMinutes + " s") : 'EMAIL OTP'}
  201. style={styles.sendBtn}
  202. disabled={this.state.sendMinutes > 0}
  203. viewStyle={styles.sendBtnView}
  204. textStyle={styles.sendBtnText}
  205. onClick={() => this.sendVerification()}
  206. />
  207. </View>
  208. <View style={styles.signInput}>
  209. {/* <Text style={styles.inputLabel}>New Password</Text> */}
  210. <Image
  211. style={styles.inputIcon}
  212. source={require('../../images/user/sign-password.png')}
  213. />
  214. <TextInput
  215. secureTextEntry={this.state.wrongCount}
  216. style={styles.inputView}
  217. placeholder='New Password'
  218. maxLength={20}
  219. onChangeText={(value) => this.applyStrength(value)}
  220. />
  221. <Pressable
  222. style={[Styles.backIcon, {position: 'absolute', right: 0}]}
  223. android_ripple={rippleLess}
  224. onPress={() => this.changeSecurety()}>
  225. <MaterialCommunityIcons
  226. name={this.state.wrongCount ? "eye-off" : "eye"}
  227. size={20}
  228. color={textCancel}/>
  229. </Pressable>
  230. <View style={this.getStrengthStyle()}></View>
  231. </View>
  232. {/* <View style={styles.signInput}>
  233. <Text style={styles.inputLabel}></Text>
  234. <View style={styles.passwordView}>
  235. <View style={styles.strengthView}>
  236. <Text style={{fontSize:12, paddingRight: 4, color: textPrimary}}>Password Strength</Text>
  237. <StrengthView {...this.state}/>
  238. </View>
  239. <View>
  240. <Text style={styles.passwordRole}>Your Password Must Have:</Text>
  241. <Text style={styles.passwordRole}>- 8 or more characters</Text>
  242. {/* <Text style={styles.passwordRole}>- upper and lower case letters</Text> *}
  243. <Text style={styles.passwordRole}>- at least one number</Text>
  244. </View>
  245. </View>
  246. </View> */}
  247. <View style={[styles.signInput, {borderBottomColor: this.state.confirmStatusColor}]}>
  248. {/* <Text style={styles.inputLabel}>Confirm Password</Text> */}
  249. <Image
  250. style={styles.inputIcon}
  251. source={require('../../images/user/sign-password.png')}
  252. />
  253. <TextInput
  254. secureTextEntry={this.state.wrongCount}
  255. style={styles.inputView}
  256. placeholder='Confirm Password'
  257. maxLength={20}
  258. onChangeText={v => this.changeInfo('password', v)}
  259. />
  260. </View>
  261. <Button
  262. text='Confirm'
  263. style={styles.resetButton}
  264. onClick={() => this.onResetPassword()}
  265. />
  266. </View>
  267. {/* <Text style={styles.divideText}>We will send you an email with the verification code.</Text> */}
  268. </ScrollView>
  269. </View>
  270. );
  271. }
  272. }
  273. const styles = StyleSheet.create({
  274. header: {
  275. paddingTop: 56,
  276. backgroundColor: colorThemes
  277. },
  278. backView: {
  279. top: 12,
  280. zIndex: 5,
  281. padding: 16,
  282. position: 'absolute',
  283. flexDirection: 'row'
  284. },
  285. backButton: {
  286. borderRadius: 60,
  287. backgroundColor: colorLight
  288. },
  289. backButtonView: {
  290. height: 43,
  291. paddingLeft: 16,
  292. paddingRight: 16,
  293. alignItems: 'center',
  294. flexDirection: 'row'
  295. },
  296. scollView: {
  297. flex: 1,
  298. backgroundColor: colorLight
  299. },
  300. logoView: {
  301. paddingTop: 40,
  302. paddingBottom: 56,
  303. alignItems: 'center'
  304. },
  305. logoImg: {
  306. width:165.09,
  307. height: 51.94,
  308. },
  309. resetView: {
  310. flex: 1,
  311. padding: 16,
  312. marginTop: -12,
  313. borderTopLeftRadius: 20,
  314. borderTopRightRadius: 20,
  315. backgroundColor: colorLight
  316. },
  317. inputIcon: {
  318. width: 24,
  319. height: 24
  320. },
  321. title: {
  322. color: textPrimary,
  323. fontSize: 18,
  324. fontWeight: '700',
  325. paddingTop: 4,
  326. paddingBottom: 6,
  327. },
  328. signInput: {
  329. marginTop: 16,
  330. ...$padding(6, 16),
  331. alignItems: 'center',
  332. flexDirection: 'row',
  333. borderBottomWidth: 1,
  334. borderBottomColor: '#F5F5F5'
  335. },
  336. inputLabel: {
  337. flex: 1,
  338. color: textPrimary,
  339. fontSize: 14
  340. },
  341. inputView: {
  342. flex: 2,
  343. color: textPrimary,
  344. fontSize: 14,
  345. borderRadius: 5,
  346. minHeight: 40,
  347. ...$padding(6, 16),
  348. //backgroundColor: '#F5F5F5'
  349. },
  350. passwordView: {
  351. flex: 2,
  352. marginTop: -8,
  353. },
  354. passwordRole: {
  355. color: '#999',
  356. fontSize: 10,
  357. lineHeight: 13
  358. },
  359. sendBtn: {
  360. flex: 1.4,
  361. marginLeft: 6,
  362. marginRight: -6,
  363. borderRadius: 6
  364. },
  365. sendBtnView: {
  366. flex: 1,
  367. height: 40,
  368. paddingLeft: 4,
  369. paddingRight: 4,
  370. alignItems: 'center',
  371. justifyContent: 'center'
  372. },
  373. sendBtnText: {
  374. color: colorLight,
  375. fontSize: 13,
  376. fontWeight: 'bold'
  377. },
  378. divideText: {
  379. color: textPrimary,
  380. fontSize: 14,
  381. padding: 58,
  382. textAlign: 'center'
  383. },
  384. resetButton: {
  385. marginTop: 32,
  386. marginBottom: 16
  387. }
  388. })