page.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.validMobileNumber = 0;
  18. this.validEmailNumber = 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.validMobileNumber += 1;
  31. const number = this.validMobileNumber;
  32. User.validWechat(area, mobile)
  33. .then(result => {
  34. if (result) {
  35. this.setState({ mobileError: '' });
  36. return User.validMobile(area, mobile).then(r => {
  37. if (number !== this.validMobileNumber) 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. validEmail() {
  49. const { data } = this.state;
  50. const { email } = data;
  51. if (!email) return;
  52. this.validEmailNumber += 1;
  53. const number = this.validEmailNumber;
  54. User.validEmail(email)
  55. .then(result => {
  56. if (number !== this.validEmailNumber) return Promise.resolve();
  57. if (result) {
  58. this.setState({ emailError: '' });
  59. return Promise.resolve();
  60. }
  61. return Promise.reject(new Error('该邮箱已绑定其他账号,请更换邮箱地址'));
  62. })
  63. .catch(err => {
  64. this.setState({ emailError: err.message });
  65. });
  66. }
  67. sendValid() {
  68. const { data, mobileError } = this.state;
  69. const { area, mobile } = data;
  70. if (!area || !mobile || mobileError) return Promise.reject();
  71. return Common.sendSms(area, mobile)
  72. .then(result => {
  73. if (result) {
  74. asyncSMessage('发送成功');
  75. this.setState({ mobileError: '', validError: '' });
  76. } else {
  77. throw new Error('发送失败');
  78. }
  79. })
  80. .catch(err => {
  81. this.setState({ mobileError: err.message });
  82. throw err;
  83. });
  84. }
  85. submit() {
  86. const { data, needEmail, mobileError, emailError, validError } = this.state;
  87. const { area, mobile, mobileVerifyCode, email } = data;
  88. if (mobileError || emailError || validError) return;
  89. if (!area || !mobile || !mobileVerifyCode) return;
  90. if (needEmail && !email) return;
  91. User.bind(area, mobile, mobileVerifyCode, email)
  92. .then(() => {
  93. const { url } = this.props.core.query;
  94. if (url) {
  95. toLink(url);
  96. } else {
  97. linkTo('/product');
  98. }
  99. })
  100. .catch(err => {
  101. if (err.message.indexOf('验证码') >= 0) {
  102. this.setState({ validError: err.message });
  103. } else if (err.message.indexOf('邮箱') >= 0) {
  104. this.setState({ emailError: err.message });
  105. } else {
  106. this.setState({ mobileError: err.message });
  107. }
  108. });
  109. }
  110. renderView() {
  111. const { needEmail } = this.state;
  112. return (
  113. <div>
  114. <SelectInput
  115. placeholder="请输入手机号"
  116. selectValue={this.state.data.area}
  117. select={MobileArea}
  118. value={this.state.data.mobile}
  119. error={this.state.mobileError}
  120. onSelect={value => {
  121. this.changeData('area', value);
  122. this.validMobile();
  123. }}
  124. onChange={e => {
  125. this.changeData('mobile', e.target.value);
  126. this.validMobile();
  127. }}
  128. />
  129. <VerificationInput
  130. placeholder="请输入验证码"
  131. value={this.state.data.mobileVerifyCode}
  132. error={this.state.validError}
  133. onSend={() => {
  134. return this.sendValid();
  135. }}
  136. onChange={e => {
  137. this.changeData('mobileVerifyCode', e.target.value);
  138. }}
  139. />
  140. {needEmail && (
  141. <Input
  142. placeholder="请输入邮箱"
  143. value={this.state.data.email}
  144. onChange={e => {
  145. this.changeData('email', e.target.value);
  146. }}
  147. />
  148. )}
  149. <Button
  150. margin={25}
  151. radius
  152. block
  153. disabled={this.state.validError || this.state.mobileError}
  154. onClick={() => {
  155. this.submit();
  156. }}
  157. >
  158. 绑定
  159. </Button>
  160. </div>
  161. );
  162. }
  163. }