user.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import BaseStore from '@src/stores/base';
  2. import * as querystring from 'querystring';
  3. export default class UserStore extends BaseStore {
  4. constructor(props) {
  5. super(props);
  6. this.adminLogin = null;
  7. const { token } = querystring.parse(window.location.search.replace('?', ''));
  8. if (token) {
  9. this.adminLogin = token;
  10. }
  11. }
  12. initState() {
  13. if (this.adminLogin) this.setToken(this.adminLogin);
  14. return { login: !!this.adminLogin };
  15. }
  16. initAfter() {
  17. if (this.state.login || this.adminLogin) {
  18. this.refreshToken().then(() => {
  19. if (this.adminLogin) {
  20. this.linkTo(window.location.href.substr(0, window.location.href.indexOf('?') + 1));
  21. }
  22. });
  23. }
  24. }
  25. refreshToken() {
  26. return this.apiPost('/auth/token')
  27. .then(result => {
  28. this.infoHandle(result);
  29. })
  30. .catch(() => {
  31. this.logout(false);
  32. });
  33. }
  34. infoHandle(result) {
  35. this.setToken(result.token);
  36. this.setState({ login: true, info: result, username: result.username });
  37. }
  38. /**
  39. * 设置长难句试用
  40. */
  41. sentenceTrail() {
  42. this.setState({ sentenceTrail: true });
  43. }
  44. /**
  45. * 清除长难句试用
  46. */
  47. clearSentenceTrail() {
  48. this.setState({ sentenceTrail: null });
  49. }
  50. /**
  51. * 验证token
  52. */
  53. token() {
  54. return this.apiPost('/auth/token');
  55. }
  56. /**
  57. * 登陆
  58. * @param {*} mobile 手机号
  59. * @param {*} mobileVerifyCode 手机验证码
  60. * @param {*} inviteCode 邀请人手机/邀请码
  61. */
  62. login(mobile, mobileVerifyCode, inviteCode) {
  63. return this.apiPost('/auth/login', { mobile, mobileVerifyCode, inviteCode });
  64. }
  65. loginWechat(code) {
  66. return this.apiGet('/auth/wechat_pc', { code }).then(() => {
  67. this.setState({ login: true });
  68. });
  69. }
  70. /**
  71. * 登出
  72. */
  73. logout(login = true) {
  74. return Promise.resolve()
  75. .then(() => {
  76. if (login) {
  77. return this.apiPost('/auth/logout', {});
  78. }
  79. return true;
  80. })
  81. .then(() => {
  82. this.setState({ login: false, info: {}, username: '' });
  83. })
  84. .then(() => {
  85. linkTo(this.project.loginPath);
  86. });
  87. }
  88. /**
  89. * 绑定手机
  90. * @param {*} mobile 手机号
  91. * @param {*} mobileVerifyCode 手机验证码
  92. * @param {*} inviteCode 邀请人手机/邀请码
  93. */
  94. bind(mobile, mobileVerifyCode, inviteCode) {
  95. return this.apiPost('/auth/bind', { mobile, mobileVerifyCode, inviteCode });
  96. }
  97. /**
  98. * 查询邀请码对应账号
  99. * @param {*} code 邀请码
  100. */
  101. validInviteCode(code) {
  102. return this.apiGet('/auth/valid/invite_code', { code });
  103. }
  104. /**
  105. * 查询手机对应账号
  106. */
  107. validMobile(mobile) {
  108. return this.apiGet('/auth/valid/mobile', { mobile });
  109. }
  110. }
  111. export const User = new UserStore({ key: 'user', local: true });