This commit is contained in:
qsh
2024-08-01 18:41:37 +08:00
parent feccb2a1fa
commit 605151c5c8
17 changed files with 143 additions and 137 deletions

View File

@@ -2,13 +2,12 @@ import { defineStore } from 'pinia'
import { store } from '../index'
import { setCssVar, humpToUnderline } from '@/utils'
import { ElMessage } from 'element-plus'
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
import { CACHE_KEY } from '@/hooks/web/useCache'
import { ElementPlusSize } from '@/types/elementPlus'
import { LayoutType } from '@/types/layout'
import { ThemeTypes } from '@/types/theme'
import { getAppInfo } from '@/api/login'
const { wsCache } = useCache()
import cache from '@/plugins/cache'
interface AppState {
breadcrumb: boolean
@@ -46,7 +45,7 @@ export const useAppStore = defineStore('app', {
sizeMap: ['default', 'large', 'small'],
mobile: false, // 是否是移动端
title: import.meta.env.VITE_APP_TITLE, // 标题
appInfo: wsCache.get('appInfo'),
appInfo: cache.local.get('appInfo'),
pageLoading: false, // 路由跳转loading
breadcrumb: true, // 面包屑
@@ -64,12 +63,12 @@ export const useAppStore = defineStore('app', {
fixedHeader: true, // 固定toolheader
footer: false, // 显示页脚
greyMode: false, // 是否开始灰色模式,用于特殊悼念日
fixedMenu: wsCache.get('fixedMenu') || false, // 是否固定菜单
fixedMenu: cache.local.get('fixedMenu') || false, // 是否固定菜单
layout: wsCache.get(CACHE_KEY.LAYOUT) || 'classic', // layout布局
isDark: wsCache.get(CACHE_KEY.IS_DARK) || false, // 是否是暗黑模式
currentSize: wsCache.get('default') || 'default', // 组件尺寸
theme: wsCache.get(CACHE_KEY.THEME) || {
layout: cache.local.get(CACHE_KEY.LAYOUT) || 'classic', // layout布局
isDark: cache.local.get(CACHE_KEY.IS_DARK) || false, // 是否是暗黑模式
currentSize: cache.local.get('default') || 'default', // 组件尺寸
theme: cache.local.get(CACHE_KEY.THEME) || {
// 主题色
elColorPrimary: '#409eff',
// 左侧菜单边框颜色
@@ -225,7 +224,7 @@ export const useAppStore = defineStore('app', {
this.greyMode = greyMode
},
setFixedMenu(fixedMenu: boolean) {
wsCache.set('fixedMenu', fixedMenu)
cache.local.set('fixedMenu', fixedMenu)
this.fixedMenu = fixedMenu
},
setPageLoading(pageLoading: boolean) {
@@ -237,7 +236,7 @@ export const useAppStore = defineStore('app', {
return
}
this.layout = layout
wsCache.set(CACHE_KEY.LAYOUT, this.layout)
cache.local.set(CACHE_KEY.LAYOUT, this.layout)
},
setTitle(title: string) {
this.title = title
@@ -251,18 +250,18 @@ export const useAppStore = defineStore('app', {
document.documentElement.classList.add('light')
document.documentElement.classList.remove('dark')
}
wsCache.set(CACHE_KEY.IS_DARK, this.isDark)
cache.local.set(CACHE_KEY.IS_DARK, this.isDark)
},
setCurrentSize(currentSize: ElementPlusSize) {
this.currentSize = currentSize
wsCache.set('currentSize', this.currentSize)
cache.local.set('currentSize', this.currentSize)
},
setMobile(mobile: boolean) {
this.mobile = mobile
},
setTheme(theme: ThemeTypes) {
this.theme = Object.assign(this.theme, theme)
wsCache.set(CACHE_KEY.THEME, this.theme)
cache.local.set(CACHE_KEY.THEME, this.theme)
},
setCssVarTheme() {
for (const key in this.theme) {
@@ -274,7 +273,7 @@ export const useAppStore = defineStore('app', {
},
async setAppInfo(appId: number) {
const appInfo = await getAppInfo(appId)
wsCache.set('appInfo', appInfo)
cache.local.set('appInfo', appInfo)
this.appInfo = appInfo
return appInfo
}

View File

@@ -2,8 +2,8 @@ import { defineStore } from 'pinia'
import { store } from '../index'
// @ts-ignore
import { DictDataVO } from '@/api/system/dict/types'
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
const { wsCache } = useCache('sessionStorage')
import { CACHE_KEY } from '@/hooks/web/useCache'
import cache from '@/plugins/cache'
import { listSimpleDictData } from '@/api/system/dict/dict.data'
export interface DictValueType {
@@ -28,7 +28,7 @@ export const useDictStore = defineStore('dict', {
}),
getters: {
getDictMap(): Recordable {
const dictMap = wsCache.get(CACHE_KEY.DICT_CACHE)
const dictMap = cache.session.get(CACHE_KEY.DICT_CACHE)
if (dictMap) {
this.dictMap = dictMap
}
@@ -40,7 +40,7 @@ export const useDictStore = defineStore('dict', {
},
actions: {
async setDictMap() {
const dictMap = wsCache.get(CACHE_KEY.DICT_CACHE)
const dictMap = cache.session.get(CACHE_KEY.DICT_CACHE)
if (dictMap) {
this.dictMap = dictMap
this.isSetDict = true
@@ -64,7 +64,7 @@ export const useDictStore = defineStore('dict', {
})
this.dictMap = dictDataMap
this.isSetDict = true
wsCache.set(CACHE_KEY.DICT_CACHE, dictDataMap, { exp: 60 }) // 60 秒 过期
cache.session.set(CACHE_KEY.DICT_CACHE, dictDataMap) // 60 秒 过期
}
},
getDictByType(type: string) {
@@ -74,7 +74,7 @@ export const useDictStore = defineStore('dict', {
return this.dictMap[type]
},
async resetDict() {
wsCache.delete(CACHE_KEY.DICT_CACHE)
cache.session.delete(CACHE_KEY.DICT_CACHE)
const res = await listSimpleDictData()
// 设置数据
const dictDataMap = new Map<string, any>()
@@ -94,7 +94,7 @@ export const useDictStore = defineStore('dict', {
})
this.dictMap = dictDataMap
this.isSetDict = true
wsCache.set(CACHE_KEY.DICT_CACHE, dictDataMap, { exp: 60 }) // 60 秒 过期
cache.session.set(CACHE_KEY.DICT_CACHE, dictDataMap) // 60 秒 过期
}
}
})

View File

@@ -3,9 +3,8 @@ import { store } from '../index'
import { cloneDeep } from 'lodash-es'
import remainingRouter from '@/router/modules/remaining'
import { generateRoute, flatMultiLevelRoutes } from '@/utils/routerHelper'
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
const { wsCache } = useCache()
import { CACHE_KEY } from '@/hooks/web/useCache'
import cache from '@/plugins/cache'
export interface PermissionState {
routers: AppRouteRecordRaw[]
@@ -34,8 +33,8 @@ export const usePermissionStore = defineStore('permission', {
async generateRoutes(): Promise<unknown> {
return new Promise<void>(async (resolve) => {
let res: AppCustomRouteRecordRaw[] = []
if (wsCache.get(CACHE_KEY.ROLE_ROUTERS)) {
res = wsCache.get(CACHE_KEY.ROLE_ROUTERS) as AppCustomRouteRecordRaw[]
if (cache.local.get(CACHE_KEY.ROLE_ROUTERS)) {
res = cache.local.get(CACHE_KEY.ROLE_ROUTERS) as AppCustomRouteRecordRaw[]
}
const routerMap: AppRouteRecordRaw[] = generateRoute(res)
// 动态路由404一定要放到最后面

View File

@@ -1,10 +1,9 @@
import { store } from '../index'
import { defineStore } from 'pinia'
import { getAccessToken, removeToken } from '@/utils/auth'
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
import { CACHE_KEY } from '@/hooks/web/useCache'
import { getInfo, loginOut } from '@/api/login'
const { wsCache } = useCache()
import cache from '@/plugins/cache'
interface UserVO {
id: number
@@ -49,7 +48,7 @@ export const useUserStore = defineStore('admin-user', {
this.resetState()
return null
}
let userInfo = wsCache.get(CACHE_KEY.USER)
let userInfo = cache.local.get(CACHE_KEY.USER)
if (!userInfo) {
userInfo = await getInfo({})
}
@@ -57,13 +56,13 @@ export const useUserStore = defineStore('admin-user', {
this.roles = userInfo.roles
this.user = userInfo.user
this.isSetUser = true
wsCache.set(CACHE_KEY.USER, userInfo)
wsCache.set(CACHE_KEY.ROLE_ROUTERS, userInfo.menus)
cache.local.set(CACHE_KEY.USER, userInfo)
cache.local.set(CACHE_KEY.ROLE_ROUTERS, userInfo.menus)
},
async loginOut() {
await loginOut()
removeToken()
wsCache.clear()
cache.local.clear()
this.resetState()
},
resetState() {
@@ -77,7 +76,7 @@ export const useUserStore = defineStore('admin-user', {
}
},
refresh() {
wsCache.delete(CACHE_KEY.USER)
cache.local.delete(CACHE_KEY.USER)
this.resetState()
window.location.href = ''
}