user.js 3.6 KB

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