page.js 5.7 KB

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