user.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. }
  19. }
  20. infoHandle(result) {
  21. if (result.token) this.setToken(result.token);
  22. this.setState({ login: result.id, needLogin: false, info: result, username: result.username });
  23. }
  24. originInviteCode(inviteCode) {
  25. this.setState({
  26. inviteCode,
  27. });
  28. }
  29. /**
  30. * 验证token
  31. */
  32. refreshToken() {
  33. return this.apiPost('/auth/token')
  34. .then(result => {
  35. this.infoHandle(result);
  36. })
  37. .catch(() => {
  38. this.logout(false);
  39. });
  40. }
  41. /**
  42. * 登陆
  43. * @param {*} area 区域码
  44. * @param {*} mobile 手机号
  45. * @param {*} mobileVerifyCode 手机验证码
  46. * @param {*} inviteCode 邀请人手机/邀请码
  47. * @param {*} email 绑定邮箱
  48. */
  49. login(area, mobile, mobileVerifyCode, inviteCode, email) {
  50. if (!inviteCode) {
  51. ({ inviteCode } = this.state);
  52. }
  53. return this.apiPost('/auth/login', { area, mobile, mobileVerifyCode, inviteCode, email }).then(result => {
  54. this.infoHandle(result);
  55. return result;
  56. });
  57. }
  58. loginWechat(code, userInfo) {
  59. return this.apiGet('/auth/wechat', { code, userInfo }).then((result) => {
  60. this.infoHandle(result);
  61. return result;
  62. });
  63. }
  64. /**
  65. * 登出
  66. */
  67. logout() {
  68. return this.apiPost('/auth/logout');
  69. }
  70. /**
  71. * 绑定手机
  72. * @param {*} area 区域码
  73. * @param {*} mobile 手机号
  74. * @param {*} mobileVerifyCode 手机验证码
  75. * @param {*} email 绑定邮箱
  76. * @param {*} inviteCode 邀请人手机/邀请码
  77. */
  78. bind(area, mobile, mobileVerifyCode, email, inviteCode) {
  79. if (!inviteCode) {
  80. ({ inviteCode } = this.state);
  81. }
  82. return this.apiPost('/auth/bind', { area, mobile, mobileVerifyCode, inviteCode, email }).then(result => {
  83. this.infoHandle(result);
  84. return result;
  85. });
  86. }
  87. /**
  88. * 查询邀请码对应账号
  89. * @param {*} code 邀请码
  90. */
  91. validInviteCode(code) {
  92. return this.apiGet('/auth/valid/invite_code', { code });
  93. }
  94. /**
  95. * 查询手机对应账号
  96. */
  97. validMobile(area, mobile) {
  98. return this.apiGet('/auth/valid/mobile', { area, mobile });
  99. }
  100. /**
  101. * 查询手机是否绑定微信
  102. */
  103. validWechat(area, mobile) {
  104. return this.apiGet('/auth/valid/wechat', { area, mobile });
  105. }
  106. }
  107. export const User = new UserStore({ key: 'user', local: true });