refactor: 缓存重命名Local修改为localStorage
This commit is contained in:
@@ -1,31 +1,32 @@
|
|||||||
import {AppState} from "@/store/interface";
|
import {AppState} from "@/store/interface";
|
||||||
import {Local} from "@/utils/storage";
|
import {localStorage} from "@/utils/storage";
|
||||||
import {store} from "@/store";
|
import {store} from "@/store";
|
||||||
import {defineStore} from "pinia";
|
import {defineStore} from "pinia";
|
||||||
|
import { getLanguage } from '@/lang/index'
|
||||||
|
|
||||||
export const useAppStore = defineStore({
|
export const useAppStore = defineStore({
|
||||||
id: "app",
|
id: "app",
|
||||||
state: (): AppState => ({
|
state: (): AppState => ({
|
||||||
device: 'desktop',
|
device: 'desktop',
|
||||||
sidebar: {
|
sidebar: {
|
||||||
opened: Local.get('sidebarStatus') ? !!+Local.get('sidebarStatus') : true,
|
opened: localStorage.get('sidebarStatus') ? !!+localStorage.get('sidebarStatus') : true,
|
||||||
withoutAnimation: false
|
withoutAnimation: false
|
||||||
},
|
},
|
||||||
language: 'zh',
|
language: getLanguage(),
|
||||||
size: Local.get('size')||'default'
|
size: localStorage.get('size')||'default'
|
||||||
}),
|
}),
|
||||||
actions: {
|
actions: {
|
||||||
toggleSidebar() {
|
toggleSidebar() {
|
||||||
this.sidebar.opened = !this.sidebar.opened
|
this.sidebar.opened = !this.sidebar.opened
|
||||||
this.sidebar.withoutAnimation = false
|
this.sidebar.withoutAnimation = false
|
||||||
if (this.sidebar.opened) {
|
if (this.sidebar.opened) {
|
||||||
Local.set('sidebarStatus', 1)
|
localStorage.set('sidebarStatus', 1)
|
||||||
} else {
|
} else {
|
||||||
Local.set('sidebarStatus', 0)
|
localStorage.set('sidebarStatus', 0)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
closeSideBar(withoutAnimation: any) {
|
closeSideBar(withoutAnimation: any) {
|
||||||
Local.set('sidebarStatus', 0)
|
localStorage.set('sidebarStatus', 0)
|
||||||
this.sidebar.opened = false
|
this.sidebar.opened = false
|
||||||
this.sidebar.withoutAnimation = withoutAnimation
|
this.sidebar.withoutAnimation = withoutAnimation
|
||||||
},
|
},
|
||||||
@@ -34,7 +35,11 @@ export const useAppStore = defineStore({
|
|||||||
},
|
},
|
||||||
setSize(size: string) {
|
setSize(size: string) {
|
||||||
this.size = size
|
this.size = size
|
||||||
Local.set('size', size)
|
localStorage.set('size', size)
|
||||||
|
},
|
||||||
|
setLanguage(language: string) {
|
||||||
|
this.language = language
|
||||||
|
localStorage.set('language', language)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
import { defineStore } from "pinia";
|
import { defineStore } from "pinia";
|
||||||
import { store } from "@/store";
|
import { store } from "@/store";
|
||||||
import {UserState} from "@/store/interface";
|
import {UserState} from "@/store/interface";
|
||||||
import {Local} from "@/utils/storage";
|
import {localStorage} from "@/utils/storage";
|
||||||
import {getUserInfo, login, logout} from "@/api/login";
|
import {getUserInfo, login, logout} from "@/api/login";
|
||||||
import {resetRouter} from "@/router";
|
import {resetRouter} from "@/router";
|
||||||
|
|
||||||
const getDefaultState = () => {
|
const getDefaultState = () => {
|
||||||
return {
|
return {
|
||||||
token: Local.get('token'),
|
token: localStorage.get('token'),
|
||||||
nickname: '',
|
nickname: '',
|
||||||
avatar: '',
|
avatar: '',
|
||||||
roles: [],
|
roles: [],
|
||||||
@@ -18,7 +18,7 @@ const getDefaultState = () => {
|
|||||||
export const useUserStore = defineStore({
|
export const useUserStore = defineStore({
|
||||||
id:"user",
|
id:"user",
|
||||||
state: ():UserState=>({
|
state: ():UserState=>({
|
||||||
token: Local.get('token') || '',
|
token: localStorage.get('token') || '',
|
||||||
nickname: '',
|
nickname: '',
|
||||||
avatar: '',
|
avatar: '',
|
||||||
roles: [],
|
roles: [],
|
||||||
@@ -50,7 +50,7 @@ export const useUserStore = defineStore({
|
|||||||
).then(response => {
|
).then(response => {
|
||||||
const {access_token, token_type} = response.data
|
const {access_token, token_type} = response.data
|
||||||
const accessToken = token_type + " " + access_token
|
const accessToken = token_type + " " + access_token
|
||||||
Local.set("token", accessToken)
|
localStorage.set("token", accessToken)
|
||||||
this.token = accessToken
|
this.token = accessToken
|
||||||
resolve(access_token)
|
resolve(access_token)
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
@@ -90,7 +90,7 @@ export const useUserStore = defineStore({
|
|||||||
logout() {
|
logout() {
|
||||||
return new Promise(((resolve, reject) => {
|
return new Promise(((resolve, reject) => {
|
||||||
logout().then(() => {
|
logout().then(() => {
|
||||||
Local.remove('token')
|
localStorage.remove('token')
|
||||||
this.RESET_STATE()
|
this.RESET_STATE()
|
||||||
resetRouter()
|
resetRouter()
|
||||||
resolve(null)
|
resolve(null)
|
||||||
@@ -105,7 +105,7 @@ export const useUserStore = defineStore({
|
|||||||
*/
|
*/
|
||||||
resetToken(){
|
resetToken(){
|
||||||
return new Promise(resolve=>{
|
return new Promise(resolve=>{
|
||||||
Local.remove('token')
|
localStorage.remove('token')
|
||||||
this.RESET_STATE()
|
this.RESET_STATE()
|
||||||
resolve(null)
|
resolve(null)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import {ElMessage, ElMessageBox} from "element-plus";
|
import {ElMessage, ElMessageBox} from "element-plus";
|
||||||
import {Local} from "@/utils/storage";
|
import {localStorage} from "@/utils/storage";
|
||||||
import {useUserStoreHook} from "@/store/modules/user";
|
import {useUserStoreHook} from "@/store/modules/user";
|
||||||
|
|
||||||
// 创建 axios 实例
|
// 创建 axios 实例
|
||||||
@@ -17,7 +17,7 @@ service.interceptors.request.use(
|
|||||||
throw new Error(`Expected 'config' and 'config.headers' not to be undefined`);
|
throw new Error(`Expected 'config' and 'config.headers' not to be undefined`);
|
||||||
}
|
}
|
||||||
if (useUserStoreHook().token) {
|
if (useUserStoreHook().token) {
|
||||||
config.headers.Authorization = `${Local.get('token')}`;
|
config.headers.Authorization = `${localStorage.get('token')}`;
|
||||||
}
|
}
|
||||||
return config
|
return config
|
||||||
}, (error) => {
|
}, (error) => {
|
||||||
@@ -43,7 +43,7 @@ service.interceptors.response.use(
|
|||||||
(error) => {
|
(error) => {
|
||||||
const {code, msg} = error.response.data
|
const {code, msg} = error.response.data
|
||||||
if (code === 'A0230') { // token 过期
|
if (code === 'A0230') { // token 过期
|
||||||
Local.clear(); // 清除浏览器全部缓存
|
localStorage.clear(); // 清除浏览器全部缓存
|
||||||
window.location.href = '/'; // 跳转登录页
|
window.location.href = '/'; // 跳转登录页
|
||||||
ElMessageBox.alert('当前页面已失效,请重新登录', '提示', {})
|
ElMessageBox.alert('当前页面已失效,请重新登录', '提示', {})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* window.localStorage 浏览器永久缓存
|
* window.localStorage 浏览器永久缓存
|
||||||
*/
|
*/
|
||||||
export const Local = {
|
export const localStorage = {
|
||||||
// 设置永久缓存
|
// 设置永久缓存
|
||||||
set(key: string, val: any) {
|
set(key: string, val: any) {
|
||||||
window.localStorage.setItem(key, JSON.stringify(val));
|
window.localStorage.setItem(key, JSON.stringify(val));
|
||||||
@@ -18,13 +18,13 @@ export const Local = {
|
|||||||
// 移除全部永久缓存
|
// 移除全部永久缓存
|
||||||
clear() {
|
clear() {
|
||||||
window.localStorage.clear();
|
window.localStorage.clear();
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* window.sessionStorage 浏览器临时缓存
|
* window.sessionStorage 浏览器临时缓存
|
||||||
*/
|
*/
|
||||||
export const Session = {
|
export const sessionStorage = {
|
||||||
// 设置临时缓存
|
// 设置临时缓存
|
||||||
set(key: string, val: any) {
|
set(key: string, val: any) {
|
||||||
window.sessionStorage.setItem(key, JSON.stringify(val));
|
window.sessionStorage.setItem(key, JSON.stringify(val));
|
||||||
|
|||||||
Reference in New Issue
Block a user