From c01a0261493148237ee1c02125d9abada589abbb Mon Sep 17 00:00:00 2001 From: ZF sun <34314687@qq.com> Date: Tue, 16 Jun 2026 17:34:09 +0800 Subject: [PATCH] =?UTF-8?q?perf(store,=20globalConfig):=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E5=A4=9A=E8=AF=AD=E8=A8=80=E5=88=B7=E6=96=B0=E5=92=8C?= =?UTF-8?q?=E4=B8=BB=E9=A2=98=E9=A2=9C=E8=89=B2=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 修改全局配置的多语言刷新逻辑,新增参数控制全量刷新时机 2. 为主题颜色生成添加缓存,避免重复遍历主题对象计算CSS变量 3. 从本地存储初始化主题配置,持久化主题状态 --- common/js/golbalConfig.js | 5 +++-- store/index.js | 20 +++++++++++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/common/js/golbalConfig.js b/common/js/golbalConfig.js index 3ed62d7..02b4b18 100644 --- a/common/js/golbalConfig.js +++ b/common/js/golbalConfig.js @@ -34,8 +34,9 @@ const _callUtil = (vm, method, ...args) => { export default { onLoad() { }, onShow() { - // 刷新多语言 - this.$langConfig.refresh(); + // 刷新多语言(仅刷新标题,不触发全页面 $forceUpdate) + // 全量刷新仅在语言包真正变化时由 lang.js emitUpdated 触发 + this.$langConfig.refresh(false); // TabBar 根页面时隐藏系统导航栏的返回按钮 // (uni-app 的系统导航栏在通过 navigateTo 跳转到 tab 页时可能残留返回键) diff --git a/store/index.js b/store/index.js index e12b957..333665b 100644 --- a/store/index.js +++ b/store/index.js @@ -9,6 +9,11 @@ import util from '../common/js/util.js' import ThemeOverride from '../common/js/theme-override.js' import { sanitizeFlatMap, mergeThemeBuckets } from '../common/js/merge-theme.js' +// themeColor 结果缓存(避免每次都全量遍历 theme 对象) +// key: JSON.stringify(theme) + tabBarHeight, value: CSS变量字符串 +let _themeColorCache = null; +let _themeColorCacheKey = ''; + const store = new Vuex.Store({ state: { token: null, @@ -16,8 +21,8 @@ const store = new Vuex.Store({ memberInfo: null, tabBarList: null, siteState: 1, - themeStyle: '', - themeColor: '', + themeStyle: (function() { try { return uni.getStorageSync('themeStyle') || ''; } catch(e) { return ''; } })(), + themeColor: (function() { try { return uni.getStorageSync('themeColor') || ''; } catch(e) { return ''; } })(), addonIsExist: { bundling: 0, coupon: 0, @@ -420,11 +425,18 @@ const store = new Vuex.Store({ this.commit('setCartIds', ids); }, - // 生成主题颜色CSS变量 + // 生成主题颜色CSS变量(带缓存) themeColorSet() { let theme = this.state.themeStyle; if (!theme?.main_color || !theme?.aux_color) return; try { + const cacheKey = (typeof theme === 'object' ? JSON.stringify(theme) : String(theme)) + '|' + (this.state.tabBarHeight || ''); + if (_themeColorCache && _themeColorCacheKey === cacheKey) { + if (this.state.themeColor !== _themeColorCache) { + this.commit('setThemeColor', _themeColorCache); + } + return; + } let themeColor = `--base-color:${theme.main_color};--base-help-color:${theme.aux_color};`; if (this.state.tabBarHeight != '56px') themeColor += `--tab-bar-height:${this.state.tabBarHeight};` Object.keys(theme).forEach(key => { @@ -441,6 +453,8 @@ const store = new Vuex.Store({ let color = util.colourBlend(theme.main_color, '#ffffff', (i / 10)); themeColor += `--base-color-light-${i}:${color};`; } + _themeColorCache = themeColor; + _themeColorCacheKey = cacheKey; this.commit('setThemeColor', themeColor); } catch (e) { console.error('设置主题颜色失败', e);