ChangePhoneNumber.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /**
  2. * Created by FANGlh on 2019/1/16 14:18.
  3. * Desc: 更换手机号
  4. */
  5. import React,{Component} from 'react';
  6. import {connect} from 'react-redux';
  7. import { Result, Icon, WhiteSpace } from 'antd-mobile';
  8. import './ChangePhoneNumber.css';
  9. import {Input,Button} from 'antd';
  10. import {Toast} from 'antd-mobile';
  11. import {fetchPost,fetchGet,fetchGetNoSession} from '../../../utils/fetchRequest';
  12. import {API,_baseURL} from '../../../configs/api.config';
  13. let mSeconds = 0;
  14. class ChangePhoneNumber extends Component{
  15. constructor(props){
  16. super(props);
  17. this.state = {
  18. phoneNumber:null,
  19. vCode:null,
  20. changeSuccess:false, //修改成功后显示视图
  21. sendCodeSuccess:false, //已点击过发送验证吗
  22. obtainText: '重新发送(59)',
  23. }
  24. }
  25. render(){
  26. return(
  27. <div className="common-column-layout" style={{height:"100vh",backgroundColor:"#F6F6F6"}} onChange={this.handelValueCom}>
  28. {
  29. this.state.changeSuccess == false ?
  30. <div style={{marginTop:20}}>
  31. <div className="input_phone_number">
  32. <div className="add_86">+86</div>
  33. <div className="shu_xian"></div>
  34. <input type="number" placeholder="请输入手机号码" className="textarea_sty" maxLength={11} ref='phoneNumber' value={this.state.phoneNumber}/>
  35. </div>
  36. <div className="input_phone_number">
  37. <input type="number" placeholder="请输入验证码" className="textarea_sty" style={{width:'60%'}} maxLength={6} ref='vCode' value={this.state.vCode}/>
  38. {
  39. this.state.sendCodeSuccess == false ?
  40. <div className="send_code handle_code" onClick={this.getvCode}> 发送验证码</div>
  41. :
  42. <div className="send_code_again handle_code">{this.state.obtainText}</div>
  43. }
  44. </div>
  45. </div>
  46. :
  47. <div style={{textAlign:'center',marginTop:100}}>
  48. <Icon type="check-circle" style={{ fill: '#1F90E6',width:98,height:98}} />
  49. <div style={{fontFamily:'PingFangSC-Regular',fontSize:18,color:'#323232',letterSpacing:2.25,marginTop:20}}>更换手机号成功</div>
  50. </div>
  51. }
  52. {
  53. this.state.changeSuccess == false ? <Button className='commonButton' type='primary' style={{margin: '35px',borderRadius:30}}
  54. onClick={this.doBindPhone}>绑定</Button> : ''
  55. }
  56. </div>
  57. )
  58. }
  59. doBindPhone = ()=>{
  60. console.log('state',this.state)
  61. if(this.state.phoneNumber == null || this.state.phoneNumber.trim().length < 11){
  62. Toast.show("请输入正确的手机号码")
  63. return
  64. }
  65. if(this.state.vCode == null || this.state.vCode.trim().length == 0){
  66. Toast.show("请输入验证码")
  67. return
  68. }
  69. var params = {
  70. userPhone:this.state.phoneNumber,
  71. openid:this.props.userInfo.user.userOpenid,
  72. code:this.state.vCode,
  73. }
  74. fetchPost(API.updatePhone,params,{})
  75. .then((response)=>{
  76. console.log('response',response)
  77. if(response.success){
  78. Toast.show("绑定成功")
  79. this.setState({
  80. changeSuccess:true
  81. },function () {
  82. // this.props.history.push('/homepage/')
  83. this.props.history.goBack()
  84. })
  85. }else {
  86. Toast.show("绑定失败")
  87. }
  88. })
  89. .catch((error) =>{
  90. console.log('error',error)
  91. Toast.show(error)
  92. })
  93. }
  94. getvCode = () =>{
  95. if(this.state.phoneNumber == null || this.state.phoneNumber.trim().length < 11){
  96. Toast.show("请输入正确的手机号码")
  97. return
  98. }
  99. Toast.loading('验证码获取中...', 0)
  100. this.setState({
  101. obtainText: '获取中'
  102. })
  103. var params = {
  104. userPhone:this.state.phoneNumber.trim(),
  105. openid: this.props.userInfo.user.userOpenid,
  106. schoolId:this.props.userInfo.user.schoolId
  107. }
  108. fetchGet(API.SEND_CODEUPDATE, params).then(response => {
  109. Toast.hide()
  110. if (response.success){
  111. Toast.success('验证码已发送,请注意查收', 2)
  112. mSeconds = 60
  113. this.setState({
  114. sendCodeSuccess:true,
  115. obtainText: '重新发送(' + mSeconds + ')'
  116. })
  117. this.countdown()
  118. }else {
  119. Toast.fail(response.data.message, 2)
  120. }
  121. }).catch(error => {
  122. Toast.hide()
  123. this.setState({
  124. obtainText: '获取验证码'
  125. })
  126. Toast.fail(error || '获取验证码失败', 2)
  127. })
  128. }
  129. countdown = () => {
  130. setTimeout(() => {
  131. if (mSeconds > 0) {
  132. mSeconds--
  133. this.setState({
  134. obtainText: '重新发送(' + mSeconds + ')'
  135. })
  136. this.countdown()
  137. } else {
  138. this.setState({
  139. sendCodeSuccess:false,
  140. obtainText: '重新发送(59)'
  141. })
  142. }
  143. }, 1000)
  144. }
  145. handelValueCom = (event) => {
  146. //获取手机号
  147. let phoneNumber = this.refs.phoneNumber.value;
  148. //获得验证码
  149. let vCode = this.refs.vCode.value;
  150. this.setState({
  151. phoneNumber: phoneNumber,
  152. vCode:vCode
  153. })
  154. }
  155. componentWillMount() {
  156. document.title='更换手机号码'
  157. }
  158. componentDidMount() {
  159. }
  160. }
  161. let mapStateToProps = (state) => ({
  162. userInfo: {...state.redUserInfo}
  163. })
  164. let mapDispatchToProps = (dispatch) => ({})
  165. export default connect(mapStateToProps, mapDispatchToProps)(ChangePhoneNumber)