Files
wx_pbzc/utils/theme.js
T

128 lines
3.4 KiB
JavaScript

const THEMES = [
{
id: 'orange',
name: '活力橙',
primary: '#FF6B35',
primaryLight: '#FF8C5A',
primaryBg: '#FFF3ED',
primaryRgb: '255,107,53'
},
{
id: 'blue',
name: '深海蓝',
primary: '#2979FF',
primaryLight: '#5C9CFF',
primaryBg: '#EDF4FF',
primaryRgb: '41,121,255'
},
{
id: 'green',
name: '森林绿',
primary: '#43A047',
primaryLight: '#6EC072',
primaryBg: '#EDF7EE',
primaryRgb: '67,160,71'
},
{
id: 'purple',
name: '优雅紫',
primary: '#7E57C2',
primaryLight: '#A08AD6',
primaryBg: '#F5F0FA',
primaryRgb: '126,87,194'
},
{
id: 'pink',
name: '樱花粉',
primary: '#EC407A',
primaryLight: '#F06A9A',
primaryBg: '#FDEEF3',
primaryRgb: '236,64,122'
}
]
const BASE_VARS = '--text:#333;--text-secondary:#999;--bg:#F5F5F5;--card-bg:#FFFFFF;--border:#EEE;--success:#4CAF50;--primary:#FF6B35;--primary-light:#FF8C5A;--primary-bg:#FFF3ED;--primary-rgb:255,107,53;'
const DEFAULT_THEME_ID = 'orange'
const THEME_KEY = 'app_theme'
let _navColorTimer = null
const getThemeById = (id) => THEMES.find(t => t.id === id) || THEMES[0]
const getCurrentTheme = () => {
let id = wx.getStorageSync(THEME_KEY)
if (!id) {
// fallback: try recovering from user_settings backup
try {
const s = wx.getStorageSync('user_settings')
if (s && s.themeId) id = s.themeId
} catch (e) {}
}
return getThemeById(id || DEFAULT_THEME_ID)
}
const _setNavBarColor = (primary) => {
if (_navColorTimer) clearTimeout(_navColorTimer)
_navColorTimer = setTimeout(() => {
try {
wx.setNavigationBarColor({
frontColor: '#ffffff',
backgroundColor: primary
})
} catch (e) {}
}, 50)
}
const setTheme = (id) => {
try {
wx.setStorageSync(THEME_KEY, id)
// redundant backup in user_settings
try {
const s = wx.getStorageSync('user_settings') || {}
s.themeId = id
wx.setStorageSync('user_settings', s)
} catch (e) {}
} catch (e) {
console.error('Failed to save theme:', e)
}
const theme = getThemeById(id)
_setNavBarColor(theme.primary)
const app = getApp()
if (app && app.globalData) app.globalData.theme = theme
// sync to cloud (lazy-require to avoid circular deps)
try { require('./cloud').pushAll() } catch (e) {}
return theme
}
const getThemeStyle = (theme, isDark = false) => {
const t = theme || getCurrentTheme()
const baseVars = isDark
? '--text:#E5E5E7;--text-secondary:#98989F;--bg:#0F0F12;--card-bg:#1C1C1E;--border:#2C2C2E;--success:#30D158;'
: '--text:#333333;--text-secondary:#999999;--bg:#F5F5F5;--card-bg:#FFFFFF;--border:#EEEEEE;--success:#4CAF50;'
return `${baseVars}--primary:${t.primary};--primary-light:${t.primaryLight};--primary-bg:${t.primaryBg};--primary-rgb:${t.primaryRgb};`
}
const applyThemeToPage = (self) => {
const darkMod = require('./darkMode')
const theme = getCurrentTheme()
const isDark = darkMod.getInitialDarkMode()
const nextStyle = getThemeStyle(theme, isDark)
// Skip setData if nothing changed — avoid unnecessary re-renders on every onShow
if (self.data && self.data.themeStyle === nextStyle) {
return
}
self.setData({ theme, themeStyle: nextStyle, isDark })
_setNavBarColor(theme.primary)
}
module.exports = {
THEMES,
DEFAULT_THEME_ID,
BASE_VARS,
getThemeById,
getCurrentTheme,
setTheme,
applyThemeToPage,
getThemeStyle
}