page.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. this.validNumber = 0;
  19. }
  20. changeData(field, value) {
  21. let { data } = this.state;
  22. data = data || {};
  23. data[field] = value;
  24. this.setState({ data });
  25. }
  26. validMobile(login) {
  27. const { data } = this.state;
  28. const { area, mobile } = data;
  29. if (!area || !mobile) return;
  30. this.validNumber += 1;
  31. const number = this.validNumber;
  32. User.validWechat(area, mobile)
  33. .then(result => {
  34. if (result) {
  35. this.setState({ mobileError: '' });
  36. return User.validMobile(area, mobile)
  37. .then(r => {
  38. if (number !== this.validNumber) return;
  39. this.setState({ needEmail: r });
  40. });
  41. }
  42. this.setState({ needEmail: false });
  43. return login ? Promise.resolve() : Promise.reject(new Error('该手机已绑定其他账号,请更换手机号码'));
  44. })
  45. .catch(err => {
  46. this.setState({ mobileError: err.message });
  47. });
  48. }
  49. sendValid() {
  50. const { data, mobileError } = this.state;
  51. const { area, mobile } = data;
  52. if (!area || !mobile || mobileError) return Promise.reject();
  53. return Common.sendSms(area, mobile)
  54. .then((result) => {
  55. if (result) {
  56. asyncSMessage('发送成功');
  57. this.setState({ mobileError: '', validError: '' });
  58. } else {
  59. throw new Error('发送失败');
  60. }
  61. })
  62. .catch(err => {
  63. this.setState({ mobileError: err.message });
  64. throw err;
  65. });
  66. }
  67. submit() {
  68. const { data, needEmail, mobileError, validError } = this.state;
  69. const { area, mobile, mobileVerifyCode, email } = data;
  70. if (mobileError || validError) return;
  71. if (!area || !mobile || !mobileVerifyCode) return;
  72. if (needEmail && !email) return;
  73. User.bind(area, mobile, mobileVerifyCode).then(() => {
  74. let handler = null;
  75. if (needEmail) {
  76. handler = My.bindEmail(email);
  77. } else {
  78. handler = Promise.resolve();
  79. }
  80. handler.then(() => {
  81. linkTo('/');
  82. });
  83. })
  84. .catch(err => {
  85. if (err.message.indexOf('验证码') >= 0) {
  86. this.setState({ validError: err.message });
  87. } else {
  88. this.setState({ mobileError: err.message });
  89. }
  90. });
  91. }
  92. renderView() {
  93. const { needEmail } = this.state;
  94. return (
  95. <div>
  96. <SelectInput placeholder="请输入手机号" selectValue={this.state.data.area} select={MobileArea} value={this.state.data.mobile} error={this.state.mobileError} onSelect={(value) => {
  97. this.changeData('area', value);
  98. this.validMobile();
  99. }} onChange={(e) => {
  100. this.changeData('mobile', e.target.value);
  101. this.validMobile();
  102. }} />
  103. <VerificationInput placeholder="请输入验证码" value={this.state.data.mobileVerifyCode} error={this.state.validError} onSend={() => {
  104. return this.sendValid();
  105. }} onChange={(e) => {
  106. this.changeData('mobileVerifyCode', e.target.value);
  107. }} />
  108. {needEmail && <Input placeholder="请输入邮箱" value={this.state.data.email} onChange={(e) => {
  109. this.changeData('email', e.target.value);
  110. }} />}
  111. <Button margin={25} radius block onClick={() => {
  112. this.submit();
  113. }}>
  114. 绑定
  115. </Button>
  116. </div>
  117. );
  118. }
  119. }