user.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. originInviteCode(inviteCode) {
  39. this.setState({
  40. inviteCode,
  41. });
  42. }
  43. /**
  44. * 设置长难句试用
  45. */
  46. sentenceTrail() {
  47. this.setState({ sentenceTrail: true });
  48. }
  49. /**
  50. * 清除长难句试用
  51. */
  52. clearSentenceTrail() {
  53. this.setState({ sentenceTrail: null });
  54. }
  55. /**
  56. * 验证token
  57. */
  58. token() {
  59. return this.apiPost('/auth/token');
  60. }
  61. /**
  62. * 登陆
  63. * @param {*} mobile 手机号
  64. * @param {*} mobileVerifyCode 手机验证码
  65. * @param {*} inviteCode 邀请人手机/邀请码
  66. */
  67. login(mobile, mobileVerifyCode, inviteCode) {
  68. if (!inviteCode) {
  69. ({ inviteCode } = this.state);
  70. }
  71. return this.apiPost('/auth/login', { mobile, mobileVerifyCode, inviteCode });
  72. }
  73. loginWechat(code) {
  74. return this.apiGet('/auth/wechat_pc', { code }).then(() => {
  75. this.setState({ login: true });
  76. });
  77. }
  78. /**
  79. * 登出
  80. */
  81. logout(login = true) {
  82. return Promise.resolve()
  83. .then(() => {
  84. if (login) {
  85. return this.apiPost('/auth/logout', {});
  86. }
  87. return true;
  88. })
  89. .then(() => {
  90. this.setState({ login: false, info: {}, username: '' });
  91. })
  92. .then(() => {
  93. linkTo(this.project.loginPath);
  94. });
  95. }
  96. /**
  97. * 绑定手机
  98. * @param {*} mobile 手机号
  99. * @param {*} mobileVerifyCode 手机验证码
  100. * @param {*} inviteCode 邀请人手机/邀请码
  101. */
  102. bind(mobile, mobileVerifyCode, inviteCode) {
  103. return this.apiPost('/auth/bind', { mobile, mobileVerifyCode, inviteCode });
  104. }
  105. /**
  106. * 查询邀请码对应账号
  107. * @param {*} code 邀请码
  108. */
  109. validInviteCode(code) {
  110. return this.apiGet('/auth/valid/invite_code', { code });
  111. }
  112. /**
  113. * 查询手机对应账号
  114. */
  115. validMobile(mobile) {
  116. return this.apiGet('/auth/valid/mobile', { mobile });
  117. }
  118. /**
  119. * 查询手机是否绑定微信
  120. */
  121. validWechat(area, mobile) {
  122. return this.apiGet('/auth/valid/wechat', { area, mobile });
  123. }
  124. }
  125. export const User = new UserStore({ key: 'user', local: true });