refactor: 存储优化
Former-commit-id: 6a051fac3c80be339c1eb01780e12334ad888551
This commit is contained in:
15
src/utils/auth.ts
Normal file
15
src/utils/auth.ts
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import Cookies from 'js-cookie';
|
||||||
|
|
||||||
|
const TokenKey = 'vue3-element-admin-token';
|
||||||
|
|
||||||
|
export function getToken() {
|
||||||
|
return Cookies.get(TokenKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function setToken(token: string) {
|
||||||
|
Cookies.set(TokenKey, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function removeToken() {
|
||||||
|
return Cookies.remove(TokenKey);
|
||||||
|
}
|
||||||
53
src/utils/localStorage.ts
Normal file
53
src/utils/localStorage.ts
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
/**
|
||||||
|
* window.localStorage 浏览器永久缓存
|
||||||
|
*/
|
||||||
|
export const localStorage = {
|
||||||
|
// 设置永久缓存
|
||||||
|
set(key: string, val: any) {
|
||||||
|
window.localStorage.setItem(key, JSON.stringify(val));
|
||||||
|
},
|
||||||
|
// 获取永久缓存
|
||||||
|
get(key: string) {
|
||||||
|
const json: any = window.localStorage.getItem(key);
|
||||||
|
return JSON.parse(json);
|
||||||
|
},
|
||||||
|
// 移除永久缓存
|
||||||
|
remove(key: string) {
|
||||||
|
window.localStorage.removeItem(key);
|
||||||
|
},
|
||||||
|
// 移除全部永久缓存
|
||||||
|
clear() {
|
||||||
|
window.localStorage.clear();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 侧边栏状态(显示/隐藏)
|
||||||
|
const SidebarStatusKey = 'sidebarStatus';
|
||||||
|
export function getSidebarStatus() {
|
||||||
|
return localStorage.get(SidebarStatusKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function setSidebarStatus(sidebarStatus: string) {
|
||||||
|
localStorage.set(SidebarStatusKey, sidebarStatus);
|
||||||
|
}
|
||||||
|
// 布局大小
|
||||||
|
const SizeKey = 'size';
|
||||||
|
|
||||||
|
export function getSize() {
|
||||||
|
return localStorage.get(SizeKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function setSize(size: string) {
|
||||||
|
localStorage.set(SizeKey, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 语言
|
||||||
|
const LanguageKey = 'language';
|
||||||
|
|
||||||
|
export function getLanguage() {
|
||||||
|
return localStorage.get(LanguageKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function setLanguage(language: string) {
|
||||||
|
localStorage.set(LanguageKey, language);
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
|
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
|
||||||
import { ElMessage, ElMessageBox } from 'element-plus';
|
import { ElMessage, ElMessageBox } from 'element-plus';
|
||||||
import { localStorage } from '@/utils/storage';
|
import { getToken } from '@/utils/auth';
|
||||||
import useStore from '@/store';
|
import { useUserStoreHook } from '@/store/modules/user';
|
||||||
|
|
||||||
// 创建 axios 实例
|
// 创建 axios 实例
|
||||||
const service = axios.create({
|
const service = axios.create({
|
||||||
@@ -18,9 +18,9 @@ service.interceptors.request.use(
|
|||||||
`Expected 'config' and 'config.headers' not to be undefined`
|
`Expected 'config' and 'config.headers' not to be undefined`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const { user } = useStore();
|
const user = useUserStoreHook();
|
||||||
if (user.token) {
|
if (user.token) {
|
||||||
config.headers.Authorization = `${localStorage.get('token')}`;
|
config.headers.Authorization = getToken();
|
||||||
}
|
}
|
||||||
return config;
|
return config;
|
||||||
},
|
},
|
||||||
|
|||||||
22
src/utils/sessionStorage.ts
Normal file
22
src/utils/sessionStorage.ts
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
/**
|
||||||
|
* window.sessionStorage 浏览器临时缓存
|
||||||
|
*/
|
||||||
|
export const sessionStorage = {
|
||||||
|
// 设置临时缓存
|
||||||
|
set(key: string, val: any) {
|
||||||
|
window.sessionStorage.setItem(key, JSON.stringify(val));
|
||||||
|
},
|
||||||
|
// 获取临时缓存
|
||||||
|
get(key: string) {
|
||||||
|
const json: any = window.sessionStorage.getItem(key);
|
||||||
|
return JSON.parse(json);
|
||||||
|
},
|
||||||
|
// 移除临时缓存
|
||||||
|
remove(key: string) {
|
||||||
|
window.sessionStorage.removeItem(key);
|
||||||
|
},
|
||||||
|
// 移除全部临时缓存
|
||||||
|
clear() {
|
||||||
|
window.sessionStorage.clear();
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user