index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 } 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_PHONE };
  21. }
  22. close() {
  23. User.closeLogin();
  24. }
  25. login() {
  26. const { data, needEmail, mobileError, validError } = this.state;
  27. const { area, mobile, mobileVerifyCode, email } = data;
  28. if (mobileError !== '' || validError !== '') return;
  29. if (area === '' || mobile === '' || mobileVerifyCode === '') return;
  30. if (needEmail && email === '') return;
  31. User.login(area, mobile, mobileVerifyCode).then(() => {
  32. let handler = null;
  33. if (needEmail) {
  34. handler = My.bindEmail(email);
  35. } else {
  36. handler = Promise.resolve();
  37. }
  38. handler.then((result) => {
  39. if (result.bindWechat) {
  40. this.close();
  41. } else {
  42. this.setState({ type: BIND_WX });
  43. }
  44. });
  45. })
  46. .catch(err => {
  47. if (err.message.indexOf('验证码') >= 0) {
  48. this.setState({ validError: err.message });
  49. } else {
  50. this.setState({ mobileError: err.message });
  51. }
  52. });
  53. }
  54. bind() {
  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.bind(area, mobile, mobileVerifyCode).then(() => {
  61. let handler = null;
  62. if (needEmail) {
  63. handler = My.bindEmail(email);
  64. } else {
  65. handler = Promise.resolve();
  66. }
  67. handler.then(() => {
  68. this.close();
  69. });
  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() {
  80. User.loginWechat('').then((result) => {
  81. if (result.id) {
  82. this.close();
  83. } else {
  84. this.setState({ type: BIND_PHONE });
  85. }
  86. });
  87. }
  88. scanBind() {
  89. User.loginWechat('').then(() => {
  90. this.close();
  91. }).catch(err => {
  92. this.setState({ type: BIND_WX_ERROR, wechatError: err.message });
  93. });
  94. }
  95. changeData(field, value) {
  96. let { data } = this.state;
  97. data = data || {};
  98. data[field] = value;
  99. this.setState({ data });
  100. }
  101. validMobile() {
  102. const { data } = this.state;
  103. const { area, mobile } = data;
  104. if (area === '' || mobile === '') return;
  105. User.validWechat(area, mobile)
  106. .then(result => {
  107. if (result) {
  108. this.setState({ mobileError: '' });
  109. return User.validMobile(area, mobile)
  110. .then(r => {
  111. this.setState({ needEmail: r });
  112. });
  113. }
  114. this.setState({ needEmail: false });
  115. return Promise.reject(new Error('该手机已绑定其他账号,请更换手机号码'));
  116. })
  117. .catch(err => {
  118. this.setState({ mobileError: err.message });
  119. });
  120. }
  121. sendValid() {
  122. const { data, mobileError } = this.state;
  123. const { area, mobile } = data;
  124. if (area === '' || mobile === '' || mobileError !== '') return Promise.reject();
  125. return Common.sendSms(area, mobile)
  126. .then((result) => {
  127. if (result) {
  128. asyncSMessage('发送成功');
  129. this.setState({ mobileError: '', validError: '' });
  130. } else {
  131. throw new Error('发送失败');
  132. }
  133. })
  134. .catch(err => {
  135. this.setState({ mobileError: err.message });
  136. throw err;
  137. });
  138. }
  139. render() {
  140. const { type } = this.state;
  141. const { user } = this.props;
  142. return (
  143. <Modal wrapClassName={`login-modal ${type}`} visible={user.needLogin} footer={null} closable={false} width={470}>
  144. {this.renderBody(type)}
  145. <GIcon name="close" onClick={() => {
  146. this.close();
  147. }} />
  148. </Modal>
  149. );
  150. }
  151. renderBody(type) {
  152. switch (type) {
  153. case LOGIN_WX:
  154. return this.renderLoginWx();
  155. case BIND_PHONE:
  156. return this.renderBindPhone();
  157. case BIND_WX:
  158. return this.renderBindWx();
  159. case BIND_WX_ERROR:
  160. return this.renderBindWxError();
  161. case LOGIN_PHONE:
  162. default:
  163. return this.renderLoginPhone();
  164. }
  165. }
  166. renderLoginPhone() {
  167. const { needEmail } = this.state;
  168. return (
  169. <div className="body">
  170. <div className="title">手机号登录</div>
  171. <div className="tip" hidden={!needEmail}>
  172. <Assets name="notice" />
  173. 该手机号尚未注册,将自动为您注册账户
  174. </div>
  175. <SelectInput placeholder="请输入手机号" selectValue={this.state.data.area} select={MobileArea} value={this.state.data.mobile} error={this.state.mobileError} onSelect={(value) => {
  176. this.changeData('area', value);
  177. }} onChange={(e) => {
  178. this.changeData('mobile', e.target.value);
  179. this.validMobile(e.target.value);
  180. }} />
  181. <VerificationInput placeholder="请输入验证码" value={this.state.data.mobileVerifyCode} error={this.state.validError} onSend={() => {
  182. return this.sendValid();
  183. }} />
  184. {needEmail && <Input placeholder="请输入邮箱" value={this.state.data.email} onChange={(e) => {
  185. this.changeData('email', e.target.value);
  186. }} />}
  187. <Button type="primary" size="large" block onClick={() => {
  188. this.login();
  189. }}>
  190. 登录
  191. </Button>
  192. <Tooltip overlayClassName="gray" placement="left" title="微信扫码登录">
  193. <a className="other" onClick={() => {
  194. this.setState({ type: LOGIN_WX });
  195. }}>
  196. <Assets name="code" />
  197. </a>
  198. </Tooltip>
  199. </div>
  200. );
  201. }
  202. renderLoginWx() {
  203. return (
  204. <div className="body">
  205. <div className="title">微信扫码登录</div>
  206. <div className="qr-code">
  207. <Assets name="qrcode" />
  208. <div className="text">请使用微信扫描二维码登录</div>
  209. </div>
  210. <Tooltip overlayClassName="gray" placement="left" title="手机号登录">
  211. <a className="other" onClick={() => {
  212. this.setState({ type: LOGIN_PHONE });
  213. }}>
  214. <Assets name="phone" />
  215. </a>
  216. </Tooltip>
  217. </div>
  218. );
  219. }
  220. renderBindPhone() {
  221. const { needEmail } = this.state;
  222. return (
  223. <div className="body">
  224. <div className="title">绑定手机号</div>
  225. <div className="tip">
  226. <Assets name="notice" />
  227. 微信登录成功!为更好的使用服务,请您绑定手机号和邮箱。
  228. </div>
  229. <SelectInput placeholder="请输入手机号" selectValue={this.state.data.area} select={MobileArea} value={this.state.data.mobile} error={this.state.mobileError} onSelect={(value) => {
  230. this.changeData('area', value);
  231. }} onChange={(e) => {
  232. this.changeData('mobile', e.target.value);
  233. this.validMobile(e.target.value);
  234. }} />
  235. <VerificationInput placeholder="请输入验证码" value={this.state.data.mobileVerifyCode} error={this.state.validError} onSend={() => {
  236. return this.sendValid();
  237. }} />
  238. {needEmail && <Input placeholder="请输入邮箱" value={this.state.data.email} onChange={(e) => {
  239. this.changeData('email', e.target.value);
  240. }} />}
  241. <Button type="primary" size="large" block onClick={() => {
  242. this.bind();
  243. }}>
  244. 绑定
  245. </Button>
  246. </div>
  247. );
  248. }
  249. renderBindWx() {
  250. return (
  251. <div className="body">
  252. <div className="title">绑定微信号</div>
  253. <div className="tip">
  254. <Assets name="notice" />
  255. 手机号注册成功!为更好的使用服务,建议您绑定微信号。
  256. </div>
  257. <div className="qr-code">
  258. <Assets name="qrcode" />
  259. <div className="text">请使用微信扫描二维码登录</div>
  260. <div className="jump" onClick={() => {
  261. this.close();
  262. }}>跳过</div>
  263. </div>
  264. </div>
  265. );
  266. }
  267. renderBindWxError() {
  268. return (
  269. <div className="body">
  270. <div className="title">绑定失败</div>
  271. <div className="text">该微信账户已绑定其他手机号,您可直接使用微信登入。</div>
  272. <div className="btn">
  273. <GButton radius onClick={() => {
  274. this.close();
  275. }}>Ok</GButton>
  276. </div>
  277. </div>
  278. );
  279. }
  280. }
  281. class Input extends Component {
  282. render() {
  283. const { className = '', onChange, placeholder, value, error, left, right } = this.props;
  284. return (
  285. <div className={`g-input-container ${className}`}>
  286. <div className={`g-input-wrapper ${error ? 'error' : ''}`}>
  287. {left && <div className="g-input-left">{left}</div>}
  288. <input className="g-input" placeholder={placeholder} value={value} onChange={data => onChange && onChange(data)} />
  289. {right && <div className="g-input-right">{right}</div>}
  290. </div>
  291. <div hidden={!error} className="g-input-error">
  292. {error}
  293. </div>
  294. </div>
  295. );
  296. }
  297. }
  298. class SelectInput extends Component {
  299. constructor(props) {
  300. super(props);
  301. this.state = { showSelect: false };
  302. }
  303. render() {
  304. const { showSelect } = this.state;
  305. const { className = '', onChange, placeholder, value, error, selectValue, select, onSelect } = this.props;
  306. return (
  307. <Input
  308. className={className}
  309. left={
  310. <span className="g-input-left-select" onClick={() => this.setState({ showSelect: !showSelect })}>
  311. {selectValue}
  312. <Icon type={showSelect ? 'up' : 'down'} />
  313. {showSelect && <ul className="select-list">{select.map((row) => {
  314. return <li onClick={() => {
  315. this.setState({ showSelect: false });
  316. if (onSelect) onSelect(row.value);
  317. }}>{row.label}</li>;
  318. })}</ul>}
  319. </span>
  320. }
  321. value={value}
  322. placeholder={placeholder}
  323. onChange={data => onChange && onChange(data)}
  324. error={error}
  325. />
  326. );
  327. }
  328. }
  329. export class VerificationInput extends Component {
  330. constructor(props) {
  331. super(props);
  332. this.timeKey = null;
  333. this.state = { loading: 0 };
  334. }
  335. componentWillUnmount() {
  336. if (this.timeKey) clearTimeout(this.timeKey);
  337. }
  338. onSend() {
  339. const { onSend, time = 60 } = this.props;
  340. if (onSend) {
  341. onSend()
  342. .then(() => {
  343. this.setTime(time);
  344. });
  345. }
  346. }
  347. setTime(time) {
  348. this.setState({ loading: time });
  349. this.timeKey = setTimeout(() => {
  350. this.setTime(time - 1);
  351. }, 1000);
  352. }
  353. render() {
  354. const { loading } = this.state;
  355. const { className = '', onChange, placeholder, value } = this.props;
  356. return (
  357. <Input
  358. className={className}
  359. right={
  360. loading <= 0 ? (
  361. <span className="g-input-right-verification" onClick={() => this.onSend()}>
  362. 获取验证码
  363. </span>
  364. ) : (
  365. <span className="g-input-right-verification-loading">等待{loading}秒</span>
  366. )
  367. }
  368. value={value}
  369. placeholder={placeholder}
  370. onChange={data => onChange && onChange(data)}
  371. />
  372. );
  373. }
  374. }