153 lines
3.5 KiB
JavaScript
153 lines
3.5 KiB
JavaScript
![]() |
import { defineStore } from 'pinia'
|
||
|
|
||
|
export const useAppStore = defineStore('app', {
|
||
|
state: () => ({
|
||
|
// 侧边栏
|
||
|
sidebar: {
|
||
|
opened: true,
|
||
|
withoutAnimation: false
|
||
|
},
|
||
|
|
||
|
// 设备类型
|
||
|
device: 'desktop',
|
||
|
|
||
|
// 主题
|
||
|
theme: 'light',
|
||
|
|
||
|
// 加载状态
|
||
|
loading: false,
|
||
|
|
||
|
// 页面标题
|
||
|
title: '企业级日报系统',
|
||
|
|
||
|
// 标签页
|
||
|
visitedViews: [],
|
||
|
cachedViews: []
|
||
|
}),
|
||
|
|
||
|
getters: {
|
||
|
sidebarOpened: (state) => state.sidebar.opened,
|
||
|
isMobile: (state) => state.device === 'mobile',
|
||
|
isDark: (state) => state.theme === 'dark'
|
||
|
},
|
||
|
|
||
|
actions: {
|
||
|
// 切换侧边栏
|
||
|
toggleSidebar() {
|
||
|
this.sidebar.opened = !this.sidebar.opened
|
||
|
this.sidebar.withoutAnimation = false
|
||
|
},
|
||
|
|
||
|
// 关闭侧边栏
|
||
|
closeSidebar(withoutAnimation = false) {
|
||
|
this.sidebar.opened = false
|
||
|
this.sidebar.withoutAnimation = withoutAnimation
|
||
|
},
|
||
|
|
||
|
// 设置设备类型
|
||
|
setDevice(device) {
|
||
|
this.device = device
|
||
|
},
|
||
|
|
||
|
// 切换主题
|
||
|
toggleTheme() {
|
||
|
this.theme = this.theme === 'light' ? 'dark' : 'light'
|
||
|
this.updateTheme()
|
||
|
},
|
||
|
|
||
|
// 设置主题
|
||
|
setTheme(theme) {
|
||
|
this.theme = theme
|
||
|
this.updateTheme()
|
||
|
},
|
||
|
|
||
|
// 更新主题
|
||
|
updateTheme() {
|
||
|
const html = document.documentElement
|
||
|
if (this.theme === 'dark') {
|
||
|
html.classList.add('dark')
|
||
|
} else {
|
||
|
html.classList.remove('dark')
|
||
|
}
|
||
|
localStorage.setItem('theme', this.theme)
|
||
|
},
|
||
|
|
||
|
// 初始化主题
|
||
|
initTheme() {
|
||
|
const savedTheme = localStorage.getItem('theme')
|
||
|
if (savedTheme) {
|
||
|
this.setTheme(savedTheme)
|
||
|
} else {
|
||
|
// 检查系统主题偏好
|
||
|
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches
|
||
|
this.setTheme(prefersDark ? 'dark' : 'light')
|
||
|
}
|
||
|
},
|
||
|
|
||
|
// 设置加载状态
|
||
|
setLoading(loading) {
|
||
|
this.loading = loading
|
||
|
},
|
||
|
|
||
|
// 设置页面标题
|
||
|
setTitle(title) {
|
||
|
this.title = title
|
||
|
document.title = title
|
||
|
},
|
||
|
|
||
|
// 添加访问过的视图
|
||
|
addVisitedView(view) {
|
||
|
if (this.visitedViews.some(v => v.path === view.path)) return
|
||
|
this.visitedViews.push({
|
||
|
name: view.name,
|
||
|
path: view.path,
|
||
|
title: view.meta?.title || view.name
|
||
|
})
|
||
|
},
|
||
|
|
||
|
// 删除访问过的视图
|
||
|
delVisitedView(view) {
|
||
|
const index = this.visitedViews.findIndex(v => v.path === view.path)
|
||
|
if (index > -1) {
|
||
|
this.visitedViews.splice(index, 1)
|
||
|
}
|
||
|
},
|
||
|
|
||
|
// 删除其他访问过的视图
|
||
|
delOthersVisitedViews(view) {
|
||
|
this.visitedViews = this.visitedViews.filter(v => v.path === view.path)
|
||
|
},
|
||
|
|
||
|
// 删除所有访问过的视图
|
||
|
delAllVisitedViews() {
|
||
|
this.visitedViews = []
|
||
|
},
|
||
|
|
||
|
// 添加缓存视图
|
||
|
addCachedView(view) {
|
||
|
if (this.cachedViews.includes(view.name)) return
|
||
|
if (!view.meta?.noCache) {
|
||
|
this.cachedViews.push(view.name)
|
||
|
}
|
||
|
},
|
||
|
|
||
|
// 删除缓存视图
|
||
|
delCachedView(view) {
|
||
|
const index = this.cachedViews.indexOf(view.name)
|
||
|
if (index > -1) {
|
||
|
this.cachedViews.splice(index, 1)
|
||
|
}
|
||
|
},
|
||
|
|
||
|
// 删除其他缓存视图
|
||
|
delOthersCachedViews(view) {
|
||
|
this.cachedViews = this.cachedViews.filter(name => name === view.name)
|
||
|
},
|
||
|
|
||
|
// 删除所有缓存视图
|
||
|
delAllCachedViews() {
|
||
|
this.cachedViews = []
|
||
|
}
|
||
|
}
|
||
|
})
|