user.js 3.5 KB

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