index.js 14 KB

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