fix(darkmode): modify to useDark

This commit is contained in:
Coffee-crocodile 2023-06-21 15:07:06 +08:00
parent 6bc16a28f9
commit 74f90b62b3
7 changed files with 42 additions and 45 deletions

View File

@ -2,6 +2,6 @@ import { viteMockServe } from 'vite-plugin-mock' // https://github.com/vbenjs/vi
export default viteMockServe({ export default viteMockServe({
mockPath: 'mock', mockPath: 'mock',
enable: true, // enable: true,
// watchFiles: false, // watchFiles: false,
}) })

View File

@ -59,7 +59,7 @@
"crypto-js": "^4.1.1", "crypto-js": "^4.1.1",
"echarts": "^5.4.2", "echarts": "^5.4.2",
"md-editor-v3": "^4.2.0", "md-editor-v3": "^4.2.0",
"pinia": "^2.1.3", "pinia": "^2.1.4",
"pinia-plugin-persist": "^1.0.0", "pinia-plugin-persist": "^1.0.0",
"qs": "^6.11.2", "qs": "^6.11.2",
"vue": "^3.3.4", "vue": "^3.3.4",
@ -81,10 +81,10 @@
"commitizen": "^4.2.6", "commitizen": "^4.2.6",
"cz-conventional-changelog": "^3.3.0", "cz-conventional-changelog": "^3.3.0",
"cz-customizable": "^7.0.0", "cz-customizable": "^7.0.0",
"eslint": "^8.42.0", "eslint": "^8.43.0",
"husky": "^8.0.3", "husky": "^8.0.3",
"less": "^4.1.3", "less": "^4.1.3",
"lint-staged": "^13.1.0", "lint-staged": "^13.2.2",
"mockjs": "^1.1.0", "mockjs": "^1.1.0",
"naive-ui": "^2.34.4", "naive-ui": "^2.34.4",
"rollup-plugin-visualizer": "^5.9.0", "rollup-plugin-visualizer": "^5.9.0",
@ -97,9 +97,9 @@
"vite-plugin-compression": "^0.5.1", "vite-plugin-compression": "^0.5.1",
"vite-plugin-mock": "^2.9.6", "vite-plugin-mock": "^2.9.6",
"vite-plugin-svg-icons": "^2.0.1", "vite-plugin-svg-icons": "^2.0.1",
"vite-plugin-vue-devtools": "^0.2.0", "vite-plugin-vue-devtools": "^0.2.1",
"vite-plugin-vue-setup-extend": "^0.4.0", "vite-plugin-vue-setup-extend": "^0.4.0",
"vue-tsc": "^1.6.5" "vue-tsc": "^1.8.1"
}, },
"lint-staged": { "lint-staged": {
"*.{js,jsx,mjs,ts,tsx,json,md,yml}": [ "*.{js,jsx,mjs,ts,tsx,json,md,yml}": [

View File

@ -1,21 +1,21 @@
<script setup lang="ts"> <script setup lang="ts">
import type { GlobalThemeOverrides } from 'naive-ui' import type { GlobalThemeOverrides } from 'naive-ui'
import { dateZhCN, useOsTheme, zhCN } from 'naive-ui' import { darkTheme, dateZhCN, zhCN } from 'naive-ui'
import { useAppStore } from './store' import { useAppStore } from './store'
import themeConfig from './theme.json' import themeConfig from './theme.json'
const locale = zhCN const locale = zhCN
const dateLocale = dateZhCN const dateLocale = dateZhCN
const appStore = useAppStore() const appStore = useAppStore()
const osThemeRef = useOsTheme() // const osThemeRef = useOsTheme()
appStore.setDarkMode(osThemeRef.value === 'dark') // appStore.toggleDarkMode(osThemeRef.value === 'dark')
const themeOverrides: GlobalThemeOverrides = {} || themeConfig const themeOverrides: GlobalThemeOverrides = {} || themeConfig
</script> </script>
<template> <template>
<n-config-provider <n-config-provider
class="wh-full" :theme="appStore.darkTheme" :locale="locale" :date-locale="dateLocale" class="wh-full" :theme="appStore.darkMode ? darkTheme : null" :locale="locale" :date-locale="dateLocale"
:theme-overrides="themeOverrides" :theme-overrides="themeOverrides"
> >
<naive-provider><router-view /></naive-provider> <naive-provider><router-view /></naive-provider>

View File

@ -3,27 +3,32 @@ import AppLoading from './components/common/appLoading.vue'
import { setupRouter } from './router' import { setupRouter } from './router'
import { setupAssets } from './plugins' import { setupAssets } from './plugins'
import { setupStore } from './store' import { setupStore } from './store'
import { setupDirectives } from './directive' import { setupDirectives } from './directive'
async function setupApp() { async function setupApp() {
// 引入静态资源 // 引入静态资源
setupAssets() setupAssets()
// 创建vue实例
const app = createApp(App)
// 安装pinia全局状态库
setupStore(app)
// 载入全局loading加载状态 // 载入全局loading加载状态
const appLoading = createApp(AppLoading) const appLoading = createApp(AppLoading)
appLoading.mount('#appLoading') appLoading.mount('#appLoading')
// 创建vue实例
const app = createApp(App)
// 安装pinia全局状态库
setupStore (app)
// 安装自定义指令 // 安装自定义指令
setupDirectives(app) setupDirectives(app)
// 安装router // 安装router
await setupRouter(app) await setupRouter(app)
// 挂载
app.mount('#app')
// 卸载载入动画 // 卸载载入动画
appLoading.unmount() appLoading.unmount()
// 挂载
app.mount('#app')
} }
await setupApp() await setupApp()

View File

@ -2,8 +2,9 @@ import piniaPluginPersist from 'pinia-plugin-persist'
import type { App } from 'vue' import type { App } from 'vue'
export function setupStore(app: App) { export function setupStore(app: App) {
const store = createPinia() const pinia = createPinia()
store.use(piniaPluginPersist) pinia.use(piniaPluginPersist)
app.use(store) app.use(pinia)
} }
export * from './modules' export * from './modules'

View File

@ -1,6 +1,3 @@
import type { GlobalTheme } from 'naive-ui'
import { darkTheme } from 'naive-ui'
interface AppStatus { interface AppStatus {
readonly footerText: string readonly footerText: string
collapsed: boolean collapsed: boolean
@ -8,7 +5,6 @@ interface AppStatus {
darkMode: boolean darkMode: boolean
grayMode: boolean grayMode: boolean
colorWeak: boolean colorWeak: boolean
darkTheme: GlobalTheme | null
loadFlag: boolean loadFlag: boolean
showLogo: boolean showLogo: boolean
showTabs: boolean showTabs: boolean
@ -23,16 +19,19 @@ const docEle = document.documentElement
const { isFullscreen, toggle } = useFullscreen(docEle) const { isFullscreen, toggle } = useFullscreen(docEle)
const isDark = useDark({
storageKey: 'admin-dark-mode',
})
export const useAppStore = defineStore('app-store', { export const useAppStore = defineStore('app-store', {
state: (): AppStatus => { state: (): AppStatus => {
return { return {
footerText: 'Copyright ©2023 Ench Admin', footerText: 'Copyright ©2023 Ench Admin',
collapsed: false, collapsed: false,
fullScreen: isFullscreen.value, fullScreen: false,
darkMode: false, darkMode: isDark.value,
grayMode: false, grayMode: false,
colorWeak: false, colorWeak: false,
darkTheme: null,
loadFlag: true, loadFlag: true,
showLogo: true, showLogo: true,
showTabs: true, showTabs: true,
@ -54,22 +53,14 @@ export const useAppStore = defineStore('app-store', {
await toggle() await toggle()
}, },
/* 切换主题 亮/深色 */ /* 切换主题 亮/深色 */
toggleDarkMode() { toggleDarkMode(mode?: boolean) {
this.darkMode = !this.darkMode if (typeof mode === 'boolean')
if (this.darkMode) isDark.value = mode
this.darkTheme = darkTheme
else this.darkTheme = null else
}, isDark.value = !isDark.value
/* 设置主题深色 */
setDarkMode(mode: boolean) { this.darkMode = isDark.value
if (mode) {
this.darkMode = true
this.darkTheme = darkTheme
}
else {
this.darkMode = false
this.darkTheme = null
}
}, },
/** /**
* @description: * @description:

View File

@ -4,7 +4,6 @@ import { router } from '@/router'
import { useAppRouter } from '@/hooks' import { useAppRouter } from '@/hooks'
import { local } from '@/utils' import { local } from '@/utils'
const routeStore = useRouteStore()
const emptyInfo: Auth.UserInfo = { const emptyInfo: Auth.UserInfo = {
userId: 0, userId: 0,
userName: '', userName: '',
@ -35,6 +34,7 @@ export const useAuthStore = defineStore('auth-store', {
// 清除本地缓存 // 清除本地缓存
this.clearAuthStorage() this.clearAuthStorage()
// 清空路由、菜单等数据 // 清空路由、菜单等数据
const routeStore = useRouteStore()
routeStore.resetRouteStore() routeStore.resetRouteStore()
this.$reset() this.$reset()
if (route.meta.requiresAuth) if (route.meta.requiresAuth)
@ -66,7 +66,7 @@ export const useAuthStore = defineStore('auth-store', {
const catchSuccess = await this.catchUserInfo(data) const catchSuccess = await this.catchUserInfo(data)
// 添加路由和菜单 // 添加路由和菜单
// const { initAuthRoute } = useRouteStore() const routeStore = useRouteStore()
await routeStore.initAuthRoute() await routeStore.initAuthRoute()
// 登录写入信息成功 // 登录写入信息成功