index.js 15 KB

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