mirror of
https://github.com/pure-admin/vue-pure-admin.git
synced 2025-06-09 01:47:20 +08:00
封装存储方法
This commit is contained in:
parent
10ba75b1b6
commit
afbbaebfc0
@ -9,7 +9,7 @@ import ElementPlus from 'element-plus'
|
|||||||
import 'element-plus/lib/theme-chalk/index.css'
|
import 'element-plus/lib/theme-chalk/index.css'
|
||||||
|
|
||||||
// 导入公共样式
|
// 导入公共样式
|
||||||
import "./assets/index.css"
|
import './assets/index.css'
|
||||||
|
|
||||||
const app = createApp(App)
|
const app = createApp(App)
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
|
import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router"
|
||||||
import Home from '../views/home.vue'
|
import Home from "../views/home.vue"
|
||||||
import { setToken, getToken } from "../utils/token"
|
import { storageSession } from "../utils/storage"
|
||||||
|
|
||||||
const routes: Array<RouteRecordRaw> = [
|
const routes: Array<RouteRecordRaw> = [
|
||||||
{
|
{
|
||||||
@ -34,8 +34,7 @@ const router = createRouter({
|
|||||||
const whiteList = ["/login", "/register"]
|
const whiteList = ["/login", "/register"]
|
||||||
|
|
||||||
router.beforeEach((to, _from, next) => {
|
router.beforeEach((to, _from, next) => {
|
||||||
const token = getToken()
|
whiteList.indexOf(to.path) !== -1 || storageSession.getItem("Token") ? next() : next("/login") // 全部重定向到登录页
|
||||||
whiteList.indexOf(to.path) !== -1 || token ? next() : next("/login") // 全部重定向到登录页
|
|
||||||
})
|
})
|
||||||
|
|
||||||
export default router
|
export default router
|
||||||
|
42
src/utils/storage/index.ts
Normal file
42
src/utils/storage/index.ts
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
interface ProxyStorage {
|
||||||
|
getItem(key: string): any
|
||||||
|
setItem(Key: string, value: string): void
|
||||||
|
removeItem(key: string): void
|
||||||
|
}
|
||||||
|
|
||||||
|
//sessionStorage operate
|
||||||
|
class sessionStorageProxy implements ProxyStorage {
|
||||||
|
|
||||||
|
protected storage: ProxyStorage
|
||||||
|
|
||||||
|
constructor(storageModel: ProxyStorage) {
|
||||||
|
this.storage = storageModel
|
||||||
|
}
|
||||||
|
|
||||||
|
// 存
|
||||||
|
public setItem(key: string, value: string): void {
|
||||||
|
this.storage.setItem(key, JSON.stringify(value))
|
||||||
|
}
|
||||||
|
|
||||||
|
// 取
|
||||||
|
public getItem(key: string): any {
|
||||||
|
return JSON.parse(this.storage.getItem(key)) || null
|
||||||
|
}
|
||||||
|
|
||||||
|
// 删
|
||||||
|
public removeItem(key: string): void {
|
||||||
|
this.storage.removeItem(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//localStorage operate
|
||||||
|
class localStorageProxy extends sessionStorageProxy implements ProxyStorage {
|
||||||
|
constructor(localStorage: ProxyStorage) {
|
||||||
|
super(localStorage)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const storageSession = new sessionStorageProxy(sessionStorage)
|
||||||
|
|
||||||
|
export const storageLocal = new localStorageProxy(localStorage)
|
@ -1,19 +0,0 @@
|
|||||||
/**
|
|
||||||
* 处理token的工具
|
|
||||||
*/
|
|
||||||
|
|
||||||
// 存
|
|
||||||
export function setToken(token: string): void {
|
|
||||||
token && sessionStorage.setItem("Token", token)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 取
|
|
||||||
export function getToken(): string {
|
|
||||||
let token = sessionStorage.getItem("Token") && JSON.parse(sessionStorage.getItem("Token") || "")
|
|
||||||
return token
|
|
||||||
}
|
|
||||||
|
|
||||||
// 删
|
|
||||||
export function delToken(): void {
|
|
||||||
sessionStorage.removeItem("Token")
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user