From 54c39259a7e96981299fec540e52d09bac97ce0a Mon Sep 17 00:00:00 2001 From: ZF sun <34314687@qq.com> Date: Mon, 2 Feb 2026 18:04:59 +0800 Subject: [PATCH] =?UTF-8?q?chore:=E4=BB=A3=E7=A0=81=E5=90=88=E5=B9=B6?= =?UTF-8?q?=EF=BC=8C=E6=95=B4=E7=90=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/js/golbalConfig.js | 2 +- common/js/lang.js | 320 +++++++++++-------- components/chat-message/chat-message.vue | 4 +- components/hover-nav/hover-nav.vue | 88 +++--- pages.json | 30 +- pages/index/index.vue | 387 +++++++++++------------ project.config.json | 26 +- store/index.js | 4 +- 8 files changed, 469 insertions(+), 392 deletions(-) diff --git a/common/js/golbalConfig.js b/common/js/golbalConfig.js index 70c050a..ea3de41 100644 --- a/common/js/golbalConfig.js +++ b/common/js/golbalConfig.js @@ -7,7 +7,7 @@ export default { computed: { // 是否是英文环境 isEnEnv() { - return uni.getStorageSync('lang') === 'en-us'; + return this.$langConfig.getCurrentLocale() === 'en-us'; }, themeStyle() { return this.$store.state.themeStyle; diff --git a/common/js/lang.js b/common/js/lang.js index 613012d..ab15bb1 100644 --- a/common/js/lang.js +++ b/common/js/lang.js @@ -1,161 +1,215 @@ import { langConfig } from './config-external.js'; + // 缓存已加载的语言包 const loadedLangPacks = {}; // 处理页面目录映射 function processRoutePath(route) { - let routeParts = route.split("/"); + let routeParts = route.split("/"); - // ---- 处理页面目录映射 分包造成的,需要根据实际目录结构进行映射---- - if (routeParts[0] === 'pages_tool') { - routeParts = [routeParts[1], ...routeParts.slice(2)]; - } else if (routeParts[0] === 'pages_goods') { - routeParts[0] = 'goods'; - } else if (routeParts[0] === 'pages_member') { - routeParts[0] = 'member'; - } else if (routeParts[0] === 'pages_order') { - routeParts[0] = 'order'; - } else if (routeParts[0] === 'pages_promotion') { - const promotionModules = ['point', 'fenxiao', 'merch']; - if (routeParts[1] && promotionModules.includes(routeParts[1])) { - routeParts = [routeParts[1], ...routeParts.slice(2)]; - } - } - // ---- 处理页面目录映射 ---- - - if (routeParts[0] === 'pages') { - routeParts = routeParts.slice(1); - } - - return routeParts.join("/"); + // ---- 处理页面目录映射 分包造成的,需要根据实际目录结构进行映射---- + // 先处理特殊的分包路径 + if (routeParts[0] === 'pages_tool') { + // pages_tool 分包下的页面,直接使用子目录作为语言包路径 + routeParts = [routeParts[1], ...routeParts.slice(2)]; + } else if (routeParts[0] === 'pages_goods') { + // pages_goods 分包映射到 goods 目录 + routeParts[0] = 'goods'; + } else if (routeParts[0] === 'pages_member') { + // pages_member 分包映射到 member 目录 + routeParts[0] = 'member'; + } else if (routeParts[0] === 'pages_order') { + // pages_order 分包映射到 order 目录 + routeParts[0] = 'order'; + } else if (routeParts[0] === 'pages_promotion') { + // pages_promotion 分包特殊处理 + const promotionModules = ['point', 'fenxiao', 'merch']; + if (routeParts[1] && promotionModules.includes(routeParts[1])) { + routeParts = [routeParts[1], ...routeParts.slice(2)]; + } + } + // ---- 处理页面目录映射 ---- + + // 去掉pages目录,只保留子目录 + if (routeParts[0] === 'pages') { + routeParts = routeParts.slice(1); + } + + return routeParts.join("/"); } // 加载语言包(同步方式) function loadLangPackSync(lang, path) { - try { - if (loadedLangPacks[`${lang}_${path}`]) { - return loadedLangPacks[`${lang}_${path}`]; - } - const langData = require(`@/lang/${lang}/${path}.js`).lang; - loadedLangPacks[`${lang}_${path}`] = langData; - return langData; - } catch (error) { - console.error(`加载语言包 ${lang}/${path} 失败:`, error); - return {}; - } + try { + if (loadedLangPacks[`${lang}_${path}`]) { + return loadedLangPacks[`${lang}_${path}`]; + } + const langData = require(`@/lang/${lang}/${path}.js`).lang; + loadedLangPacks[`${lang}_${path}`] = langData; + return langData; + } catch (error) { + console.error(`加载语言包 ${lang}/${path} 失败:`, error); + return {}; + } } export default { - langList: langConfig.langList, - /** - * 解析多语言 - */ - lang(field) { - let _this = getCurrentPages()[getCurrentPages().length - 1]; - if (!_this) return; + langList: langConfig.langList, - const locale = uni.getStorageSync('lang') || "zh-cn"; + /** + * 获得当前本地语言 + * @returns + */ + getCurrentLocale() { + return uni.getStorageSync('lang') || "zh-cn"; + }, - let value = ''; - let langPath = ''; + /** + * * 解析多语言 + * @param {Object} field + */ + lang(field) { + let _page = getCurrentPages()[getCurrentPages().length - 1]; + if (!_page) return; - try { - var lang = loadLangPackSync(locale, 'common'); - - let route = _this.route; - langPath = processRoutePath(route); - - let currentPageLang = loadLangPackSync(locale, langPath); + const locale = this.getCurrentLocale(); // 获得当前本地语言 - let mergedLang = { ...lang, ...currentPageLang }; + let value = ''; // 存放解析后的语言值 + let langPath = ''; // 存放当前页面语言包路径 - var arr = field.split("."); - if (arr.length > 1) { - let temp = mergedLang; - let found = true; - for (let key of arr) { - if (temp[key] !== undefined) { - temp = temp[key]; - } else { - found = false; - break; - } - } - value = found ? temp : field; - } else { - value = mergedLang[field] !== undefined ? mergedLang[field] : field; - } - - } catch (e) { - console.error('解析语言包失败:', e, { langPath, field, locale }); - value = field; - } + try { + //公共语言包(同步加载) + var lang = loadLangPackSync(locale, 'common'); + + //当前页面语言包(同步加载) + let route = _page.route; + langPath = processRoutePath(route); + + // 加载当前页面语言包 + let currentPageLang = loadLangPackSync(locale, langPath); - if (arguments.length > 1) { - for (var i = 1; i < arguments.length; i++) { - value = value.replace("{" + (i - 1) + "}", arguments[i]); - } - } - if (value == undefined || (value == 'title' && field == 'title')) value = ''; + // 合并语言包 + let mergedLang = { ...lang, ...currentPageLang }; - if (field == value) { - console.warn(`警告: 字段 ${field} 在语言包 ${langPath} 中未找到对应值,使用默认值 ${field} 当前语言: ${locale}`); - } + // 解析字段 + var arr = field.split("."); + if (arr.length > 1) { + // 处理嵌套属性,如 common.currencySymbol + let temp = mergedLang; + let found = true; + for (let key of arr) { + if (temp[key] !== undefined) { + temp = temp[key]; + } else { + found = false; + break; + } + } + value = found ? temp : field; + } else { + value = mergedLang[field] !== undefined ? mergedLang[field] : field; + } + + } catch (e) { + console.error('解析语言包失败:', e, { langPath, field, locale }); + value = field; + } - return value; - }, - /** - * 切换语言 - */ - change(value, url = '/pages_tool/member/index') { - let _this = getCurrentPages()[getCurrentPages().length - 1]; - if (!_this) return; + if (arguments.length > 1) { + //有参数,需要替换 + for (var i = 1; i < arguments.length; i++) { + value = value.replace("{" + (i - 1) + "}", arguments[i]); + } + } + if (value == undefined || (value == 'title' && field == 'title')) value = ''; // field - uni.setStorageSync("lang", value); - const locale = uni.getStorageSync('lang') || "zh-cn"; + // 多语言调试,注释后可以关闭控制台输出 + if (field == value) { + console.warn(`警告: 字段 ${field} 在语言包 ${langPath} 中未找到对应值,使用默认值 ${field} 当前语言: ${locale}`); + } - // ✅ 关键修复:清空所有语言包缓存(不再保留任何旧缓存) - for (let key in loadedLangPacks) { - delete loadedLangPacks[key]; - } + return value; + }, + /** + * * 切换语言 + * @param {String} value 语言值 + * @param {String} url 切换后跳转的页面url + */ + change(value, url = '/pages_tool/member/index') { + let _page = getCurrentPages()[getCurrentPages().length - 1]; + if (!_page) return; - this.refresh(); + uni.setStorageSync("lang", value); + const locale = this.getCurrentLocale(); - if (url) { - uni.reLaunch({ url: url }); - } - }, - //刷新标题、tabbar - refresh() { - let _this = getCurrentPages()[getCurrentPages().length - 1]; - if (!_this) return; - const locale = uni.getStorageSync('lang') || "zh-cn"; - - this.title(this.lang("title")); - }, - title(str) { - if (str) { - uni.setNavigationBarTitle({ - title: str - }); - } - }, - // 获取语言包列表 - list() { - var list = []; - try { - for (var i = 0; i < langConfig.langList.length; i++) { - let langType = langConfig.langList[i]; - let item = loadLangPackSync(langType, 'common'); - list.push({ - name: item.common ? item.common.name : langType, - value: langType - }); - } - } catch (e) { - console.error('获取语言包列表失败:', e); - } - return list; - } + // 清空已加载的语言包缓存 + for (let key in loadedLangPacks) { + if (!key.startsWith(locale)) { + delete loadedLangPacks[key]; + } + } + + this.refresh(); + + if (url) { + uni.reLaunch({ url: url }); + } + }, + //刷新标题、tabbar + refresh() { + let _page = getCurrentPages()[getCurrentPages().length - 1]; + if (!_page) return; + + const locale = this.getCurrentLocale(); + + this.title(this.lang("title")); + + //设置tabbar的文字语言 + // uni.setTabBarItem({ + // index: 0, + // text: this.lang("tabBar.home") + // }); + // uni.setTabBarItem({ + // index: 1, + // text: this.lang("tabBar.category") + // }); + // uni.setTabBarItem({ + // index: 2, + // text: this.lang("tabBar.cart") + // }); + // uni.setTabBarItem({ + // index: 3, + // text: this.lang("tabBar.member") + // }); + }, + title(str) { + if (str) { + uni.setNavigationBarTitle({ + title: str, + success: function (res) { + }, + fail: function (err) { + } + }); + } + }, + // 获取语言包列表 + list() { + var list = []; + try { + //公共语言包 + for (var i = 0; i < langConfig.langList.length; i++) { + let langType = langConfig.langList[i]; + let item = loadLangPackSync(langType, 'common'); + list.push({ + name: item.common ? item.common.name : langType, + value: langType + }); + } + } catch (e) { + console.error('获取语言包列表失败:', e); + } + return list; + } } \ No newline at end of file diff --git a/components/chat-message/chat-message.vue b/components/chat-message/chat-message.vue index bfa8d4f..910d842 100644 --- a/components/chat-message/chat-message.vue +++ b/components/chat-message/chat-message.vue @@ -156,11 +156,11 @@ - \ No newline at end of file diff --git a/project.config.json b/project.config.json index 2ff4f15..776d6c4 100644 --- a/project.config.json +++ b/project.config.json @@ -7,7 +7,7 @@ "setting": { "urlCheck": true, "es6": true, - "enhance": false, + "enhance": true, "postcss": true, "preloadBackgroundData": false, "minified": true, @@ -39,19 +39,27 @@ "packNpmManually": false, "packNpmRelationList": [], "minifyWXSS": true, - "compileWorklet": false, + "condition": true, + "swc": false, + "disableSWC": true, "minifyWXML": true, + "compileWorklet": true, "localPlugins": false, "disableUseStrict": false, - "useCompilerPlugins": false, - "condition": false, - "swc": false, - "disableSWC": true + "useCompilerPlugins": false }, "compileType": "miniprogram", - "libVersion": "3.12.0", + "libVersion": "2.16.1", "appid": "wx29215aa1bd97bbd6", "projectname": "uniapp", + "debugOptions": { + "hidedInDevtools": [] + }, + "scripts": {}, + "staticServerOptions": { + "baseURL": "", + "servePath": "" + }, "isGameTourist": false, "condition": { "search": { @@ -72,7 +80,5 @@ "miniprogram": { "list": [] } - }, - "simulatorPluginLibVersion": {}, - "editorSetting": {} + } } \ No newline at end of file diff --git a/store/index.js b/store/index.js index bffef98..eb9ebe8 100644 --- a/store/index.js +++ b/store/index.js @@ -416,7 +416,7 @@ const store = new Vuex.Store({ }, // 生成主题颜色CSS变量 themeColorSet() { - console.log('样式颜色设置...'); + // console.log('样式颜色设置...'); let theme = this.state.themeStyle; if (!theme?.main_color || !theme?.aux_color) return; try { @@ -440,7 +440,7 @@ const store = new Vuex.Store({ } catch (e) { console.error('设置主题颜色失败', e); } - console.log('themeColor => ', this.state.themeColor); + // console.log('themeColor => ', this.state.themeColor); } } })