auth.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import UserApi, { LoginResult, UserInfo } from "@/api/auth/UserApi";
  2. import { defineStore } from "pinia"
  3. const STORAGE_KEY = 'authInfo';
  4. export const useAuthStore = defineStore('auth', {
  5. state: () => ({
  6. token: '',
  7. expireAt: 0,
  8. userId: 0,
  9. userInfo: null as null|UserInfo,
  10. loginType: 0,
  11. }),
  12. actions: {
  13. async loadLoginState() {
  14. try {
  15. const res = await uni.getStorage({ key: STORAGE_KEY });
  16. if (!res.data)
  17. return false;
  18. const authInfo = JSON.parse(res.data);
  19. this.token = authInfo.token;
  20. this.userId = authInfo.userId;
  21. this.expireAt = authInfo.expireAt;
  22. this.userInfo = authInfo.userInfo;
  23. this.loginType = authInfo.loginType;
  24. setToken(this.token);
  25. return true;
  26. } catch (error) {
  27. this.token = '';
  28. this.userId = 0;
  29. this.userInfo = null;
  30. setToken('');
  31. console.log('loadLoginState', error);
  32. return false;
  33. }
  34. },
  35. async loginWechart(code: string, res: UniApp.GetUserProfileRes) {
  36. const loginResult = await UserApi.loginThird({
  37. code,
  38. encrypted_data: res.encryptedData,
  39. iv: res.iv,
  40. platform: 'wechat',
  41. raw_data: JSON.parse(res.rawData),
  42. signature: res.signature,
  43. })
  44. this.loginResultHandle(loginResult, 0);
  45. },
  46. async loginMobile(account: string, password: string, loginType: number) {
  47. let loginResult;
  48. if (loginType == 0) {
  49. loginResult = await UserApi.login({
  50. account,
  51. password,
  52. })
  53. } else if (loginType == 1) {
  54. loginResult = await UserApi.loginAdmin({
  55. account,
  56. password,
  57. })
  58. } else
  59. throw 'login type error';
  60. this.loginResultHandle(loginResult, loginType);
  61. },
  62. async loginResultHandle(loginResult: LoginResult, loginType: number) {
  63. this.token = loginResult.auth.token;
  64. this.userId = loginResult.mainBodyUserInfo.id;
  65. this.userInfo = loginResult.mainBodyUserInfo;
  66. this.expireAt = loginResult.auth.expiresIn + Date.now();
  67. this.loginType = loginType;
  68. console.log('loginResultHandle');
  69. setToken(this.token);
  70. uni.setStorage({
  71. key: STORAGE_KEY,
  72. data: JSON.stringify({
  73. token: this.token,
  74. userId: this.userId ,
  75. expireAt: this.expireAt,
  76. userInfo: this.userInfo,
  77. loginType,
  78. })
  79. });
  80. },
  81. async logout() {
  82. this.token = '';
  83. this.userId = 0;
  84. this.userInfo = null;
  85. setToken('');
  86. uni.removeStorage({ key: STORAGE_KEY });
  87. },
  88. getInternalAuth() {
  89. return {
  90. token: this.token,
  91. userId: this.userId ,
  92. expireAt: this.expireAt,
  93. userInfo: this.userInfo,
  94. loginType: this.loginType,
  95. }
  96. },
  97. },
  98. getters: {
  99. isAdmin(state) {
  100. return state.loginType === 1;
  101. },
  102. isLogged(state) {
  103. return state.token != '' && state.userId != 0
  104. },
  105. },
  106. })
  107. function setToken(token: string) {
  108. const app = getApp();
  109. if (!!app.globalData)
  110. app.globalData = {};
  111. return app.globalData!.token = token;
  112. }