index.js 18 KB

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