page.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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).then(r => {
  36. if (number !== this.validNumber) return;
  37. this.setState({ needEmail: r });
  38. });
  39. }
  40. this.setState({ needEmail: false });
  41. return login ? Promise.resolve() : Promise.reject(new Error('该手机已绑定其他账号,请更换手机号码'));
  42. })
  43. .catch(err => {
  44. this.setState({ mobileError: err.message });
  45. });
  46. }
  47. sendValid() {
  48. const { data, mobileError } = this.state;
  49. const { area, mobile } = data;
  50. if (!area || !mobile || mobileError) return Promise.reject();
  51. return Common.sendSms(area, mobile)
  52. .then(result => {
  53. if (result) {
  54. asyncSMessage('发送成功');
  55. this.setState({ mobileError: '', validError: '' });
  56. } else {
  57. throw new Error('发送失败');
  58. }
  59. })
  60. .catch(err => {
  61. this.setState({ mobileError: err.message });
  62. throw err;
  63. });
  64. }
  65. submit() {
  66. const { data, needEmail, mobileError, validError } = this.state;
  67. const { area, mobile, mobileVerifyCode, email } = data;
  68. if (mobileError || validError) return;
  69. if (!area || !mobile || !mobileVerifyCode) return;
  70. if (needEmail && !email) return;
  71. User.bind(area, mobile, mobileVerifyCode, email)
  72. .then(() => {
  73. const { url } = this.props.core.query;
  74. if (url) {
  75. toLink(url);
  76. } else {
  77. linkTo('/product');
  78. }
  79. })
  80. .catch(err => {
  81. console.log(err.message);
  82. if (err.message.indexOf('验证码') >= 0) {
  83. this.setState({ validError: err.message });
  84. } else {
  85. this.setState({ mobileError: err.message });
  86. }
  87. });
  88. }
  89. renderView() {
  90. const { needEmail } = this.state;
  91. return (
  92. <div>
  93. <SelectInput
  94. placeholder="请输入手机号"
  95. selectValue={this.state.data.area}
  96. select={MobileArea}
  97. value={this.state.data.mobile}
  98. error={this.state.mobileError}
  99. onSelect={value => {
  100. this.changeData('area', value);
  101. this.validMobile();
  102. }}
  103. onChange={e => {
  104. this.changeData('mobile', e.target.value);
  105. this.validMobile();
  106. }}
  107. />
  108. <VerificationInput
  109. placeholder="请输入验证码"
  110. value={this.state.data.mobileVerifyCode}
  111. error={this.state.validError}
  112. onSend={() => {
  113. return this.sendValid();
  114. }}
  115. onChange={e => {
  116. this.changeData('mobileVerifyCode', e.target.value);
  117. }}
  118. />
  119. {needEmail && (
  120. <Input
  121. placeholder="请输入邮箱"
  122. value={this.state.data.email}
  123. onChange={e => {
  124. this.changeData('email', e.target.value);
  125. }}
  126. />
  127. )}
  128. <Button
  129. margin={25}
  130. radius
  131. block
  132. disabled={this.state.validError || this.state.mobileError}
  133. onClick={() => {
  134. this.submit();
  135. }}
  136. >
  137. 绑定
  138. </Button>
  139. </div>
  140. );
  141. }
  142. }