user.js 2.4 KB

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