user.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. * @param {*} email 绑定邮箱
  39. */
  40. login(area, mobile, mobileVerifyCode, inviteCode, email) {
  41. if (!inviteCode) {
  42. ({ inviteCode } = this.state);
  43. }
  44. return this.apiPost('/auth/login', { area, mobile, mobileVerifyCode, inviteCode, email }).then(result => {
  45. this.infoHandle(result);
  46. return result;
  47. });
  48. }
  49. loginWechat(code) {
  50. return this.apiGet('/auth/wechat', { code }).then((result) => {
  51. this.infoHandle(result);
  52. return result;
  53. });
  54. }
  55. /**
  56. * 登出
  57. */
  58. logout() {
  59. return this.apiPost('/auth/logout');
  60. }
  61. /**
  62. * 绑定手机
  63. * @param {*} area 区域码
  64. * @param {*} mobile 手机号
  65. * @param {*} mobileVerifyCode 手机验证码
  66. * @param {*} inviteCode 邀请人手机/邀请码
  67. * @param {*} email 绑定邮箱
  68. */
  69. bind(area, mobile, mobileVerifyCode, inviteCode, email) {
  70. if (!inviteCode) {
  71. ({ inviteCode } = this.state);
  72. }
  73. return this.apiPost('/auth/bind', { area, mobile, mobileVerifyCode, inviteCode, email });
  74. }
  75. /**
  76. * 查询邀请码对应账号
  77. * @param {*} code 邀请码
  78. */
  79. validInviteCode(code) {
  80. return this.apiGet('/auth/valid/invite_code', { code });
  81. }
  82. /**
  83. * 查询手机对应账号
  84. */
  85. validMobile(area, mobile) {
  86. return this.apiGet('/auth/valid/mobile', { area, mobile });
  87. }
  88. /**
  89. * 查询手机是否绑定微信
  90. */
  91. validWechat(area, mobile) {
  92. return this.apiGet('/auth/valid/wechat', { area, mobile });
  93. }
  94. }
  95. export const User = new UserStore({ key: 'user', local: true });