|
|
@@ -1,97 +1,83 @@
|
|
1
|
1
|
import { defineStore } from 'pinia'
|
|
2
|
2
|
import { ref, computed } from 'vue'
|
|
3
|
3
|
|
|
4
|
|
-export const useThemeStore = defineStore('theme', () => {
|
|
5
|
|
- // 可用主题列表
|
|
6
|
|
- const themes = ref({
|
|
7
|
|
- default: {
|
|
8
|
|
- '--primary-color': '#1890ff',
|
|
9
|
|
- '--bg-color': '#f8f9fa',
|
|
10
|
|
- '--text-color': '#333',
|
|
11
|
|
- '--card-bg': '#ffffff'
|
|
|
4
|
+export const useThemeStore = defineStore('theme', {
|
|
|
5
|
+ state: () => ({
|
|
|
6
|
+ themes: {
|
|
|
7
|
+ default: {
|
|
|
8
|
+ '--primary-color': '#1890ff',
|
|
|
9
|
+ '--bg-color': '#f8f9fa',
|
|
|
10
|
+ '--text-color': '#333',
|
|
|
11
|
+ '--card-bg': '#ffffff'
|
|
|
12
|
+ },
|
|
|
13
|
+ aydzBlue: {
|
|
|
14
|
+ '--primary-color': '#2a5caa',
|
|
|
15
|
+ '--bg-color': '#e6f7ff',
|
|
|
16
|
+ '--text-color': '#1a3353',
|
|
|
17
|
+ '--card-bg': '#d0e8ff'
|
|
|
18
|
+ },
|
|
|
19
|
+ darkMode: {
|
|
|
20
|
+ '--primary-color': '#52c41a',
|
|
|
21
|
+ '--bg-color': '#1a1a1a',
|
|
|
22
|
+ '--text-color': '#e6e6e6',
|
|
|
23
|
+ '--card-bg': '#2a2a2a'
|
|
|
24
|
+ }
|
|
12
|
25
|
},
|
|
13
|
|
- aydzBlue: {
|
|
14
|
|
- '--primary-color': '#2a5caa',
|
|
15
|
|
- '--bg-color': '#e6f7ff',
|
|
16
|
|
- '--text-color': '#1a3353',
|
|
17
|
|
- '--card-bg': '#d0e8ff'
|
|
18
|
|
- },
|
|
19
|
|
- darkMode: {
|
|
20
|
|
- '--primary-color': '#52c41a',
|
|
21
|
|
- '--bg-color': '#1a1a1a',
|
|
22
|
|
- '--text-color': '#e6e6e6',
|
|
23
|
|
- '--card-bg': '#2a2a2a'
|
|
24
|
|
- }
|
|
25
|
|
- })
|
|
26
|
|
-
|
|
27
|
|
- // 当前主题
|
|
28
|
|
- const currentTheme = ref('aydzBlue')
|
|
29
|
|
-
|
|
30
|
|
- // 初始化主题
|
|
31
|
|
- const initTheme = () => {
|
|
32
|
|
- const savedTheme = uni.getStorageSync('selectedTheme') || 'aydzBlue'
|
|
33
|
|
- setTheme(savedTheme)
|
|
34
|
|
- }
|
|
35
|
|
-
|
|
36
|
|
- // 设置主题
|
|
37
|
|
- const setTheme = (themeName) => {
|
|
38
|
|
- if (!themes.value[themeName]) return
|
|
39
|
|
-
|
|
40
|
|
- currentTheme.value = themeName
|
|
41
|
|
- uni.setStorageSync('selectedTheme', themeName)
|
|
42
|
|
-
|
|
43
|
|
- // 应用CSS变量
|
|
44
|
|
- const root = document.documentElement
|
|
45
|
|
- const themeVars = themes.value[themeName]
|
|
46
|
|
-
|
|
47
|
|
- Object.keys(themeVars).forEach(key => {
|
|
48
|
|
- root.style.setProperty(key, themeVars[key])
|
|
49
|
|
- })
|
|
50
|
|
-
|
|
51
|
|
- // 动态设置导航栏颜色
|
|
52
|
|
- if (themeName === 'darkMode') {
|
|
53
|
|
- uni.setNavigationBarColor({
|
|
54
|
|
- frontColor: '#ffffff',
|
|
55
|
|
- backgroundColor: '#1a1a1a'
|
|
56
|
|
- })
|
|
57
|
|
- } else if (themeName === 'aydzBlue') {
|
|
58
|
|
- uni.setNavigationBarColor({
|
|
59
|
|
- frontColor: '#ffffff',
|
|
60
|
|
- backgroundColor: '#2a5caa'
|
|
61
|
|
- })
|
|
62
|
|
- } else {
|
|
63
|
|
- uni.setNavigationBarColor({
|
|
64
|
|
- frontColor: '#000000',
|
|
65
|
|
- backgroundColor: '#f8f9fa'
|
|
66
|
|
- })
|
|
67
|
|
- }
|
|
68
|
|
- }
|
|
69
|
|
-
|
|
70
|
|
- // 主题配置(用于UI显示)
|
|
71
|
|
- const themeOptions = computed(() => {
|
|
72
|
|
- return Object.keys(themes.value).map(key => ({
|
|
73
|
|
- name: key,
|
|
74
|
|
- label: getThemeLabel(key),
|
|
75
|
|
- colors: themes.value[key]
|
|
76
|
|
- }))
|
|
77
|
|
- })
|
|
78
|
|
-
|
|
79
|
|
- // 获取主题显示名称
|
|
80
|
|
- const getThemeLabel = (themeKey) => {
|
|
81
|
|
- const labels = {
|
|
82
|
|
- default: '默认主题',
|
|
83
|
|
- aydzBlue: '哎呀科技蓝',
|
|
84
|
|
- darkMode: '深色模式'
|
|
85
|
|
- }
|
|
86
|
|
- return labels[themeKey] || themeKey
|
|
87
|
|
- }
|
|
88
|
|
-
|
|
89
|
|
- return {
|
|
90
|
|
- themes,
|
|
91
|
|
- currentTheme,
|
|
92
|
|
- themeOptions,
|
|
93
|
|
- initTheme,
|
|
94
|
|
- setTheme,
|
|
95
|
|
- getThemeLabel
|
|
96
|
|
- }
|
|
97
|
|
-})
|
|
|
26
|
+ currentTheme: 'aydzBlue'
|
|
|
27
|
+ }),
|
|
|
28
|
+ actions: {
|
|
|
29
|
+ initTheme() {
|
|
|
30
|
+ const savedTheme = uni.getStorageSync('selectedTheme') || 'aydzBlue'
|
|
|
31
|
+ this.setTheme(savedTheme)
|
|
|
32
|
+ },
|
|
|
33
|
+ setTheme(themeName) {
|
|
|
34
|
+ if (!this.themes[themeName]) return
|
|
|
35
|
+
|
|
|
36
|
+ this.currentTheme = themeName
|
|
|
37
|
+ uni.setStorageSync('selectedTheme', themeName)
|
|
|
38
|
+
|
|
|
39
|
+ // 应用CSS变量
|
|
|
40
|
+ const root = document.documentElement
|
|
|
41
|
+ const themeVars = this.themes[themeName]
|
|
|
42
|
+
|
|
|
43
|
+ Object.keys(themeVars).forEach(key => {
|
|
|
44
|
+ root.style.setProperty(key, themeVars[key])
|
|
|
45
|
+ })
|
|
|
46
|
+
|
|
|
47
|
+ // 动态设置导航栏颜色
|
|
|
48
|
+ if (themeName === 'darkMode') {
|
|
|
49
|
+ uni.setNavigationBarColor({
|
|
|
50
|
+ frontColor: '#ffffff',
|
|
|
51
|
+ backgroundColor: '#1a1a1a'
|
|
|
52
|
+ })
|
|
|
53
|
+ } else if (themeName === 'aydzBlue') {
|
|
|
54
|
+ uni.setNavigationBarColor({
|
|
|
55
|
+ frontColor: '#ffffff',
|
|
|
56
|
+ backgroundColor: '#2a5caa'
|
|
|
57
|
+ })
|
|
|
58
|
+ } else {
|
|
|
59
|
+ uni.setNavigationBarColor({
|
|
|
60
|
+ frontColor: '#000000',
|
|
|
61
|
+ backgroundColor: '#f8f9fa'
|
|
|
62
|
+ })
|
|
|
63
|
+ }
|
|
|
64
|
+ },
|
|
|
65
|
+ getThemeLabel(themeKey) {
|
|
|
66
|
+ const labels = {
|
|
|
67
|
+ default: '默认主题',
|
|
|
68
|
+ aydzBlue: '哎呀科技蓝',
|
|
|
69
|
+ darkMode: '深色模式'
|
|
|
70
|
+ }
|
|
|
71
|
+ return labels[themeKey] || themeKey
|
|
|
72
|
+ }
|
|
|
73
|
+ },
|
|
|
74
|
+ getters: {
|
|
|
75
|
+ themeOptions: (state) => {
|
|
|
76
|
+ return Object.keys(state.themes).map(key => ({
|
|
|
77
|
+ name: key,
|
|
|
78
|
+ label: state.getThemeLabel(key),
|
|
|
79
|
+ colors: state.themes[key]
|
|
|
80
|
+ }))
|
|
|
81
|
+ }
|
|
|
82
|
+ }
|
|
|
83
|
+ })
|