index.js 16 KB

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