change_phone.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import React, {Component} from 'react';
  2. import {View, Text, TextInput, TouchableOpacity, Image} from 'react-native';
  3. import public_css from '../../source/css/public_css';
  4. import {CheckPassword, CheckPhoneNumber} from '../../source/inspect/inspect';
  5. import {GetDataPost} from '../../data/encryption';
  6. import {ShowToast} from '../../components/rootToast/root_toast';
  7. import {ClearAll, RetrieveData} from '../../data/storage';
  8. export default class change_phone extends Component {
  9. constructor(props) {
  10. super(props);
  11. this.afterEnd = this._afterEnd;
  12. this.state = {
  13. phone: '',
  14. user_password: '',
  15. verification_code: '',
  16. token: '',
  17. timeLeft: 60,
  18. begin: 0,
  19. isDisable: false,
  20. };
  21. }
  22. render() {
  23. return (
  24. <View style={[public_css.body]}>
  25. <View
  26. style={{
  27. alignItems: 'center',
  28. flex: 1,
  29. }}>
  30. <View style={[public_css.view, public_css.lineTopBottom]}>
  31. <Text style={public_css.text}>*新手机号:</Text>
  32. <TextInput
  33. style={public_css.textInputStyle}
  34. placeholder="请输入新的手机号码"
  35. clearButtonMode="while-editing"
  36. secureTextEntry={false}
  37. onChangeText={text => {
  38. this.setState({
  39. phone: text,
  40. });
  41. }}
  42. />
  43. </View>
  44. <View style={[public_css.view, public_css.lineTopBottom]}>
  45. <Text style={public_css.text}>*密码:</Text>
  46. <TextInput
  47. style={public_css.textInputStyle}
  48. placeholder="请输入密码"
  49. clearButtonMode="while-editing"
  50. secureTextEntry
  51. onChangeText={text => {
  52. this.setState({
  53. user_password: text,
  54. });
  55. }}
  56. />
  57. </View>
  58. <View style={[public_css.view, public_css.lineTopBottom]}>
  59. <Text style={public_css.text}>*验证码:</Text>
  60. <TextInput
  61. style={public_css.textInputStyle}
  62. placeholder="请输入验证码"
  63. clearButtonMode="while-editing"
  64. secureTextEntry
  65. onChangeText={text => {
  66. this.setState({
  67. verification_code: text,
  68. });
  69. }}
  70. />
  71. <TouchableOpacity
  72. disabled={this.state.isDisable}
  73. onPress={this._beginCountDown.bind(this)}>
  74. <Text style={{lineHeight: 44, fontSize: 16, color: '#1874CD'}}>
  75. {this.state.begin === 0
  76. ? '获取验证码'
  77. : this.state.timeLeft + '秒后重试'}
  78. </Text>
  79. </TouchableOpacity>
  80. </View>
  81. </View>
  82. <View style={[public_css.bottomStaus]}>
  83. <TouchableOpacity
  84. style={[public_css.statusBtn, public_css.statusRBtn]}
  85. onPress={() => this.verificationCode()}>
  86. <Image
  87. source={require('../../source/img/productImg/confirm.png')}
  88. style={{width: 32, height: 32}}
  89. />
  90. <Text style={{color: '#fff'}}>确认</Text>
  91. </TouchableOpacity>
  92. </View>
  93. </View>
  94. );
  95. }
  96. //开始验证码计时
  97. _beginCountDown() {
  98. if (this.state.begin === 1) {
  99. return;
  100. }
  101. let time = this.state.timeLeft;
  102. let afterEnd = this.afterEnd;
  103. let begin = this.state.begin;
  104. this.countDownInfo(time, afterEnd, begin);
  105. this.sendVerificationCode();
  106. }
  107. //更新验证码状态
  108. countDownInfo(time, callback, begin) {
  109. if (time > 0) {
  110. this.state.begin = 1;
  111. let that = this;
  112. let interval = setInterval(function() {
  113. if (that.state.timeLeft < 1) {
  114. clearInterval(interval);
  115. callback(that);
  116. that.setState({
  117. isDisable: false,
  118. });
  119. } else {
  120. let totalTime = that.state.timeLeft;
  121. that.setState({
  122. timeLeft: totalTime - 1,
  123. isDisable: true,
  124. });
  125. }
  126. }, 1000);
  127. }
  128. }
  129. //提交登录信息
  130. sendVerificationCode = async () => {
  131. let {status, msg} = CheckPhoneNumber(`${this.state.phone}`);
  132. if (status) {
  133. const url = '/sms/getSmscode';
  134. let token = null;
  135. let response = await GetDataPost(
  136. url,
  137. token,
  138. {
  139. phoneNo: this.state.phone,
  140. },
  141. false,
  142. 1,
  143. );
  144. if (response) {
  145. ShowToast('验证码发送成功!');
  146. } else {
  147. ShowToast('验证码发送失败!');
  148. }
  149. } else {
  150. ShowToast(msg);
  151. }
  152. };
  153. //验证短信验证码是否正确
  154. verificationCode = async () => {
  155. if (this.state.verification_code == '') {
  156. ShowToast('验证码不能为空!');
  157. return;
  158. }
  159. let {status, msg} = CheckPhoneNumber(`${this.state.user_name}`);
  160. if (status) {
  161. let {status, msg} = CheckPassword(`${this.state.user_password}`);
  162. if (status) {
  163. const url = '/sms/matchSmscode';
  164. let token = null;
  165. let response = await GetDataPost(
  166. url,
  167. token,
  168. {
  169. phoneNo: this.state.phone,
  170. smscode: this.state.verification_code,
  171. },
  172. false,
  173. 1,
  174. );
  175. if (response) {
  176. this.submitData();
  177. } else {
  178. ShowToast('验证码不正确!');
  179. }
  180. } else {
  181. ShowToast(msg);
  182. }
  183. } else {
  184. ShowToast(msg);
  185. }
  186. };
  187. //提交修改密码数据
  188. submitData = async () => {
  189. let account = await RetrieveData('account');
  190. let token = await RetrieveData('token');
  191. if (token && account) {
  192. account = account.substring(1, account.length - 1);
  193. token = token.substring(1, token.length - 1);
  194. const url = '/auth/comm/user/modifyMobile';
  195. let response = await GetDataPost(
  196. url,
  197. token,
  198. {
  199. oriMobile: account,
  200. mobile: this.state.phone,
  201. password: this.state.user_password,
  202. smsCode: this.state.verification_code,
  203. reqChannel: 3,
  204. ip: '',
  205. },
  206. false,
  207. 1,
  208. );
  209. if (response) {
  210. if (response.code == 0) {
  211. ShowToast('修改成功!');
  212. await ClearAll();
  213. this.props.navigation.popToTop();
  214. }
  215. } else {
  216. ShowToast('服务器故障!');
  217. }
  218. }
  219. };
  220. }