changeMobile.jsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import React from 'react';
  2. import ajax from 'jquery/src/ajax/xhr.js'
  3. import $ from 'jquery/src/ajax';
  4. import { Modal, Button, Icon, Input, message } from 'antd';
  5. import './changeModal.less';
  6. const ChangePw = React.createClass({
  7. getInitialState() {
  8. return {
  9. visible: false,
  10. getCodeButton: false,
  11. checkMobile: false,
  12. vsCodeSrc: globalConfig.context + '/open/getVCode',
  13. mCodeText: '获取验证码'
  14. };
  15. },
  16. showModal() {
  17. this.setState({
  18. visible: true,
  19. });
  20. },
  21. handleOk() {
  22. if (!this.state.checkMobile) {
  23. message.warning('请输入正确的手机号码!');
  24. } else if (this.state.checkMobile) {
  25. $.ajax({
  26. method: "POST",
  27. dataType: "json",
  28. crossDomain: false,
  29. url: globalConfig.context + "/api/user/mobile",
  30. data: {
  31. "mobile": this.state.mobile,
  32. "mobileCode": this.state.mcode
  33. }
  34. }).done(function (data) {
  35. if (!data.error.length) {
  36. this.setState({
  37. visible: false,
  38. });
  39. message.success('修改成功!3秒后跳转到登录页面,请重新登录!');
  40. setInterval(function () {
  41. $.ajax({
  42. method: "get",
  43. dataType: "json",
  44. url: globalConfig.context + "/login",
  45. }).done(function (data) {
  46. window.location.href = globalConfig.context + "/user/login.html"
  47. });
  48. clearInterval(interval);
  49. }, 3000);
  50. } else {
  51. message.warning(data.error[0].message);
  52. };
  53. }.bind(this));
  54. };
  55. },
  56. handleCancel(e) {
  57. this.setState({
  58. visible: false,
  59. });
  60. },
  61. reloadVCode() {
  62. this.setState({
  63. vsCodeSrc: globalConfig.context + '/open/getVCode?t=' + Math.random()
  64. });
  65. },
  66. newMobileChange(e) {
  67. this.state.mobile = e.target.value;
  68. if (/^1[34578]\d{9}$/.test(this.state.mobile)) {
  69. this.state.checkMobile = true
  70. }
  71. },
  72. mcodeChange(e) {
  73. this.state.mcode = e.target.value;
  74. },
  75. getMCode() {
  76. if (!this.state.checkMobile) {
  77. message.warning('请输入正确的手机号码!');
  78. } else if (this.state.checkMobile) {
  79. $.ajax({
  80. method: "get",
  81. dataType: "json",
  82. url: globalConfig.context + "/open/getMCode",
  83. data: {
  84. "mobile": this.state.mobile,
  85. "verificationCode": this.state.Vcode
  86. },
  87. success: function (data) {
  88. let _me = this;
  89. if (data.error && data.error.length) {
  90. message.warning(data.error[0].message);
  91. this.reloadVCode();
  92. } else {
  93. message.success('验证码发送成功!');
  94. this.setState({
  95. getCodeButton: true
  96. });
  97. let interval = setInterval(function () {
  98. _me.setState({
  99. getCodeButton: false
  100. });
  101. clearInterval(interval);
  102. }, 60000);
  103. };
  104. let i = 59;
  105. let countDown = setInterval(function () {
  106. _me.setState({
  107. mCodeText: '剩余' + i + '秒'
  108. });
  109. i--;
  110. if (i == 0) {
  111. clearInterval(countDown);
  112. _me.setState({
  113. mCodeText: '获取验证码'
  114. });
  115. };
  116. }, 1000);
  117. this.reloadVCode();
  118. }.bind(this),
  119. });
  120. }
  121. },
  122. render() {
  123. return (
  124. <div style={{ 'float': 'left', 'marginTop': '0' }}>
  125. <Button type="ghost" onClick={this.showModal}>更换手机</Button>
  126. <Modal maskClosable={false} className="change-modal" title="更换手机号码" visible={this.state.visible}
  127. onOk={this.handleOk} onCancel={this.handleCancel}
  128. width={360}
  129. footer={[
  130. <Button key="submit" type="primary" size="large" loading={this.state.loading} onClick={this.handleOk}>确认</Button>,
  131. <Button key="back" type="ghost" size="large" onClick={this.handleCancel}>取消</Button>
  132. ]}
  133. >
  134. <div><span>新手机号码:</span><Input onChange={this.newMobileChange} /></div>
  135. <div>
  136. <span>图形验证码:</span>
  137. <Input className="mixwidth" onChange={(e) => { this.state.Vcode = e.target.value; }} />
  138. <span className="getVcode">
  139. <img id="VCode" src={this.state.vsCodeSrc} alt="点击刷新验证码" onClick={this.reloadVCode} />
  140. </span>
  141. </div>
  142. <div><span>手机验证码:</span><Input className="mixwidth" onChange={this.mcodeChange} />
  143. <Button style={{width:'92px'}} onClick={this.getMCode} disabled={this.state.getCodeButton}>{this.state.mCodeText}</Button></div>
  144. </Modal>
  145. </div>
  146. );
  147. },
  148. });
  149. export default ChangePw;