index.js 19 KB

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