@hlw-uni/mp-vue 2.1.58 → 2.1.60

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,129 +0,0 @@
1
- /**
2
- * 外观模式 — 浅色 / 深色 / 跟随系统
3
- *
4
- * 提供一套语义化 CSS 变量(--bg-page, --surface-card, --text-primary ...)。
5
- * 默认不通过 useThemePageStyle 注入,业务项目应在全局 CSS 中声明这些变量,
6
- * 避免运行时 page-meta 覆盖业务侧样式。
7
- *
8
- * 使用指南:
9
- * - 业务样式里用 `var(--text-primary)` 代替硬编码 `#0f172a`
10
- * - 用 `var(--surface-card)` 代替硬编码 `#ffffff`
11
- * - 用 `var(--border-color-light)` 代替硬编码 `#f1f5f9`
12
- */
13
-
14
- /** 外观模式选项类型:浅色模式、深色模式、跟随系统 */
15
- export type Appearance = "light" | "dark" | "auto";
16
-
17
- /** 实际生效的外观模式:仅浅色或深色 */
18
- export type AppearanceMode = "light" | "dark";
19
-
20
- /**
21
- * 外观预设配置接口。
22
- */
23
- export interface AppearancePreset {
24
- /** 选项值 */
25
- value: Appearance;
26
- /** 选项展示名称 */
27
- label: string;
28
- }
29
-
30
- /** 存储外观设置的 LocalStorage 键名 */
31
- export const APPEARANCE_KEY = "hlw_appearance";
32
-
33
- /** 预设外观模式列表,常供设置页渲染选择器使用 */
34
- export const APPEARANCE_PRESETS: AppearancePreset[] = [
35
- { value: "light", label: "浅色模式" },
36
- { value: "dark", label: "深色模式" },
37
- { value: "auto", label: "跟随系统" },
38
- ];
39
-
40
- /** 浅色模式语义变量 */
41
- const LIGHT_VARS: Record<string, string> = {
42
- "--bg-page": "#f6f6f6",
43
- "--bg-elevated": "#ffffff",
44
- "--surface-card": "#ffffff",
45
- "--surface-card-muted": "#f8fafc",
46
- "--surface-secondary": "#f1f5f9",
47
- "--surface-tertiary": "#e2e8f0",
48
- "--text-primary": "#0f172a",
49
- "--text-secondary": "#334155",
50
- "--text-muted": "#64748b",
51
- "--text-subtle": "#94a3b8",
52
- "--text-disabled": "#cbd5e1",
53
- "--border-color": "#e2e8f0",
54
- "--border-color-light": "#f1f5f9",
55
- "--border-color-focus": "#bfdbfe",
56
- "--shadow-soft": "0 2rpx 8rpx rgba(15, 23, 42, 0.04)",
57
- "--shadow-card": "0 4rpx 16rpx rgba(15, 23, 42, 0.06)",
58
- };
59
-
60
- /** 深色模式语义变量 */
61
- const DARK_VARS: Record<string, string> = {
62
- "--bg-page": "#0b1020",
63
- "--bg-elevated": "#111827",
64
- "--surface-card": "#1e293b",
65
- "--surface-card-muted": "#273549",
66
- "--surface-secondary": "#273549",
67
- "--surface-tertiary": "#334155",
68
- "--text-primary": "#f8fafc",
69
- "--text-secondary": "#cbd5e1",
70
- "--text-muted": "#94a3b8",
71
- "--text-subtle": "#64748b",
72
- "--text-disabled": "#475569",
73
- "--border-color": "#334155",
74
- "--border-color-light": "#1e293b",
75
- "--border-color-focus": "#3b82f6",
76
- "--shadow-soft": "0 2rpx 8rpx rgba(0, 0, 0, 0.3)",
77
- "--shadow-card": "0 4rpx 16rpx rgba(0, 0, 0, 0.35)",
78
- };
79
-
80
- /** 不同外观模式的 CSS 变量映射 */
81
- export const APPEARANCE_VAR_MAP: Record<AppearanceMode, Record<string, string>> = {
82
- light: LIGHT_VARS,
83
- dark: DARK_VARS,
84
- };
85
-
86
- /**
87
- * 读取用户设置的外观配置(light / dark / auto)。
88
- * 默认返回 'auto'。
89
- * @returns 设定的外观模式类型
90
- */
91
- export function getCurrentAppearance(): Appearance {
92
- try {
93
- const v = uni.getStorageSync(APPEARANCE_KEY);
94
- if (v === "light" || v === "dark" || v === "auto") return v;
95
- } catch {}
96
- return "auto";
97
- }
98
-
99
- /**
100
- * 将 auto 设置或用户选择解析为具体的 light/dark 实际模式。
101
- * 当为 auto 时,将读取系统当前的配色偏好。
102
- * @param appearance 输入的外观配置
103
- * @returns 解析后最终生效的外观模式 (light 或 dark)
104
- */
105
- export function resolveAppearance(appearance: Appearance): AppearanceMode {
106
- if (appearance === "light" || appearance === "dark") return appearance;
107
- try {
108
- const info = uni.getSystemInfoSync();
109
- if ((info as { theme?: string }).theme === "dark") return "dark";
110
- } catch {}
111
- return "light";
112
- }
113
-
114
- /**
115
- * 获取当前实际生效的外观模式 (light 或 dark)。
116
- * 自动根据用户偏好及系统主题进行判定。
117
- * @returns 实际生效的外观模式
118
- */
119
- export function getCurrentAppearanceMode(): AppearanceMode {
120
- return resolveAppearance(getCurrentAppearance());
121
- }
122
-
123
- /**
124
- * 获取当前外观对应的 CSS 变量映射表。
125
- * @returns 包含各 CSS 变量名与对应颜色值键值对
126
- */
127
- export function getCurrentAppearanceVars(): Record<string, string> {
128
- return APPEARANCE_VAR_MAP[getCurrentAppearanceMode()];
129
- }
@@ -1,95 +0,0 @@
1
- /**
2
- * 字体大小缩放档位类型。
3
- * 从较小到特大,共 7 个档位。
4
- */
5
- export type FontScale = "small" | "compact" | "normal" | "medium" | "large" | "xlarge" | "xxlarge";
6
-
7
- /**
8
- * 字体大小配置预设接口。
9
- */
10
- export interface FontPreset {
11
- /** 档位名称 */
12
- label: string;
13
- /** 该档位对应的全部 CSS 字号变量及对应尺寸 */
14
- vars: Record<string, string>;
15
- }
16
-
17
- /** 存储字体字号档位设置的 LocalStorage 键名 */
18
- export const FONT_SCALE_KEY = "hlw_font_scale";
19
-
20
- /**
21
- * 字体档位对应的尺寸变量预设表。
22
- * 每个档位定义了从超小 (--font-xs) 到超大 (--font-xl) 的响应式字号。
23
- */
24
- export const FONT_PRESETS: Record<FontScale, FontPreset> = {
25
- small: {
26
- label: "较小",
27
- vars: {
28
- "--font-xs": "16rpx", "--font-sm": "20rpx", "--font-base": "24rpx",
29
- "--font-md": "28rpx", "--font-lg": "32rpx", "--font-xl": "36rpx",
30
- },
31
- },
32
- compact: {
33
- label: "略小",
34
- vars: {
35
- "--font-xs": "18rpx", "--font-sm": "22rpx", "--font-base": "26rpx",
36
- "--font-md": "30rpx", "--font-lg": "34rpx", "--font-xl": "38rpx",
37
- },
38
- },
39
- normal: {
40
- label: "标准",
41
- vars: {
42
- "--font-xs": "20rpx", "--font-sm": "24rpx", "--font-base": "28rpx",
43
- "--font-md": "32rpx", "--font-lg": "36rpx", "--font-xl": "40rpx",
44
- },
45
- },
46
- medium: {
47
- label: "适中",
48
- vars: {
49
- "--font-xs": "22rpx", "--font-sm": "28rpx", "--font-base": "32rpx",
50
- "--font-md": "36rpx", "--font-lg": "42rpx", "--font-xl": "46rpx",
51
- },
52
- },
53
- large: {
54
- label: "较大",
55
- vars: {
56
- "--font-xs": "24rpx", "--font-sm": "30rpx", "--font-base": "34rpx",
57
- "--font-md": "40rpx", "--font-lg": "46rpx", "--font-xl": "52rpx",
58
- },
59
- },
60
- xlarge: {
61
- label: "超大",
62
- vars: {
63
- "--font-xs": "28rpx", "--font-sm": "36rpx", "--font-base": "42rpx",
64
- "--font-md": "48rpx", "--font-lg": "56rpx", "--font-xl": "64rpx",
65
- },
66
- },
67
- xxlarge: {
68
- label: "特大",
69
- vars: {
70
- "--font-xs": "32rpx", "--font-sm": "42rpx", "--font-base": "48rpx",
71
- "--font-md": "56rpx", "--font-lg": "64rpx", "--font-xl": "72rpx",
72
- },
73
- },
74
- };
75
-
76
- /**
77
- * 获取当前用户配置的字体字号缩放档位。
78
- * 默认返回 'normal'。
79
- * @returns 字体档位
80
- */
81
- export function getCurrentFontScale(): FontScale {
82
- try {
83
- const v = uni.getStorageSync(FONT_SCALE_KEY);
84
- if (v === "small" || v === "compact" || v === "medium" || v === "large" || v === "xlarge" || v === "xxlarge") return v;
85
- } catch {}
86
- return "normal";
87
- }
88
-
89
- /**
90
- * 获取当前字体字号档位所映射的 CSS 变量表。
91
- * @returns CSS 变量名与具体字号的键值对
92
- */
93
- export function getCurrentFontVars(): Record<string, string> {
94
- return FONT_PRESETS[getCurrentFontScale()].vars;
95
- }
@@ -1,89 +0,0 @@
1
- import { computed } from "vue";
2
- import { useThemeStore } from "../../stores/theme";
3
-
4
- import { getCurrentFontVars } from "./font";
5
- import { getCurrentThemeVars } from "./palette";
6
-
7
- /**
8
- * @deprecated 历史事件名;现已改用 pinia store 响应式驱动,emit 不再有效。
9
- * 保留 export 仅为不破坏外部 import(不影响功能)。
10
- */
11
- export const THEME_CHANGE_EVENT = "hlw:theme-change";
12
-
13
- /**
14
- * 构建仅注入运行时配置变量(字号档位、主题色)的行内样式字符串。
15
- *
16
- * 页面背景、卡片色、文字色、边框色等业务视觉变量统一由项目全局 CSS
17
- * (static/css/style.scss)控制,避免 page-meta 运行时样式覆盖业务侧配置。
18
- *
19
- * @returns 扁平化后的 CSS 变量样式属性字符串
20
- */
21
- export function buildThemeStyle(): string {
22
- return varsToStyle({
23
- ...getCurrentFontVars(),
24
- ...getCurrentThemeVars(),
25
- });
26
- }
27
-
28
- /**
29
- * 将 CSS 键值对对象平铺拼接为 CSS inline style 格式字符串。
30
- */
31
- function varsToStyle(vars: Record<string, string>): string {
32
- return Object.entries(vars).map(([key, value]) => `${key}:${value}`).join(";") + ";";
33
- }
34
-
35
- /**
36
- * 获取及监听主题与字号变化,返回可注入到 `<page-meta :page-style="themePageStyle">` 的计算属性。
37
- *
38
- * 实现基于 pinia store 响应式:当 store 中的字号档位或主题色发生改变时,
39
- * 触发计算属性重算并由 Vue 自动更新至 `<page-meta>` 的 setData 接口。
40
- *
41
- * @returns 包含 `themePageStyle` 计算属性的对象
42
- *
43
- * @example
44
- * ```vue
45
- * <template>
46
- * <page-meta :page-style="themePageStyle" />
47
- * <view class="container">...</view>
48
- * </template>
49
- *
50
- * <script setup>
51
- * const { themePageStyle } = useThemePageStyle();
52
- * </script>
53
- * ```
54
- */
55
- export function useThemePageStyle() {
56
- const store = useThemeStore();
57
- const themePageStyle = computed(() => {
58
- // 显式 track 两个响应字段,触发 computed 重算
59
- // setScale / setTheme 内部已先 set storage 再改 ref,
60
- // 所以 buildThemeStyle 从 storage 读到的一定是最新值
61
- void store.scale;
62
- void store.primaryColor;
63
- return buildThemeStyle();
64
- });
65
- return { themePageStyle };
66
- }
67
-
68
- export type { FontScale, FontPreset } from "./font";
69
- export { FONT_SCALE_KEY, FONT_PRESETS, getCurrentFontScale, getCurrentFontVars } from "./font";
70
- export type { ThemeColor } from "./palette";
71
- export {
72
- THEME_COLOR_KEY,
73
- THEME_SEMANTIC_COLORS,
74
- DEFAULT_THEMES,
75
- getCurrentThemeColor,
76
- getCurrentThemeVars,
77
- } from "./palette";
78
- export type { Appearance, AppearanceMode, AppearancePreset } from "./appearance";
79
- export {
80
- APPEARANCE_KEY,
81
- APPEARANCE_PRESETS,
82
- APPEARANCE_VAR_MAP,
83
- getCurrentAppearance,
84
- getCurrentAppearanceMode,
85
- getCurrentAppearanceVars,
86
- resolveAppearance,
87
- } from "./appearance";
88
- export type { TypographyRole } from "./typography";
89
- export { TYPOGRAPHY_ROLES, getCurrentTypographyVars } from "./typography";
@@ -1,117 +0,0 @@
1
- /**
2
- * 主题颜色配置项接口。
3
- */
4
- export interface ThemeColor {
5
- /** 颜色名称描述 */
6
- label: string;
7
- /** 主题色十六进制值 (Hex Color) */
8
- value: string;
9
- }
10
-
11
- /** 存储当前主题色设置的 LocalStorage 键名 */
12
- export const THEME_COLOR_KEY = "hlw_theme_color";
13
-
14
- /**
15
- * 预设的语义化颜色。
16
- * 包含成功、警告、错误和提示的默认色彩配置。
17
- */
18
- export const THEME_SEMANTIC_COLORS = {
19
- success: "#10b981",
20
- warning: "#f59e0b",
21
- error: "#ef4444",
22
- info: "#64748b",
23
- } as const;
24
-
25
- /**
26
- * 系统预设的默认主题色列表。
27
- */
28
- export const DEFAULT_THEMES: ThemeColor[] = [
29
- { label: "翡翠绿", value: "#10b981" },
30
- { label: "活力橙", value: "#f97316" },
31
- { label: "默认蓝", value: "#3b82f6" },
32
- { label: "玫瑰粉", value: "#f43f5e" },
33
- { label: "紫罗兰", value: "#8b5cf6" },
34
- { label: "青石灰", value: "#64748b" },
35
- ];
36
-
37
- /**
38
- * 读取当前配置的主题色 Hex 字符串。
39
- * 默认使用 `DEFAULT_THEMES` 数组中首位颜色的值。
40
- * @returns 十六进制颜色字符串
41
- */
42
- export function getCurrentThemeColor(): string {
43
- try {
44
- const v = uni.getStorageSync(THEME_COLOR_KEY);
45
- if (v && typeof v === "string") return v;
46
- } catch {}
47
- return DEFAULT_THEMES[0].value;
48
- }
49
-
50
- /**
51
- * 获取当前主题色所对应的完整 CSS 颜色语义变量映射表。
52
- * 会基于当前主题色,自动计算衍生出对应的亮色调、暗色调以及各状态色语义变量。
53
- * @returns 包含各 CSS 变量名与对应颜色值键值对
54
- */
55
- export function getCurrentThemeVars(): Record<string, string> {
56
- const color = getCurrentThemeColor();
57
- return {
58
- "--primary-color": color,
59
- "--primary-light": hexToRgba(color, 0.12),
60
- "--primary-dark": darkenHex(color),
61
- "--primary-success": THEME_SEMANTIC_COLORS.success,
62
- "--primary-success-light": hexToRgba(THEME_SEMANTIC_COLORS.success, 0.12),
63
- "--primary-success-dark": darkenHex(THEME_SEMANTIC_COLORS.success),
64
- "--primary-warning": THEME_SEMANTIC_COLORS.warning,
65
- "--primary-warning-light": hexToRgba(THEME_SEMANTIC_COLORS.warning, 0.12),
66
- "--primary-warning-dark": darkenHex(THEME_SEMANTIC_COLORS.warning),
67
- "--primary-error": THEME_SEMANTIC_COLORS.error,
68
- "--primary-error-light": hexToRgba(THEME_SEMANTIC_COLORS.error, 0.12),
69
- "--primary-error-dark": darkenHex(THEME_SEMANTIC_COLORS.error),
70
- "--primary-info": THEME_SEMANTIC_COLORS.info,
71
- "--primary-info-light": hexToRgba(THEME_SEMANTIC_COLORS.info, 0.12),
72
- "--primary-info-dark": darkenHex(THEME_SEMANTIC_COLORS.info),
73
- "--success-color": THEME_SEMANTIC_COLORS.success,
74
- "--success-light": hexToRgba(THEME_SEMANTIC_COLORS.success, 0.12),
75
- "--success-dark": darkenHex(THEME_SEMANTIC_COLORS.success),
76
- "--warning-color": THEME_SEMANTIC_COLORS.warning,
77
- "--warning-light": hexToRgba(THEME_SEMANTIC_COLORS.warning, 0.12),
78
- "--warning-dark": darkenHex(THEME_SEMANTIC_COLORS.warning),
79
- "--error-color": THEME_SEMANTIC_COLORS.error,
80
- "--error-light": hexToRgba(THEME_SEMANTIC_COLORS.error, 0.12),
81
- "--error-dark": darkenHex(THEME_SEMANTIC_COLORS.error),
82
- "--info-color": THEME_SEMANTIC_COLORS.info,
83
- "--info-light": hexToRgba(THEME_SEMANTIC_COLORS.info, 0.12),
84
- "--info-dark": darkenHex(THEME_SEMANTIC_COLORS.info),
85
- };
86
- }
87
-
88
- const HEX_RE = /^#[0-9a-fA-F]{6}$/;
89
-
90
- /**
91
- * 将 6 位 hex 颜色字符串转换为 RGB 三原色元组。
92
- */
93
- function parseHex(hex: string): [number, number, number] {
94
- if (!HEX_RE.test(hex)) throw new Error(`Invalid hex color: ${hex}`);
95
- return [
96
- parseInt(hex.slice(1, 3), 16),
97
- parseInt(hex.slice(3, 5), 16),
98
- parseInt(hex.slice(5, 7), 16),
99
- ];
100
- }
101
-
102
- /**
103
- * 转换 Hex 颜色为 rgba 格式字符串。
104
- */
105
- function hexToRgba(hex: string, alpha: number): string {
106
- const [r, g, b] = parseHex(hex);
107
- return `rgba(${r},${g},${b},${alpha})`;
108
- }
109
-
110
- /**
111
- * 将 Hex 颜色调暗。
112
- */
113
- function darkenHex(hex: string, amount = 0.15): string {
114
- const [r, g, b] = parseHex(hex);
115
- const darken = (value: number) => Math.max(0, Math.round(value * (1 - amount)));
116
- return `#${darken(r).toString(16).padStart(2, "0")}${darken(g).toString(16).padStart(2, "0")}${darken(b).toString(16).padStart(2, "0")}`;
117
- }
@@ -1,90 +0,0 @@
1
- /**
2
- * 语义化排版 token — 把"字号 + 字重 + 行高 + 颜色"打包成角色,统一各页面文字样式。
3
- *
4
- * 使用指南:
5
- * - 页面样式里用 `var(--text-title-size)` 等变量代替硬编码,自动响应字体档位/外观模式切换
6
- * - 或通过全局 utility class(见 qz2 项目的 static/css/style.scss)直接加 class
7
- *
8
- * 6 个语义角色:
9
- * - title-lg: 页面大标题 / hero
10
- * - title: 卡片 / 分区主标题
11
- * - subtitle: 次级标题
12
- * - body: 正文
13
- * - desc: 卡片描述 / 副文
14
- * - caption: 角标 / 底部小字 / 时间戳
15
- */
16
-
17
- /**
18
- * 语义排版角色配置项接口。
19
- */
20
- export interface TypographyRole {
21
- /** 对应 `--text-{role}-size` 的值(通常引用字号档位 token) */
22
- size: string;
23
- /** 对应 `--text-{role}-weight` */
24
- weight: string;
25
- /** 对应 `--text-{role}-line-height` */
26
- lineHeight: string;
27
- /** 对应 `--text-{role}-color`(通常引用文字色 token) */
28
- color: string;
29
- }
30
-
31
- /**
32
- * 全局预设排版角色的参数配置表。
33
- */
34
- export const TYPOGRAPHY_ROLES: Record<string, TypographyRole> = {
35
- "title-lg": {
36
- size: "var(--font-xl)",
37
- weight: "600",
38
- lineHeight: "1.2",
39
- color: "var(--text-primary)",
40
- },
41
- "title": {
42
- size: "var(--font-md)",
43
- weight: "500",
44
- lineHeight: "1.3",
45
- color: "var(--text-primary)",
46
- },
47
- "subtitle": {
48
- size: "var(--font-base)",
49
- weight: "500",
50
- lineHeight: "1.3",
51
- color: "var(--text-secondary)",
52
- },
53
- "body": {
54
- size: "var(--font-base)",
55
- weight: "400",
56
- lineHeight: "1.5",
57
- color: "var(--text-secondary)",
58
- },
59
- "desc": {
60
- size: "var(--font-sm)",
61
- weight: "400",
62
- lineHeight: "1.4",
63
- color: "var(--text-subtle)",
64
- },
65
- "caption": {
66
- size: "var(--font-xs)",
67
- weight: "500",
68
- lineHeight: "1.3",
69
- color: "var(--text-muted)",
70
- },
71
- };
72
-
73
- /**
74
- * 展开成 CSS 变量平铺 map,用于 buildThemeStyle 注入 page 元素。
75
- *
76
- * 每个角色产出 4 个变量:
77
- * --text-${role}-size / -weight / -line-height / -color
78
- *
79
- * @returns 包含全部排版 CSS 变量键值对的对象
80
- */
81
- export function getCurrentTypographyVars(): Record<string, string> {
82
- const vars: Record<string, string> = {};
83
- for (const [role, cfg] of Object.entries(TYPOGRAPHY_ROLES)) {
84
- vars[`--text-${role}-size`] = cfg.size;
85
- vars[`--text-${role}-weight`] = cfg.weight;
86
- vars[`--text-${role}-line-height`] = cfg.lineHeight;
87
- vars[`--text-${role}-color`] = cfg.color;
88
- }
89
- return vars;
90
- }
@@ -1,114 +0,0 @@
1
- /**
2
- * Theme Store — 全局主题设置(字体档位 + 主题色)
3
- * 持久化到 storage,变更时广播 THEME_CHANGE_EVENT 通知所有页面实时刷新
4
- */
5
- import { defineStore } from "pinia";
6
- import { ref, computed } from "vue";
7
- import {
8
- FONT_PRESETS,
9
- FONT_SCALE_KEY,
10
- DEFAULT_THEMES,
11
- THEME_COLOR_KEY,
12
- THEME_CHANGE_EVENT,
13
- APPEARANCE_KEY,
14
- APPEARANCE_PRESETS,
15
- resolveAppearance,
16
- } from "../composables/theme";
17
- import type { FontScale, ThemeColor, Appearance, AppearanceMode } from "../composables/theme";
18
-
19
- /**
20
- * 统一管理全局主题、字体缩放和外观模式的 Pinia Store。
21
- */
22
- export const useThemeStore = defineStore(
23
- "theme",
24
- () => {
25
- // ─── 字体档位 ────────────────────────────
26
- /** 当前生效的字体大小档位 */
27
- const scale = ref<FontScale>("normal");
28
-
29
- /**
30
- * 改变并保存当前的字体大小档位。
31
- *
32
- * @param s 目标字体大小档位
33
- */
34
- function setScale(s: FontScale) {
35
- scale.value = s;
36
- uni.setStorageSync(FONT_SCALE_KEY, s);
37
- uni.$emit(THEME_CHANGE_EVENT);
38
- }
39
-
40
- /** 供界面渲染选择的字体档位选项列表 */
41
- const fontOptions = (Object.keys(FONT_PRESETS) as FontScale[]).map((key) => ({
42
- value: key,
43
- label: FONT_PRESETS[key].label,
44
- }));
45
-
46
- // ─── 主题色 ─────────────────────────────
47
- /** 当前生效的主题色十六进制值 (Hex Color) */
48
- const primaryColor = ref(DEFAULT_THEMES[0].value);
49
-
50
- /** 系统内置的预设主题颜色列表 */
51
- const themes = DEFAULT_THEMES;
52
-
53
- /** 当前激活的主题色相关信息 */
54
- const activeTheme = computed<ThemeColor>(() =>
55
- themes.find((t: ThemeColor) => t.value === primaryColor.value)
56
- ?? { label: "自定义", value: primaryColor.value },
57
- );
58
-
59
- /**
60
- * 更改并保存全局主题色。
61
- *
62
- * @param color 颜色 Hex 字符串
63
- */
64
- function setTheme(color: string) {
65
- primaryColor.value = color;
66
- uni.setStorageSync(THEME_COLOR_KEY, color);
67
- uni.$emit(THEME_CHANGE_EVENT);
68
- }
69
-
70
- // ─── 外观模式(light / dark / auto) ─────────
71
- /** 当前设置的外观显示模式:浅色、深色或自动跟随系统 */
72
- const appearance = ref<Appearance>("auto");
73
-
74
- /** 可供选择的外观预设配置列表 */
75
- const appearanceOptions = APPEARANCE_PRESETS;
76
-
77
- /** 当前最终解析的实际生效模式(auto 会依据系统检测解析为 light 或 dark) */
78
- const appearanceMode = computed<AppearanceMode>(() => resolveAppearance(appearance.value));
79
-
80
- /** 是否正处于深色模式,可供业务组件在 template 中直接用作条件渲染 */
81
- const isDark = computed(() => appearanceMode.value === "dark");
82
-
83
- /**
84
- * 改变外观设置模式。
85
- *
86
- * @param a 目标模式
87
- */
88
- function setAppearance(a: Appearance) {
89
- appearance.value = a;
90
- uni.setStorageSync(APPEARANCE_KEY, a);
91
- uni.$emit(THEME_CHANGE_EVENT);
92
- }
93
-
94
- return {
95
- // 字体
96
- scale,
97
- fontOptions,
98
- setScale,
99
- // 主题色
100
- primaryColor,
101
- themes,
102
- activeTheme,
103
- setTheme,
104
- // 外观模式
105
- appearance,
106
- appearanceOptions,
107
- appearanceMode,
108
- isDark,
109
- setAppearance,
110
- };
111
- },
112
- { unistorage: true },
113
- );
114
-