page.js 3.7 KB

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