page.js 5.2 KB

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