index.js 19 KB

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