user.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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, 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. if (result.token) 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. * @param {*} email 绑定邮箱
  82. */
  83. login(area, mobile, mobileVerifyCode, inviteCode, email) {
  84. if (!inviteCode) {
  85. ({ inviteCode } = this.state);
  86. }
  87. return this.apiPost('/auth/login', { area, mobile, mobileVerifyCode, inviteCode, email }).then(result => {
  88. this.infoHandle(result);
  89. return result;
  90. });
  91. }
  92. loginWechat(code) {
  93. return this.apiGet('/auth/wechat_pc', { code }).then(result => {
  94. this.infoHandle(result);
  95. return result;
  96. });
  97. }
  98. /**
  99. * 登出
  100. */
  101. logout(login = true) {
  102. return Promise.resolve()
  103. .then(() => {
  104. if (login) {
  105. return this.apiPost('/auth/logout', {});
  106. }
  107. return true;
  108. })
  109. .then(() => {
  110. this.setState({ login: false, info: {}, username: '' });
  111. })
  112. .then(() => {
  113. linkTo(this.project.loginPath);
  114. });
  115. }
  116. /**
  117. * 绑定手机
  118. * @param {*} mobile 手机号
  119. * @param {*} mobileVerifyCode 手机验证码
  120. * @param {*} inviteCode 邀请人手机/邀请码
  121. * @param {*} email 绑定邮箱
  122. */
  123. bind(area, mobile, mobileVerifyCode, inviteCode, email) {
  124. return this.apiPost('/auth/bind', { area, mobile, mobileVerifyCode, inviteCode, email });
  125. }
  126. /**
  127. * 查询邀请码对应账号
  128. * @param {*} code 邀请码
  129. */
  130. validInviteCode(code) {
  131. return this.apiGet('/auth/valid/invite_code', { code });
  132. }
  133. /**
  134. * 查询手机对应账号
  135. */
  136. validMobile(area, mobile) {
  137. return this.apiGet('/auth/valid/mobile', { area, mobile });
  138. }
  139. /**
  140. * 查询手机是否绑定微信
  141. */
  142. validWechat(area, mobile) {
  143. return this.apiGet('/auth/valid/wechat', { area, mobile });
  144. }
  145. }
  146. export const User = new UserStore({ key: 'user', local: true });