index.js 13 KB

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