auth.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.userInfo = authInfo.userInfo;
  22. this.loginType = authInfo.loginType;
  23. setToken(this.token);
  24. return true;
  25. } catch (error) {
  26. this.token = '';
  27. this.userId = 0;
  28. this.userInfo = null;
  29. setToken('');
  30. console.log('loadLoginState', error);
  31. return false;
  32. }
  33. },
  34. async loginWechart(code: string, res: UniApp.GetUserProfileRes) {
  35. const loginResult = await UserApi.loginThird({
  36. code,
  37. encrypted_data: res.encryptedData,
  38. iv: res.iv,
  39. platform: 'wechat',
  40. raw_data: JSON.parse(res.rawData),
  41. signature: res.signature,
  42. })
  43. this.loginResultHandle(loginResult, 0);
  44. },
  45. async loginMobile(account: string, password: string, loginType: number) {
  46. let loginResult;
  47. if (loginType == 0) {
  48. loginResult = await UserApi.login({
  49. account,
  50. password,
  51. })
  52. } else if (loginType == 1) {
  53. loginResult = await UserApi.loginAdmin({
  54. account,
  55. password,
  56. })
  57. } else
  58. throw 'login type error';
  59. this.loginResultHandle(loginResult, loginType);
  60. },
  61. async loginResultHandle(loginResult: LoginResult, loginType: number) {
  62. this.token = loginResult.token;
  63. this.userId = loginResult.userInfo.id;
  64. this.userInfo = loginResult.userInfo;
  65. this.loginType = loginType;
  66. setToken(this.token);
  67. this.saveLoginState();
  68. },
  69. async logout() {
  70. this.token = '';
  71. this.userId = 0;
  72. this.userInfo = null;
  73. setToken('');
  74. uni.removeStorage({ key: STORAGE_KEY });
  75. },
  76. saveLoginState() {
  77. uni.setStorage({
  78. key: STORAGE_KEY,
  79. data: JSON.stringify({
  80. token: this.token,
  81. userId: this.userId ,
  82. expireAt: this.expireAt,
  83. userInfo: this.userInfo,
  84. loginType: this.loginType,
  85. })
  86. });
  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. }