index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. import React, { Component } from 'react';
  2. import './index.less';
  3. import { Modal, Button, Tooltip } from 'antd';
  4. import Assets from '@src/components/Assets';
  5. import { asyncSMessage } from '@src/services/AsyncTools';
  6. import { checkEmail, checkMobile } from '@src/services/Tools';
  7. import { Icon as GIcon } from '../Icon';
  8. import { Button as GButton } from '../Button';
  9. import RadioItem from '../RadioItem';
  10. import { User } from '../../stores/user';
  11. import { Common } from '../../stores/common';
  12. import { Main } from '../../stores/main';
  13. import { MobileArea, WechatPcAppId, PcUrl } from '../../../Constant';
  14. import { DefaultInput, SelectInput, VerificationInput } from '../Input';
  15. const LOGIN_PHONE = 'LOGIN_PHONE';
  16. const LOGIN_WX = 'LOGIN_WX';
  17. const BIND_PHONE = 'BIND_PHONE';
  18. const BIND_WX = 'BIND_WX';
  19. const BIND_WX_ERROR = 'BIND_WX_ERROR';
  20. export default class Login extends Component {
  21. constructor(props) {
  22. super(props);
  23. this.validNumber = 0;
  24. this.needScan = true;
  25. this.state = { scanNumber: 0, type: LOGIN_WX, empty: {}, data: { area: MobileArea[0].value } };
  26. this.validMobileNumber = 0;
  27. this.validEmailNumber = 0;
  28. window.addEventListener(
  29. 'message',
  30. event => {
  31. if (typeof event.data === 'string' && event.data.indexOf('code:') === 0) {
  32. if (!this.needScan) {
  33. return;
  34. }
  35. this.needScan = false;
  36. const code = event.data.split(':')[1];
  37. if (this.state.type === LOGIN_WX) {
  38. this.scanLogin(code);
  39. } else if (this.state.type === BIND_WX) {
  40. this.scanBind(code);
  41. }
  42. }
  43. },
  44. false,
  45. );
  46. }
  47. componentWillReceiveProps(nextProps) {
  48. if (nextProps.user.needLogin && !this.init) {
  49. this.init = true;
  50. Main.getContract('register').then(result => {
  51. this.setState({ registerContract: result });
  52. });
  53. Main.getContract('privacy').then(result => {
  54. this.setState({ privacyContract: result });
  55. });
  56. }
  57. }
  58. close() {
  59. User.closeLogin();
  60. }
  61. login() {
  62. const { data, needEmail, mobileError, validError, emailError, empty } = this.state;
  63. const { area, mobile, mobileVerifyCode, email } = data;
  64. if (mobileError || emailError || validError) return;
  65. if (!area || !mobile || !mobileVerifyCode || (needEmail && !email)) {
  66. empty[LOGIN_PHONE] = { mobile: !mobile, mobileVerifyCode: !mobileVerifyCode, email: !email };
  67. this.setState({ empty });
  68. return;
  69. }
  70. empty[LOGIN_PHONE] = {};
  71. this.setState({ empty });
  72. User.login(area, mobile, mobileVerifyCode, email, null, false)
  73. .then(result => {
  74. if (result.bindWechat) {
  75. this.close();
  76. } else {
  77. this.changeType(BIND_WX);
  78. }
  79. })
  80. .catch(err => {
  81. if (err.message.indexOf('验证码') >= 0) {
  82. this.setState({ validError: err.message });
  83. } else if (err.message.indexOf('邮箱') >= 0) {
  84. this.setState({ emailError: err.message });
  85. } else {
  86. this.setState({ mobileError: err.message });
  87. }
  88. });
  89. }
  90. bind() {
  91. const { data, needEmail, mobileError, emailError, validError, empty } = this.state;
  92. const { area, mobile, mobileVerifyCode, email } = data;
  93. if (mobileError || emailError || validError) return;
  94. if (!area || !mobile || !mobileVerifyCode || (needEmail && !email)) {
  95. empty[BIND_PHONE] = { mobile: !mobile, mobileVerifyCode: !mobileVerifyCode, email: !email };
  96. this.setState({ empty });
  97. return;
  98. }
  99. empty[BIND_PHONE] = {};
  100. this.setState({ empty });
  101. User.bind(area, mobile, mobileVerifyCode, email)
  102. .then(() => {
  103. this.close();
  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. scanLogin(code) {
  116. User.loginWechat(code, true, false)
  117. .then(result => {
  118. this.needScan = false;
  119. if (result.bindMobile) {
  120. this.close();
  121. } else {
  122. this.changeType(BIND_PHONE);
  123. }
  124. })
  125. .catch(err => {
  126. this.needScan = true;
  127. if (err.message !== '登录失败') {
  128. asyncSMessage(err.message, 'error');
  129. }
  130. });
  131. }
  132. scanBind(code) {
  133. User.loginWechat(code, false, false)
  134. .then(() => {
  135. this.needScan = false;
  136. this.close();
  137. })
  138. .catch(() => {
  139. this.needScan = true;
  140. this.changeType(BIND_WX_ERROR);
  141. });
  142. }
  143. changeData(field, value) {
  144. let { data, empty } = this.state;
  145. data = data || {};
  146. empty = empty || {};
  147. empty[this.state.type] = empty[this.state.type] || {};
  148. data[field] = value;
  149. if (value) empty[this.state.type][field] = !value;
  150. this.setState({ data, empty });
  151. }
  152. validMobile(login) {
  153. const { data } = this.state;
  154. const { area, mobile } = data;
  155. if (!area || !mobile) return;
  156. if (!checkMobile(mobile)) {
  157. this.setState({ mobileError: '请输入正确的手机号' });
  158. return;
  159. }
  160. this.setState({ mobileError: null });
  161. this.validMobileNumber += 1;
  162. const number = this.validMobileNumber;
  163. User.validWechat(area, mobile)
  164. .then(result => {
  165. if (result) {
  166. this.setState({ mobileError: '' });
  167. return User.validMobile(area, mobile).then(r => {
  168. if (number !== this.validMobileNumber) return;
  169. this.setState({ needEmail: r });
  170. });
  171. }
  172. this.setState({ needEmail: false });
  173. return login ? Promise.resolve() : Promise.reject(new Error('该手机已绑定其他账号,请更换手机号码'));
  174. })
  175. .catch(err => {
  176. this.setState({ mobileError: err.message });
  177. });
  178. }
  179. validEmail() {
  180. const { data } = this.state;
  181. const { email } = data;
  182. if (!email) return;
  183. if (!checkEmail(email)) {
  184. this.setState({ emailError: '请输入正确的邮箱' });
  185. return;
  186. }
  187. this.setState({ emailError: null });
  188. this.validEmailNumber += 1;
  189. const number = this.validEmailNumber;
  190. User.validEmail(email)
  191. .then(result => {
  192. if (number !== this.validEmailNumber) return Promise.resolve();
  193. if (result) {
  194. this.setState({ emailError: '' });
  195. return Promise.resolve();
  196. }
  197. return Promise.reject(new Error('该邮箱已绑定其他账号,请更换邮箱地址'));
  198. })
  199. .catch(err => {
  200. this.setState({ emailError: err.message });
  201. });
  202. }
  203. sendValid() {
  204. const { data, mobileError } = this.state;
  205. const { area, mobile } = data;
  206. if (!area || !mobile || mobileError) return Promise.reject();
  207. return Common.sendSms(area, mobile)
  208. .then(result => {
  209. if (result) {
  210. asyncSMessage('发送成功');
  211. this.setState({ mobileError: '', validError: '' });
  212. } else {
  213. throw new Error('发送失败');
  214. }
  215. })
  216. .catch(err => {
  217. this.setState({ mobileError: err.message });
  218. throw err;
  219. });
  220. }
  221. changeType(nextType) {
  222. let { scanNumber } = this.state;
  223. if (nextType === LOGIN_WX || nextType === BIND_WX) {
  224. scanNumber += 1;
  225. }
  226. this.setState({ scanNumber, type: nextType, empty: {}, mobileError: '', emailError: '', validError: '', data: { area: MobileArea[0].value } });
  227. }
  228. render() {
  229. const { type } = this.state;
  230. const { user } = this.props;
  231. return (
  232. <Modal
  233. ref={ref => {
  234. this.modalR = ref;
  235. }}
  236. wrapClassName={`login-modal ${type}`}
  237. visible={user.needLogin}
  238. footer={null}
  239. closable={false}
  240. width={470}
  241. >
  242. {this.renderBody(type)}
  243. <GIcon
  244. name="close"
  245. onClick={() => {
  246. if (type === BIND_WX_ERROR) {
  247. // 绑定微信错误,返回重新绑定微信
  248. this.changeType(BIND_WX);
  249. } else {
  250. User.closeLogin(new Error('no login'));
  251. this.changeType(LOGIN_WX);
  252. }
  253. }}
  254. />
  255. </Modal>
  256. );
  257. }
  258. renderBody(type) {
  259. switch (type) {
  260. case LOGIN_WX:
  261. return this.renderLoginWx();
  262. case BIND_PHONE:
  263. return this.renderBindPhone();
  264. case BIND_WX:
  265. return this.renderBindWx();
  266. case BIND_WX_ERROR:
  267. return this.renderBindWxError();
  268. case LOGIN_PHONE:
  269. default:
  270. return this.renderLoginPhone();
  271. }
  272. }
  273. renderLoginPhone() {
  274. const { needEmail, registerContract = {}, privacyContract, empty = {} } = this.state;
  275. const emptyError = empty[LOGIN_PHONE] || {};
  276. return (
  277. <div className="body">
  278. <div className="title">手机号登录</div>
  279. <div className="tip" hidden={!needEmail}>
  280. <Assets name="notice" />
  281. 该手机号尚未注册,将自动为您注册账户
  282. </div>
  283. <SelectInput
  284. placeholder="请输入手机号"
  285. selectValue={this.state.data.area}
  286. select={MobileArea}
  287. value={this.state.data.mobile}
  288. error={this.state.mobileError}
  289. empty={emptyError.mobile}
  290. onSelect={value => {
  291. this.changeData('area', value);
  292. this.validMobile(true);
  293. }}
  294. onChange={e => {
  295. this.changeData('mobile', e.target.value);
  296. this.validMobile(true);
  297. }}
  298. />
  299. <VerificationInput
  300. placeholder="请输入验证码"
  301. value={this.state.data.mobileVerifyCode}
  302. error={this.state.validError}
  303. empty={emptyError.mobileVerifyCode}
  304. onSend={() => {
  305. return this.sendValid();
  306. }}
  307. onChange={e => {
  308. this.changeData('mobileVerifyCode', e.target.value);
  309. this.setState({ validError: '' });
  310. }}
  311. />
  312. {needEmail && (
  313. <DefaultInput
  314. placeholder="请输入邮箱"
  315. value={this.state.data.email}
  316. empty={emptyError.email}
  317. error={this.state.emailError}
  318. onChange={e => {
  319. this.changeData('email', e.target.value);
  320. this.validEmail();
  321. }}
  322. />
  323. )}
  324. {needEmail && (
  325. <div className="m-b-2">
  326. <RadioItem checked theme="white" className="m-r-5" />
  327. 我已同意{' '}
  328. <a href={`/contract/${registerContract.key}`} target="_blank">
  329. 《{registerContract.title}》
  330. </a>{' '}
  331. <a href={`/contract/${privacyContract.key}`} target="_blank">
  332. 《{privacyContract.title}》
  333. </a>
  334. </div>
  335. )}
  336. <Button
  337. type="primary"
  338. size="large"
  339. block
  340. onClick={() => {
  341. this.login();
  342. }}
  343. >
  344. 登录
  345. </Button>
  346. <Tooltip overlayClassName="gray" placement="left" title="微信扫码登录">
  347. <a
  348. className="other"
  349. onClick={() => {
  350. this.changeType(LOGIN_WX);
  351. }}
  352. >
  353. <Assets name="code" />
  354. </a>
  355. </Tooltip>
  356. </div>
  357. );
  358. }
  359. renderLoginWx() {
  360. return (
  361. <div className="body">
  362. <div className="title">微信扫码登录</div>
  363. <div className="qr-code">
  364. <iframe
  365. key={this.state.scanNumber}
  366. frameBorder="0"
  367. src={`/login.html?appid=${WechatPcAppId}&redirectUri=${encodeURIComponent(PcUrl)}`}
  368. width="300"
  369. height="300"
  370. />
  371. <div className="text">请使用微信扫描二维码登录</div>
  372. </div>
  373. <Tooltip overlayClassName="gray" placement="left" title="手机号登录">
  374. <a
  375. className="other"
  376. onClick={() => {
  377. this.changeType(LOGIN_PHONE);
  378. }}
  379. >
  380. <Assets name="phone" />
  381. </a>
  382. </Tooltip>
  383. </div>
  384. );
  385. }
  386. renderBindPhone() {
  387. const { needEmail, registerContract = {}, privacyContract = {}, empty = {} } = this.state;
  388. const emptyError = empty[BIND_PHONE] || {};
  389. return (
  390. <div className="body">
  391. <div className="title">绑定手机号</div>
  392. <div className="tip">
  393. <Assets name="notice" />
  394. 微信登录成功!为更好的使用服务,请您绑定手机号和邮箱。
  395. </div>
  396. <SelectInput
  397. placeholder="请输入手机号"
  398. selectValue={this.state.data.area}
  399. select={MobileArea}
  400. value={this.state.data.mobile}
  401. error={this.state.mobileError}
  402. empty={emptyError.mobile}
  403. onSelect={value => {
  404. this.changeData('area', value);
  405. this.validMobile(false);
  406. }}
  407. onChange={e => {
  408. this.changeData('mobile', e.target.value);
  409. this.validMobile(false);
  410. }}
  411. />
  412. <VerificationInput
  413. placeholder="请输入验证码"
  414. value={this.state.data.mobileVerifyCode}
  415. error={this.state.validError}
  416. empty={emptyError.mobileVerifyCode}
  417. onSend={() => {
  418. return this.sendValid();
  419. }}
  420. onChange={e => {
  421. this.changeData('mobileVerifyCode', e.target.value);
  422. this.setState({ validError: '' });
  423. }}
  424. />
  425. {needEmail && (
  426. <DefaultInput
  427. placeholder="请输入邮箱"
  428. value={this.state.data.email}
  429. empty={emptyError.email}
  430. error={this.state.emailError}
  431. onChange={e => {
  432. this.changeData('email', e.target.value);
  433. this.validEmail();
  434. }}
  435. />
  436. )}
  437. {needEmail && (
  438. <div className="m-b-2">
  439. <RadioItem checked theme="white" className="m-r-5" />
  440. 我已同意{' '}
  441. <a href={`/contract/${registerContract.key}`} target="_blank">
  442. 《{registerContract.title}》
  443. </a>{' '}
  444. <a href={`/contract/${privacyContract.key}`} target="_blank">
  445. 《{privacyContract.title}》
  446. </a>
  447. </div>
  448. )}
  449. {this.state.mobileError && (
  450. <div className="m-b-2">
  451. <a className="f-r" onClick={() => this.changeType(LOGIN_PHONE)}>
  452. 使用手机号码登录
  453. </a>
  454. </div>
  455. )}
  456. <Button
  457. type="primary"
  458. size="large"
  459. disabled={this.state.validError || this.state.mobileError}
  460. block
  461. onClick={() => {
  462. this.bind();
  463. }}
  464. >
  465. 绑定
  466. </Button>
  467. </div>
  468. );
  469. }
  470. renderBindWx() {
  471. return (
  472. <div className="body">
  473. <div className="title">绑定微信号</div>
  474. <div className="tip">
  475. <Assets name="notice" />
  476. 手机号注册成功!为更好的使用服务,建议您绑定微信号。
  477. </div>
  478. <div className="qr-code">
  479. <iframe
  480. key={this.state.scanNumber}
  481. frameBorder="0"
  482. src={`/login.html?appid=${WechatPcAppId}&redirectUri=${encodeURIComponent(PcUrl)}`}
  483. width="300"
  484. height="300"
  485. />
  486. <div className="text">请使用微信扫描二维码登录</div>
  487. <div
  488. className="jump"
  489. onClick={() => {
  490. this.close();
  491. }}
  492. >
  493. 跳过
  494. </div>
  495. </div>
  496. </div>
  497. );
  498. }
  499. renderBindWxError() {
  500. return (
  501. <div className="body">
  502. <div className="title">绑定失败</div>
  503. <div className="text">该微信账户已绑定其他手机号码,您可直接扫码登入。</div>
  504. <div className="btn">
  505. <GButton
  506. radius
  507. onClick={() => {
  508. this.changeType(LOGIN_WX);
  509. }}
  510. >
  511. 扫码登入
  512. </GButton>
  513. </div>
  514. </div>
  515. );
  516. }
  517. }