index.js 15 KB

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