From 57bc68e7b045f9ae7856c775d934506d0ec861e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E4=BC=A0=E9=BE=99?= Date: Fri, 1 Jul 2022 23:27:05 +0800 Subject: [PATCH] refactor: simplify wrapper storage --- pnpm-lock.yaml | 14 ++++---------- src/store/modules/tags/helpers.js | 7 +++---- src/store/modules/tags/index.js | 7 ++++--- src/utils/cache/index.js | 12 +++++++++--- src/utils/cache/{web-storage.js => storage.js} | 6 +++--- src/utils/token.js | 12 +++++------- src/views/login/index.vue | 13 ++++++------- 7 files changed, 34 insertions(+), 37 deletions(-) rename src/utils/cache/{web-storage.js => storage.js} (87%) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 445c095..a108de2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -30,7 +30,7 @@ specifiers: vite: ^2.9.9 vite-plugin-html: ^3.2.0 vite-plugin-mock: ^2.9.6 - vite-plugin-vue-setup-extend: ^0.3.0 + vite-plugin-vue-setup-extend-plus: ^0.1.0 vue: ^3.2.31 vue-router: ^4.0.15 @@ -68,7 +68,7 @@ devDependencies: vite: 2.9.9_sass@1.49.10 vite-plugin-html: 3.2.0_vite@2.9.9 vite-plugin-mock: 2.9.6_mockjs@1.1.0+vite@2.9.9 - vite-plugin-vue-setup-extend: 0.3.0_vite@2.9.9 + vite-plugin-vue-setup-extend-plus: 0.1.0 packages: @@ -2788,14 +2788,8 @@ packages: - supports-color dev: true - /vite-plugin-vue-setup-extend/0.3.0_vite@2.9.9: - resolution: {integrity: sha512-9Nd7Bj4TftB2CoOAD2ZI4cHLW5zjKMF3LNihWbrnAPx3nuGBn33tM9SVUGBVjBB6uv1mGAPavwKCTU0xAD8qhw==} - peerDependencies: - vite: '>=2.0.0' - dependencies: - '@vue/compiler-sfc': 3.2.31 - magic-string: 0.25.9 - vite: 2.9.9_sass@1.49.10 + /vite-plugin-vue-setup-extend-plus/0.1.0: + resolution: {integrity: sha512-pa27KIsHIBvBMv4xz9uB3UCfAuP2tr7PLlFhCS9vw+aXd326LEHsvhqd3hCQDOR5MjlQVyQH6vwuGr3u+KRiiw==} dev: true /vite/2.9.9_sass@1.49.10: diff --git a/src/store/modules/tags/helpers.js b/src/store/modules/tags/helpers.js index d10e33c..9316061 100644 --- a/src/store/modules/tags/helpers.js +++ b/src/store/modules/tags/helpers.js @@ -1,7 +1,6 @@ -import { createSessionStorage } from '@/utils/cache' +import { sStorage } from '@/utils/cache' -export const tagsSS = createSessionStorage({ prefixKey: 'tag_' }) -export const activeTag = tagsSS.get('activeTag') -export const tags = tagsSS.get('tags') +export const activeTag = sStorage.get('activeTag') +export const tags = sStorage.get('tags') export const WITHOUT_TAG_PATHS = ['/404', '/login', '/redirect'] diff --git a/src/store/modules/tags/index.js b/src/store/modules/tags/index.js index 16be158..343f3cf 100644 --- a/src/store/modules/tags/index.js +++ b/src/store/modules/tags/index.js @@ -1,6 +1,7 @@ import { defineStore } from 'pinia' -import { tagsSS, activeTag, tags, WITHOUT_TAG_PATHS } from './helpers' +import { activeTag, tags, WITHOUT_TAG_PATHS } from './helpers' import { router } from '@/router' +import { sStorage } from '@/utils/cache' export const useTagsStore = defineStore('tag', { state() { @@ -12,11 +13,11 @@ export const useTagsStore = defineStore('tag', { actions: { setActiveTag(path) { this.activeTag = path - tagsSS.set('activeTag', path) + sStorage.set('activeTag', path) }, setTags(tags) { this.tags = tags - tagsSS.set('tags', tags) + sStorage.set('tags', tags) }, addTag(tag = {}) { this.setActiveTag(tag.path) diff --git a/src/utils/cache/index.js b/src/utils/cache/index.js index f439e16..3d567f3 100644 --- a/src/utils/cache/index.js +++ b/src/utils/cache/index.js @@ -1,15 +1,21 @@ -import { createWebStorage } from './web-storage' +import { createStorage } from './storage' + +const prefixKey = 'Vue_Naive_Admin_' export const createLocalStorage = function (option = {}) { - return createWebStorage({ + return createStorage({ prefixKey: option.prefixKey || '', storage: localStorage, }) } export const createSessionStorage = function (option = {}) { - return createWebStorage({ + return createStorage({ prefixKey: option.prefixKey || '', storage: sessionStorage, }) } + +export const lStorage = createLocalStorage({ prefixKey }) + +export const sStorage = createSessionStorage({ prefixKey }) diff --git a/src/utils/cache/web-storage.js b/src/utils/cache/storage.js similarity index 87% rename from src/utils/cache/web-storage.js rename to src/utils/cache/storage.js index 8509b07..e59c0c0 100644 --- a/src/utils/cache/web-storage.js +++ b/src/utils/cache/storage.js @@ -1,6 +1,6 @@ import { isNullOrUndef } from '@/utils/is' -class WebStorage { +class Storage { constructor(option) { this.storage = option.storage this.prefixKey = option.prefixKey @@ -50,6 +50,6 @@ class WebStorage { } } -export function createWebStorage({ prefixKey = '', storage = sessionStorage }) { - return new WebStorage({ prefixKey, storage }) +export function createStorage({ prefixKey = '', storage = sessionStorage }) { + return new Storage({ prefixKey, storage }) } diff --git a/src/utils/token.js b/src/utils/token.js index a992e55..3a26fb8 100644 --- a/src/utils/token.js +++ b/src/utils/token.js @@ -1,25 +1,23 @@ -import { createLocalStorage } from './cache' +import { lStorage } from './cache' import { refreshToken } from '@/api/auth' const TOKEN_CODE = 'access_token' const DURATION = 6 * 60 * 60 -export const lsToken = createLocalStorage() - export function getToken() { - return lsToken.get(TOKEN_CODE) + return lStorage.get(TOKEN_CODE) } export function setToken(token) { - lsToken.set(TOKEN_CODE, token, DURATION) + lStorage.set(TOKEN_CODE, token, DURATION) } export function removeToken() { - lsToken.remove(TOKEN_CODE) + lStorage.remove(TOKEN_CODE) } export async function refreshAccessToken() { - const tokenItem = lsToken.getItem(TOKEN_CODE) + const tokenItem = lStorage.getItem(TOKEN_CODE) if (!tokenItem) { return } diff --git a/src/views/login/index.vue b/src/views/login/index.vue index 579d510..b287446 100644 --- a/src/views/login/index.vue +++ b/src/views/login/index.vue @@ -41,7 +41,7 @@