index.js 17 KB

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