user.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import BaseStore from '@src/stores/base';
  2. import * as querystring from 'querystring';
  3. const SENTENCE_TRAIL = 'SENTENCE_TRAIL';
  4. export default class UserStore extends BaseStore {
  5. initState() {
  6. const { token } = querystring.parse(window.location.search.replace('?', ''));
  7. if (token) {
  8. this.setToken(token);
  9. }
  10. const sentenceTrail = this.getLocal(SENTENCE_TRAIL);
  11. return { login: !!token, sentenceTrail };
  12. }
  13. /**
  14. * 设置长难句试用
  15. */
  16. sentenceTrail() {
  17. this.saveLocal(SENTENCE_TRAIL, true);
  18. this.setState({ sentenceTrail: true });
  19. }
  20. /**
  21. * 清除长难句试用
  22. */
  23. clearSentenceTrail() {
  24. this.removeLocal(SENTENCE_TRAIL);
  25. this.setState({ sentenceTrail: null });
  26. }
  27. /**
  28. * 验证token
  29. */
  30. token() {
  31. return this.apiPost('/auth/token');
  32. }
  33. /**
  34. * 登陆
  35. * @param {*} mobile 手机号
  36. * @param {*} mobileVerifyCode 手机验证码
  37. * @param {*} inviteCode 邀请人手机/邀请码
  38. */
  39. login(mobile, mobileVerifyCode, inviteCode) {
  40. return this.apiPost('/auth/login', { mobile, mobileVerifyCode, inviteCode });
  41. }
  42. loginWechat(code) {
  43. return this.apiGet('/auth/wechat_pc', { code }).then(() => {
  44. this.setState({ login: true });
  45. });
  46. }
  47. /**
  48. * 登出
  49. */
  50. logout() {
  51. return this.apiPost('/auth/logout');
  52. }
  53. /**
  54. * 绑定手机
  55. * @param {*} mobile 手机号
  56. * @param {*} mobileVerifyCode 手机验证码
  57. * @param {*} inviteCode 邀请人手机/邀请码
  58. */
  59. bind(mobile, mobileVerifyCode, inviteCode) {
  60. return this.apiPost('/auth/bind', { mobile, mobileVerifyCode, inviteCode });
  61. }
  62. /**
  63. * 查询邀请码对应账号
  64. * @param {*} code 邀请码
  65. */
  66. validInviteCode(code) {
  67. return this.apiGet('/auth/valid/invite_code', { code });
  68. }
  69. /**
  70. * 查询手机对应账号
  71. */
  72. validMobile(mobile) {
  73. return this.apiGet('/auth/valid/mobile', { mobile });
  74. }
  75. }
  76. export const User = new UserStore({ key: 'user', local: true });