新增
This commit is contained in:
@@ -4,6 +4,8 @@ import { getConfig } from '@/api/app'
|
||||
|
||||
interface AppSate {
|
||||
config: Record<string, any>
|
||||
/** 是否已尝试过拉取 getConfig(失败也置 true,避免路由守卫死循环) */
|
||||
configFetchAttempted: boolean
|
||||
isMobile: boolean
|
||||
isCollapsed: boolean
|
||||
isRouteShow: boolean
|
||||
@@ -14,6 +16,7 @@ const useAppStore = defineStore({
|
||||
state: (): AppSate => {
|
||||
return {
|
||||
config: {},
|
||||
configFetchAttempted: false,
|
||||
isMobile: true,
|
||||
isCollapsed: false,
|
||||
isRouteShow: true
|
||||
|
||||
@@ -30,6 +30,12 @@ const getHasTabIndex = (fullPath: string, tabList: TabItem[]) => {
|
||||
return tabList.findIndex((item) => item.fullPath == fullPath)
|
||||
}
|
||||
|
||||
/** 同一菜单页可能因 query、重定向等产生不同 fullPath,用 name + 解析后的 path 去重 */
|
||||
const getTabMergeKey = (item: Pick<TabItem, 'name' | 'path'>) => {
|
||||
if (item.name == null || item.name === '') return ''
|
||||
return `${String(item.name)}::${item.path}`
|
||||
}
|
||||
|
||||
const isCannotAddRoute = (route: RouteLocationNormalized, router: Router) => {
|
||||
const { path, meta, name } = route
|
||||
if (!path || isExternal(path)) return true
|
||||
@@ -109,14 +115,41 @@ const useTabsStore = defineStore({
|
||||
query,
|
||||
params
|
||||
}
|
||||
this.tasMap[fullPath] = tabItem
|
||||
if (meta?.keepAlive) {
|
||||
this.addCache(componentName)
|
||||
}
|
||||
if (hasTabIndex != -1) {
|
||||
|
||||
// 已存在相同 fullPath:刷新该项(标题/query 等可能变化)
|
||||
if (hasTabIndex !== -1) {
|
||||
this.tabList[hasTabIndex] = tabItem
|
||||
this.tasMap[fullPath!] = tabItem
|
||||
return
|
||||
}
|
||||
|
||||
const mergeKey = getTabMergeKey(tabItem)
|
||||
if (mergeKey) {
|
||||
const dupIndices: number[] = []
|
||||
this.tabList.forEach((item, i) => {
|
||||
if (getTabMergeKey(item) === mergeKey) dupIndices.push(i)
|
||||
})
|
||||
if (dupIndices.length > 0) {
|
||||
const keepIdx = dupIndices[0]
|
||||
for (let k = dupIndices.length - 1; k >= 1; k--) {
|
||||
const idx = dupIndices[k]
|
||||
delete this.tasMap[this.tabList[idx].fullPath]
|
||||
this.tabList.splice(idx, 1)
|
||||
}
|
||||
const prev = this.tabList[keepIdx]
|
||||
if (prev.fullPath !== fullPath) {
|
||||
delete this.tasMap[prev.fullPath]
|
||||
}
|
||||
this.tabList[keepIdx] = tabItem
|
||||
this.tasMap[fullPath!] = tabItem
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
this.tasMap[fullPath!] = tabItem
|
||||
this.tabList.push(tabItem)
|
||||
},
|
||||
removeTab(fullPath: string, router: Router) {
|
||||
|
||||
@@ -1,21 +1,52 @@
|
||||
import { isObject } from '@vue/shared'
|
||||
import { defineStore } from 'pinia'
|
||||
|
||||
import defaultSetting from '@/config/setting'
|
||||
import defaultSetting, {
|
||||
SETTING_SCHEMA_MIGRATIONS,
|
||||
SETTING_SCHEMA_VERSION
|
||||
} from '@/config/setting'
|
||||
import { SETTING_KEY } from '@/enums/cacheEnums'
|
||||
import cache from '@/utils/cache'
|
||||
import { setTheme } from '@/utils/theme'
|
||||
|
||||
const storageSetting = cache.get(SETTING_KEY)
|
||||
|
||||
function applySettingSchemaMigrations(
|
||||
state: Record<string, any>,
|
||||
storedVersion: number
|
||||
) {
|
||||
if (storedVersion >= SETTING_SCHEMA_VERSION) return
|
||||
for (let ver = storedVersion + 1; ver <= SETTING_SCHEMA_VERSION; ver++) {
|
||||
const keys = SETTING_SCHEMA_MIGRATIONS[ver]
|
||||
if (!keys) continue
|
||||
for (const key of keys) {
|
||||
state[key] = defaultSetting[key]
|
||||
}
|
||||
}
|
||||
state._settingSchemaVersion = SETTING_SCHEMA_VERSION
|
||||
const { showDrawer, ...persist } = state
|
||||
cache.set(SETTING_KEY, persist)
|
||||
}
|
||||
|
||||
export const useSettingStore = defineStore({
|
||||
id: 'setting',
|
||||
state: () => {
|
||||
const state = {
|
||||
const state: Record<string, any> = {
|
||||
showDrawer: false,
|
||||
...defaultSetting
|
||||
}
|
||||
isObject(storageSetting) && Object.assign(state, storageSetting)
|
||||
let storedVersion = SETTING_SCHEMA_VERSION
|
||||
if (isObject(storageSetting)) {
|
||||
storedVersion =
|
||||
typeof (storageSetting as any)._settingSchemaVersion === 'number'
|
||||
? (storageSetting as any)._settingSchemaVersion
|
||||
: 0
|
||||
Object.assign(state, storageSetting)
|
||||
if (storedVersion < SETTING_SCHEMA_VERSION) {
|
||||
applySettingSchemaMigrations(state, storedVersion)
|
||||
}
|
||||
}
|
||||
state._settingSchemaVersion = SETTING_SCHEMA_VERSION
|
||||
return state
|
||||
},
|
||||
actions: {
|
||||
@@ -28,6 +59,7 @@ export const useSettingStore = defineStore({
|
||||
}
|
||||
const settings: any = Object.assign({}, this.$state)
|
||||
delete settings.showDrawer
|
||||
settings._settingSchemaVersion = SETTING_SCHEMA_VERSION
|
||||
cache.set(SETTING_KEY, settings)
|
||||
},
|
||||
// 设置主题色
|
||||
@@ -49,6 +81,7 @@ export const useSettingStore = defineStore({
|
||||
//@ts-ignore
|
||||
this[key] = defaultSetting[key]
|
||||
}
|
||||
this._settingSchemaVersion = SETTING_SCHEMA_VERSION
|
||||
cache.remove(SETTING_KEY)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user