user.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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, userInfo) {
  50. return this.apiGet('/auth/wechat', { code, userInfo }).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 {*} email 绑定邮箱
  67. * @param {*} inviteCode 邀请人手机/邀请码
  68. */
  69. bind(area, mobile, mobileVerifyCode, email, inviteCode) {
  70. if (!inviteCode) {
  71. ({ inviteCode } = this.state);
  72. }
  73. return this.apiPost('/auth/bind', { area, mobile, mobileVerifyCode, inviteCode, email }).then(result => {
  74. this.infoHandle(result);
  75. return result;
  76. });
  77. }
  78. /**
  79. * 查询邀请码对应账号
  80. * @param {*} code 邀请码
  81. */
  82. validInviteCode(code) {
  83. return this.apiGet('/auth/valid/invite_code', { code });
  84. }
  85. /**
  86. * 查询手机对应账号
  87. */
  88. validMobile(area, mobile) {
  89. return this.apiGet('/auth/valid/mobile', { area, mobile });
  90. }
  91. /**
  92. * 查询手机是否绑定微信
  93. */
  94. validWechat(area, mobile) {
  95. return this.apiGet('/auth/valid/wechat', { area, mobile });
  96. }
  97. }
  98. export const User = new UserStore({ key: 'user', local: true });