user.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import BaseStore from '@src/stores/base';
  2. // import * as querystring from 'querystring';
  3. export default class UserStore extends BaseStore {
  4. initState() {
  5. // const { code } = querystring.parse(window.location.search.replace('?', ''));
  6. // if (code) {
  7. // this.loginWechat(code);
  8. // }
  9. return { login: false };
  10. }
  11. initAfter() {
  12. if (this.state.login) {
  13. this.refreshToken().then(() => {
  14. if (this.adminLogin) {
  15. window.location.href = window.location.href.replace(`token=${this.adminLogin}`, '').replace('&&', '&');
  16. }
  17. })
  18. .catch(() => {
  19. this.setState({ login: false });
  20. });
  21. }
  22. }
  23. infoHandle(result) {
  24. if (result.token) this.setToken(result.token);
  25. this.setState({ login: result.id, needLogin: false, info: result, username: result.username });
  26. }
  27. originInviteCode(inviteCode) {
  28. this.setState({
  29. inviteCode,
  30. });
  31. }
  32. /**
  33. * 验证token
  34. */
  35. refreshToken() {
  36. return this.apiPost('/auth/token')
  37. .then(result => {
  38. this.infoHandle(result);
  39. })
  40. .catch(() => {
  41. this.logout(false);
  42. });
  43. }
  44. /**
  45. * 登陆
  46. * @param {*} area 区域码
  47. * @param {*} mobile 手机号
  48. * @param {*} mobileVerifyCode 手机验证码
  49. * @param {*} inviteCode 邀请人手机/邀请码
  50. * @param {*} email 绑定邮箱
  51. */
  52. login(area, mobile, mobileVerifyCode, inviteCode, email) {
  53. if (!inviteCode) {
  54. ({ inviteCode } = this.state);
  55. }
  56. return this.apiPost('/auth/login', { area, mobile, mobileVerifyCode, inviteCode, email }).then(result => {
  57. this.infoHandle(result);
  58. return result;
  59. });
  60. }
  61. loginWechat(code, userInfo) {
  62. return this.apiGet('/auth/wechat', { code, userInfo }).then((result) => {
  63. this.infoHandle(result);
  64. return result;
  65. });
  66. }
  67. /**
  68. * 登出
  69. */
  70. logout(login = true) {
  71. return Promise.resolve()
  72. .then(() => {
  73. if (login) {
  74. return this.apiPost('/auth/logout', {});
  75. }
  76. return true;
  77. })
  78. .then(() => {
  79. this.setState({ login: false, info: {}, username: '' });
  80. })
  81. .then(() => {
  82. linkTo(this.project.loginPath);
  83. });
  84. }
  85. /**
  86. * 绑定手机
  87. * @param {*} area 区域码
  88. * @param {*} mobile 手机号
  89. * @param {*} mobileVerifyCode 手机验证码
  90. * @param {*} email 绑定邮箱
  91. * @param {*} inviteCode 邀请人手机/邀请码
  92. */
  93. bind(area, mobile, mobileVerifyCode, email, inviteCode) {
  94. if (!inviteCode) {
  95. ({ inviteCode } = this.state);
  96. }
  97. return this.apiPost('/auth/bind', { area, mobile, mobileVerifyCode, inviteCode, email }).then(result => {
  98. this.infoHandle(result);
  99. return result;
  100. });
  101. }
  102. /**
  103. * 查询邀请码对应账号
  104. * @param {*} code 邀请码
  105. */
  106. validInviteCode(code) {
  107. return this.apiGet('/auth/valid/invite_code', { code });
  108. }
  109. /**
  110. * 查询手机对应账号
  111. */
  112. validMobile(area, mobile) {
  113. return this.apiGet('/auth/valid/mobile', { area, mobile });
  114. }
  115. /**
  116. * 查询邮箱对应账号
  117. */
  118. validEmail(email) {
  119. return this.apiGet('/auth/valid/email', { email });
  120. }
  121. /**
  122. * 查询手机是否绑定微信
  123. */
  124. validWechat(area, mobile) {
  125. return this.apiGet('/auth/valid/wechat', { area, mobile });
  126. }
  127. }
  128. export const User = new UserStore({ key: 'user', local: true });