Files
lilishop-uniapp/utils/theme.js
Yer11214 349ffa4f34 refactor: 重构多个组件以支持 Vue 3 语法和功能
- 将多个组件转换为 `<script setup>` 语法,提升可读性和性能
- 优化状态管理和事件处理逻辑,简化代码结构
- 更新样式和布局以适应新组件结构
- 添加新功能和修复已知问题,提升用户体验
2026-07-08 18:37:11 +08:00

82 lines
2.4 KiB
JavaScript

import config from "@/config/config";
export const DEFAULT_THEME = {
mainColor: config.mainColor,
lightColor: config.lightColor,
aiderLightColor: config.aiderLightColor,
};
const STORAGE_KEY = "theme_setting";
const EXPIRATION_KEY = "theme_setting_expiration_time";
export function normalizeTheme(data = {}) {
return {
mainColor: data.themeColor || DEFAULT_THEME.mainColor,
lightColor: data.lightColor || DEFAULT_THEME.lightColor,
aiderLightColor: data.aiderLightColor || DEFAULT_THEME.aiderLightColor,
};
}
export function cacheTheme(theme) {
const expirationTime = new Date().setHours(new Date().getHours() + 1);
uni.setStorageSync(EXPIRATION_KEY, expirationTime);
uni.setStorageSync(STORAGE_KEY, theme);
}
export function loadCachedTheme() {
const expirationTime = uni.getStorageSync(EXPIRATION_KEY);
const cacheValid =
expirationTime && new Date() <= new Date(Number(expirationTime));
if (!cacheValid) return null;
try {
return uni.getStorageSync(STORAGE_KEY) || null;
} catch {
return null;
}
}
export function applyTabBarStyle(mainColor) {
if (!mainColor) return;
try {
uni.setTabBarStyle({
selectedColor: mainColor,
});
} catch (e) {
// 非 tabBar 页面可能报错,忽略
}
}
function hexToRgba(hex, alpha) {
if (!hex) return `rgba(255, 60, 42, ${alpha})`;
const normalized = String(hex).replace("#", "");
const full =
normalized.length === 3
? normalized
.split("")
.map((c) => c + c)
.join("")
: normalized;
if (full.length !== 6) return `rgba(255, 60, 42, ${alpha})`;
const r = parseInt(full.slice(0, 2), 16);
const g = parseInt(full.slice(2, 4), 16);
const b = parseInt(full.slice(4, 6), 16);
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
}
export function getThemeStyle(theme = DEFAULT_THEME) {
const safeTheme = theme || DEFAULT_THEME;
const primary = safeTheme.mainColor || DEFAULT_THEME.mainColor;
const light = safeTheme.lightColor || DEFAULT_THEME.lightColor;
return {
"--theme-primary": primary,
"--theme-light": light,
"--theme-aider": safeTheme.aiderLightColor || DEFAULT_THEME.aiderLightColor,
"--theme-primary-10": hexToRgba(primary, 0.1),
"--theme-primary-20": hexToRgba(primary, 0.2),
"--theme-primary-30": hexToRgba(primary, 0.3),
"--theme-primary-70": hexToRgba(primary, 0.7),
"--theme-light-10": hexToRgba(light, 0.1),
"--theme-light-80": hexToRgba(light, 0.8),
};
}