forked from vastsa/FileCodeBoxFronted
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.vue
More file actions
148 lines (129 loc) · 3.46 KB
/
Copy pathApp.vue
File metadata and controls
148 lines (129 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<script setup lang="ts">
import { ref, watchEffect, provide, onMounted } from 'vue'
import { RouterView } from 'vue-router'
import ThemeToggle from './components/common/ThemeToggle.vue'
import { useRouter } from 'vue-router'
import api from './utils/api'
const isDarkMode = ref(false)
const isLoading = ref(false)
const router = useRouter()
import AlertComponent from '@/components/common/AlertComponent.vue'
import { useAlertStore } from '@/stores/alertStore'
const alertStore = useAlertStore()
// 检查系统颜色模式
const checkSystemColorScheme = () => {
return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
}
// 从本地存储获取用户之前的选择
const getUserPreference = () => {
const storedPreference = localStorage.getItem('colorMode')
if (storedPreference) {
return storedPreference === 'dark'
}
return null
}
// 设置颜色模式
const setColorMode = (isDark: boolean) => {
isDarkMode.value = isDark
localStorage.setItem('colorMode', isDark ? 'dark' : 'light')
}
onMounted(() => {
const userPreference = getUserPreference()
if (userPreference !== null) {
setColorMode(userPreference)
} else {
setColorMode(checkSystemColorScheme())
}
api.post('/config', {}).then((res: any) => {
if (res.code === 200) {
localStorage.setItem('config', JSON.stringify(res.detail))
if (
res.detail.notify_title &&
res.detail.notify_content &&
localStorage.getItem('notify') !== res.detail.notify_title + res.detail.notify_content
) {
localStorage.setItem('notify', res.detail.notify_title + res.detail.notify_content)
alertStore.showAlert(res.detail.notify_title + ': ' + res.detail.notify_content, 'success')
}
}
})
})
watchEffect(() => {
document.documentElement.classList.toggle('dark', isDarkMode.value)
})
router.beforeEach((to, from, next) => {
isLoading.value = true
next()
})
router.afterEach(() => {
setTimeout(() => {
isLoading.value = false
}, 200) // 添加一个小延迟,以确保组件已加载
})
provide('isDarkMode', isDarkMode)
provide('setColorMode', setColorMode)
provide('isLoading', isLoading)
</script>
<template>
<div :class="['app-container', isDarkMode ? 'dark' : 'light']">
<ThemeToggle v-model="isDarkMode" />
<div v-if="isLoading" class="loading-overlay">
<div class="loading-spinner"></div>
</div>
<RouterView v-slot="{ Component }">
<transition name="fade" mode="out-in">
<component :is="Component" :key="$route.fullPath" />
</transition>
</RouterView>
<AlertComponent />
</div>
</template>
<style>
.app-container {
min-height: 100vh;
width: 100%;
transition: background-color 0.5s ease;
}
.light {
@apply bg-gradient-to-br from-blue-50 via-indigo-50 to-white;
}
.dark {
@apply bg-gradient-to-br from-gray-900 via-indigo-900 to-black;
}
.fade-enter-active,
.fade-leave-active {
transition: opacity 0.3s ease;
}
.fade-enter-from,
.fade-leave-to {
opacity: 0;
}
.loading-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
justify-content: center;
align-items: center;
z-index: 9999;
}
.loading-spinner {
width: 50px;
height: 50px;
border: 3px solid #fff;
border-top: 3px solid #3498db;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
</style>