index.js 18 KB

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