auth.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import CollectUserApi from "@/api/auth/CollectUserApi";
  2. import UserApi, { LoginResult, UserInfo } from "@/api/auth/UserApi";
  3. import { defineStore } from "pinia"
  4. const STORAGE_KEY = 'authInfo';
  5. export const LOGIN_TYPE_USER = -1;
  6. export const LOGIN_TYPE_COLLECT = 0;
  7. export const LOGIN_TYPE_COLLECT_ADMIN = 1;
  8. export const useAuthStore = defineStore('auth', {
  9. state: () => ({
  10. token: '',
  11. expireAt: 0,
  12. userId: 0,
  13. userInfo: null as null|UserInfo,
  14. loginType: -1,
  15. }),
  16. actions: {
  17. async loadLoginState() {
  18. try {
  19. const res = await uni.getStorage({ key: STORAGE_KEY });
  20. if (!res.data)
  21. throw 'no storage';
  22. const authInfo = JSON.parse(res.data);
  23. this.token = authInfo.token;
  24. this.userId = authInfo.userId;
  25. this.expireAt = authInfo.expireAt;
  26. this.userInfo = authInfo.userInfo;
  27. this.loginType = authInfo.loginType ?? -1;
  28. this.shareLoginInfo();
  29. //检查登录是否过期
  30. await UserApi.checkUserAuthed(this.userId);
  31. return true;
  32. } catch (error) {
  33. this.token = '';
  34. this.userId = 0;
  35. this.userInfo = null;
  36. console.log('loadLoginState', error);
  37. }
  38. },
  39. async loginWechart(code: string, res: UniApp.GetUserProfileRes) {
  40. const loginResult = await UserApi.loginThird({
  41. code,
  42. encrypted_data: res.encryptedData,
  43. iv: res.iv,
  44. platform: 'wechat',
  45. raw_data: JSON.parse(res.rawData),
  46. signature: res.signature,
  47. })
  48. console.log(loginResult);
  49. if (loginResult.inheritor) {
  50. this.loginType = LOGIN_TYPE_COLLECT;
  51. } else if (loginResult.aub) {
  52. this.loginType = LOGIN_TYPE_COLLECT_ADMIN;
  53. } else {
  54. this.loginType = LOGIN_TYPE_USER;
  55. }
  56. this.loginResultHandle(loginResult, this.loginType);
  57. },
  58. async loginMobile(mobile: string, password: string) {
  59. const loginResult = await UserApi.loginWithMobile({
  60. mobile,
  61. password,
  62. })
  63. this.loginResultHandle(loginResult);
  64. },
  65. async loginCollect(mobile: string, password: string, loginType: number) {
  66. let loginResult;
  67. if (loginType == LOGIN_TYPE_COLLECT) {
  68. loginResult = await CollectUserApi.login({
  69. mobile,
  70. password,
  71. })
  72. } else if (loginType == LOGIN_TYPE_COLLECT_ADMIN) {
  73. loginResult = await CollectUserApi.loginAdmin({
  74. account: mobile,
  75. password,
  76. })
  77. } else
  78. throw 'login type error';
  79. this.loginResultHandle(loginResult, loginType);
  80. },
  81. async loginResultHandle(loginResult: LoginResult, loginType: number = -1) {
  82. this.token = loginResult.auth.token;
  83. this.userId = loginResult.mainBodyUserInfo.id;
  84. this.userInfo = loginResult.mainBodyUserInfo;
  85. this.loginType = loginType;
  86. this.expireAt = loginResult.auth.expiresIn + Date.now();
  87. this.saveLoginState();
  88. },
  89. saveLoginState() {
  90. this.shareLoginInfo();
  91. uni.setStorage({
  92. key: STORAGE_KEY,
  93. data: JSON.stringify({
  94. token: this.token,
  95. userId: this.userId ,
  96. expireAt: this.expireAt,
  97. userInfo: this.userInfo,
  98. loginType: this.loginType,
  99. })
  100. });
  101. },
  102. shareLoginInfo() {
  103. // 全局变量存储
  104. const app = getApp();
  105. if (!app.globalData)
  106. app.globalData = {};
  107. app.globalData.token = this.token;
  108. app.globalData.userId = this.userId;
  109. app.globalData.userInfo = this.userInfo;
  110. },
  111. async logout() {
  112. this.token = '';
  113. this.userId = 0;
  114. this.userInfo = null;
  115. uni.removeStorage({ key: STORAGE_KEY });
  116. }
  117. },
  118. getters: {
  119. isLogged(state) {
  120. return state.token != '' && state.userId != 0
  121. },
  122. isCollect(state) {
  123. return state.loginType === LOGIN_TYPE_COLLECT;
  124. },
  125. isCollectAdmin(state) {
  126. return state.loginType === LOGIN_TYPE_COLLECT_ADMIN;
  127. },
  128. isCollectReviewer(state) {
  129. return state.userInfo?.isReviewer ?? false;
  130. },
  131. },
  132. })