page.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. if (value) 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 || (needEmail && !email)) {
  92. this.setState({ error: { mobile: !mobile, mobileVerifyCode: !mobileVerifyCode, email: !email } });
  93. return;
  94. }
  95. if (needEmail && !email) return;
  96. User.bind(area, mobile, mobileVerifyCode, email)
  97. .then(() => {
  98. const { url } = this.props.core.query;
  99. if (url) {
  100. toLink(url);
  101. } else {
  102. linkTo('/product');
  103. }
  104. })
  105. .catch(err => {
  106. if (err.message.indexOf('验证码') >= 0) {
  107. this.setState({ validError: err.message });
  108. } else if (err.message.indexOf('邮箱') >= 0) {
  109. this.setState({ emailError: err.message });
  110. } else {
  111. this.setState({ mobileError: err.message });
  112. }
  113. });
  114. }
  115. renderView() {
  116. const { needEmail } = this.state;
  117. return (
  118. <div>
  119. <SelectInput
  120. placeholder="请输入手机号"
  121. selectValue={this.state.data.area}
  122. select={MobileArea}
  123. value={this.state.data.mobile}
  124. error={this.state.mobileError}
  125. empty={this.state.empty.mobile}
  126. onSelect={value => {
  127. this.changeData('area', value);
  128. this.validMobile();
  129. }}
  130. onChange={e => {
  131. this.changeData('mobile', e.target.value);
  132. this.validMobile();
  133. }}
  134. />
  135. <VerificationInput
  136. placeholder="请输入验证码"
  137. value={this.state.data.mobileVerifyCode}
  138. error={this.state.validError}
  139. empty={this.state.empty.mobileVerifyCode}
  140. onSend={() => {
  141. return this.sendValid();
  142. }}
  143. onChange={e => {
  144. this.changeData('mobileVerifyCode', e.target.value);
  145. this.setState({ validError: '' });
  146. }}
  147. />
  148. {needEmail && (
  149. <Input
  150. placeholder="请输入邮箱"
  151. value={this.state.data.email}
  152. empty={this.state.empty.email}
  153. onChange={e => {
  154. this.changeData('email', e.target.value);
  155. }}
  156. />
  157. )}
  158. <Button
  159. margin={25}
  160. radius
  161. block
  162. disabled={this.state.validError || this.state.mobileError}
  163. onClick={() => {
  164. this.submit();
  165. }}
  166. >
  167. 绑定
  168. </Button>
  169. </div>
  170. );
  171. }
  172. }