page.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import React from 'react';
  2. import './index.less';
  3. import Page from '@src/containers/Page';
  4. import { asyncSMessage } from '@src/services/AsyncTools';
  5. import { MobileArea } from '../../../../Constant';
  6. import Input, { SelectInput, VerificationInput } from '../../../components/Input';
  7. import Button from '../../../components/Button';
  8. import { User } from '../../../stores/user';
  9. import { My } from '../../../stores/my';
  10. import { Common } from '../../../stores/common';
  11. export default class extends Page {
  12. initState() {
  13. return {
  14. data: { mobile: '', area: MobileArea[0].value, email: '' },
  15. };
  16. }
  17. init() { }
  18. changeData(field, value) {
  19. let { data } = this.state;
  20. data = data || {};
  21. data[field] = value;
  22. this.setState({ data });
  23. }
  24. validMobile() {
  25. const { data } = this.state;
  26. const { area, mobile } = data;
  27. if (area === '' || mobile === '') return;
  28. User.validWechat(area, mobile)
  29. .then(result => {
  30. if (result) {
  31. this.setState({ mobileError: '' });
  32. return User.validMobile(area, mobile)
  33. .then(r => {
  34. this.setState({ needEmail: r });
  35. });
  36. }
  37. this.setState({ needEmail: false });
  38. return Promise.reject(new Error('该手机已绑定其他账号,请更换手机号码'));
  39. })
  40. .catch(err => {
  41. this.setState({ mobileError: err.message });
  42. });
  43. }
  44. sendValid() {
  45. const { data, mobileError } = this.state;
  46. const { area, mobile } = data;
  47. if (area === '' || mobile === '' || mobileError !== '') return Promise.reject();
  48. return Common.sendSms(area, mobile)
  49. .then((result) => {
  50. if (result) {
  51. asyncSMessage('发送成功');
  52. this.setState({ mobileError: '', validError: '' });
  53. } else {
  54. throw new Error('发送失败');
  55. }
  56. })
  57. .catch(err => {
  58. this.setState({ mobileError: err.message });
  59. throw err;
  60. });
  61. }
  62. submit() {
  63. const { data, needEmail, mobileError, validError } = this.state;
  64. const { area, mobile, mobileVerifyCode, email } = data;
  65. if (mobileError !== '' || validError !== '') return;
  66. if (area === '' || mobile === '' || mobileVerifyCode === '') return;
  67. if (needEmail && email === '') return;
  68. User.bind(area, mobile, mobileVerifyCode).then(() => {
  69. let handler = null;
  70. if (needEmail) {
  71. handler = My.bindEmail(email);
  72. } else {
  73. handler = Promise.resolve();
  74. }
  75. handler.then(() => {
  76. linkTo('/identity');
  77. });
  78. })
  79. .catch(err => {
  80. if (err.message.indexOf('验证码') >= 0) {
  81. this.setState({ validError: err.message });
  82. } else {
  83. this.setState({ mobileError: err.message });
  84. }
  85. });
  86. }
  87. renderView() {
  88. const { needEmail } = this.state;
  89. return (
  90. <div>
  91. <SelectInput placeholder="请输入手机号" selectValue={this.state.data.area} select={MobileArea} value={this.state.data.mobile} error={this.state.mobileError} onSelect={(value) => {
  92. this.changeData('area', value);
  93. }} onChange={(e) => {
  94. this.changeData('mobile', e.target.value);
  95. this.validMobile(e.target.value);
  96. }} />
  97. <VerificationInput placeholder="请输入验证码" value={this.state.data.mobileVerifyCode} error={this.state.validError} onSend={() => {
  98. return this.sendValid();
  99. }} />
  100. {needEmail && <Input placeholder="请输入邮箱" value={this.state.data.email} onChange={(e) => {
  101. this.changeData('email', e.target.value);
  102. }} />}
  103. <Button margin={25} radius block onClick={() => {
  104. this.submit();
  105. }}>
  106. 绑定
  107. </Button>
  108. </div>
  109. );
  110. }
  111. }