gaoluyang
2026-11-03 2d8b1a28fa5c8b852f0d0078bac4894bfb4abed5
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { computed, readonly, ref } from "vue";
import { APP_COMPANY } from "../config/company";
 
export type ThemeKey = "blue" | "electric" | "green" | "aurora" | "purple";
 
export interface AppTheme {
  key: ThemeKey;
  name: string;
  description: string;
  primary: string;
  primaryDark: string;
  primaryLight: string;
  primarySoft: string;
  primaryBorder: string;
  pageBackground: string;
  cardBackground: string;
}
 
export const THEME_STORAGE_KEY = `mom_app_theme_${APP_COMPANY.key}`;
 
export const appThemes: AppTheme[] = [
  {
    key: "blue",
    name: "商务蓝",
    description: "清晰稳重,适合日常办公",
    primary: "#1769d8",
    primaryDark: "#0f54b8",
    primaryLight: "#4b8ee8",
    primarySoft: "#eef5ff",
    primaryBorder: "#bfdbff",
    pageBackground: "#f6f7f9",
    cardBackground: "#ffffff",
  },
  {
    key: "green",
    name: "生产绿",
    description: "沉稳自然,突出生产状态",
    primary: "#16835d",
    primaryDark: "#0f6949",
    primaryLight: "#42a782",
    primarySoft: "#eaf8f2",
    primaryBorder: "#bdebd7",
    pageBackground: "#f5f8f7",
    cardBackground: "#ffffff",
  },
  {
    key: "electric",
    name: "电力深蓝",
    description: "冷峻清晰,突出电力运行状态",
    primary: "#0879e8",
    primaryDark: "#0756ac",
    primaryLight: "#47a9ff",
    primarySoft: "#e9f5ff",
    primaryBorder: "#b7ddff",
    pageBackground: "#f2f7fb",
    cardBackground: "#ffffff",
  },
  {
    key: "aurora",
    name: "智能极光",
    description: "紫青渐变,体现智能科技感",
    primary: "#7257df",
    primaryDark: "#5640b7",
    primaryLight: "#9a84f3",
    primarySoft: "#f2efff",
    primaryBorder: "#d9d0ff",
    pageBackground: "#f7f5fc",
    cardBackground: "#ffffff",
  },
  {
    key: "purple",
    name: "科技紫",
    description: "层次鲜明,适合数据场景",
    primary: "#7156c7",
    primaryDark: "#5940ad",
    primaryLight: "#927de0",
    primarySoft: "#f3efff",
    primaryBorder: "#d8ccff",
    pageBackground: "#f7f6fa",
    cardBackground: "#ffffff",
  },
];
 
const companyDefaultThemeKey: ThemeKey = APP_COMPANY.key === "changhui"
  ? "electric"
  : APP_COMPANY.key === "kangyi"
    ? "aurora"
    : "blue";
const defaultTheme = appThemes.find(item => item.key === companyDefaultThemeKey) || appThemes[0];
const currentTheme = ref<AppTheme>(defaultTheme);
 
function findTheme(key?: string) {
  return appThemes.find((item) => item.key === key) || defaultTheme;
}
 
function syncPlatformChrome(theme: AppTheme) {
  uni.setNavigationBarColor({
    frontColor: "#000000",
    backgroundColor: theme.cardBackground,
    animation: { duration: 180, timingFunc: "easeIn" },
    fail: () => undefined,
  });
  uni.setTabBarStyle({
    color: "#7a8494",
    selectedColor: theme.primary,
    backgroundColor: theme.cardBackground,
    borderStyle: "white",
    fail: () => undefined,
  });
}
 
function syncCssVariables(theme: AppTheme) {
  // #ifdef H5
  if (typeof document !== "undefined") {
    const root = document.documentElement;
    root.style.setProperty("--color-primary", theme.primary);
    root.style.setProperty("--color-primary-dark", theme.primaryDark);
    root.style.setProperty("--color-primary-light", theme.primaryLight);
    root.style.setProperty("--color-primary-soft", theme.primarySoft);
    root.style.setProperty("--color-primary-border", theme.primaryBorder);
    root.style.setProperty("--color-bg-page", theme.pageBackground);
    root.style.setProperty("--color-bg-card", theme.cardBackground);
  }
  // #endif
}
 
export function applyTheme(theme: AppTheme) {
  currentTheme.value = theme;
  syncCssVariables(theme);
  syncPlatformChrome(theme);
  uni.$emit("app-theme-change", theme);
}
 
export function restoreTheme() {
  let savedKey: string | undefined;
  try {
    savedKey = uni.getStorageSync(THEME_STORAGE_KEY) || undefined;
  } catch {
    savedKey = undefined;
  }
  applyTheme(findTheme(savedKey));
}
 
export function setTheme(key: ThemeKey) {
  const theme = findTheme(key);
  uni.setStorageSync(THEME_STORAGE_KEY, theme.key);
  applyTheme(theme);
}
 
export function resetTheme() {
  uni.removeStorageSync(THEME_STORAGE_KEY);
  applyTheme(defaultTheme);
}
 
export function useAppTheme() {
  return {
    themes: appThemes,
    theme: readonly(currentTheme),
    themeKey: computed(() => currentTheme.value.key),
    primaryColor: computed(() => currentTheme.value.primary),
    themeStyle: computed(() => ({
      "--color-primary": currentTheme.value.primary,
      "--color-primary-dark": currentTheme.value.primaryDark,
      "--color-primary-light": currentTheme.value.primaryLight,
      "--color-primary-soft": currentTheme.value.primarySoft,
      "--color-primary-border": currentTheme.value.primaryBorder,
      "--color-bg-page": currentTheme.value.pageBackground,
      "--color-bg-card": currentTheme.value.cardBackground,
    })),
    setTheme,
    resetTheme,
    restoreTheme,
  };
}