mirror of
https://github.com/m-xlsea/ruoyi-plus-soybean.git
synced 2025-09-24 07:49:47 +08:00
refactor(projects): new storage system [新的本地数据存储系统]
This commit is contained in:
25
src/store/modules/auth/helpers.ts
Normal file
25
src/store/modules/auth/helpers.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import { localStg } from '@/utils';
|
||||
|
||||
/** 获取token */
|
||||
export function getToken() {
|
||||
return localStg.get('token') || '';
|
||||
}
|
||||
|
||||
/** 获取用户信息 */
|
||||
export function getUserInfo() {
|
||||
const emptyInfo: Auth.UserInfo = {
|
||||
userId: '',
|
||||
userName: '',
|
||||
userRole: 'user'
|
||||
};
|
||||
const userInfo: Auth.UserInfo = localStg.get('userInfo') || emptyInfo;
|
||||
|
||||
return userInfo;
|
||||
}
|
||||
|
||||
/** 去除用户相关缓存 */
|
||||
export function clearAuthStorage() {
|
||||
localStg.remove('token');
|
||||
localStg.remove('refreshToken');
|
||||
localStg.remove('userInfo');
|
||||
}
|
@ -3,9 +3,10 @@ import { defineStore } from 'pinia';
|
||||
import { router } from '@/router';
|
||||
import { fetchLogin, fetchUserInfo } from '@/service';
|
||||
import { useRouterPush } from '@/composables';
|
||||
import { clearAuthStorage, getToken, getUserInfo, setRefreshToken, setToken, setUserInfo } from '@/utils';
|
||||
import { localStg } from '@/utils';
|
||||
import { useTabStore } from '../tab';
|
||||
import { useRouteStore } from '../route';
|
||||
import { getToken, getUserInfo, clearAuthStorage } from './helpers';
|
||||
|
||||
interface AuthState {
|
||||
/** 用户信息 */
|
||||
@ -81,14 +82,14 @@ export const useAuthStore = defineStore('auth-store', {
|
||||
|
||||
// 先把token存储到缓存中(后面接口的请求头需要token)
|
||||
const { token, refreshToken } = backendToken;
|
||||
setToken(token);
|
||||
setRefreshToken(refreshToken);
|
||||
localStg.set('token', token);
|
||||
localStg.set('refreshToken', refreshToken);
|
||||
|
||||
// 获取用户信息
|
||||
const { data } = await fetchUserInfo();
|
||||
if (data) {
|
||||
// 成功后把用户信息存储到缓存中
|
||||
setUserInfo(data);
|
||||
localStg.set('userInfo', data);
|
||||
|
||||
// 更新状态
|
||||
this.userInfo = data;
|
||||
|
Reference in New Issue
Block a user