// 远程获取语言包并加载 import i18n from "@/main"; import { CommonUtils } from "./common.js" let MissingKeysMap = {} let enableCollectMissingKey = false let clockID = null // 远程获取语言包 export async function getLanguagePackJson(language, forceUpdate = false) { // TODO 获取语言包时,添加一个版本字段,对比浏览器缓存中的版本字段和后端文件的是否对应 // 如果对应,则不返回任何文件,使用缓存文件即可。 try { const res = await CommonUtils.doRequest2Async({ url: '/Xt_grdAlignment_WMES/SelectMESLanguage_JSON', data: { language: language, forceUpdate: forceUpdate } }) let { data, Message, count } = res.data if (count == 1) { enableCollectMissingKey = true let getRemoteFile = true if (!res.data.EnableRemoteI18n || res.data.EnableRemoteI18n == 'N') { // 判断服务器是否启用多语言配置项,无该配置项或者不启用配置项,则不收集缺失字符串,使用本地翻译文件 enableCollectMissingKey = false getRemoteFile = false } return { data: data, getRemoteFile: getRemoteFile } } else { enableCollectMissingKey = false CommonUtils.showTips({ title: "温馨提示", message: "[i18n]获取语言包失败: " + Message }) return { data: [], getRemoteFile: false } } } catch (err) { enableCollectMissingKey = false CommonUtils.showTips({ title: "温馨提示", message: "[i18n]获取语言包失败: " + err.message }) return } } export function addMissingKeyToMap(key, HFieldCode) { // 防止页面初次加载时,使用本地翻译模块导致的 假缺失现象 if (enableCollectMissingKey) { if (!MissingKeysMap[key]) { MissingKeysMap[key] = new Set() } MissingKeysMap[key].add(HFieldCode) if (!clockID) { clockID = setTimeout(() => { syncMissingKeyToDB() }, 1000) } else { clearTimeout(clockID) clockID = setTimeout(() => { syncMissingKeyToDB() }, 1000) } } } // 添加缺失字段到远程数据库 export async function syncMissingKeyToDB() { console.log('MissingKeysMap: ', MissingKeysMap); for (let key in MissingKeysMap) { if (MissingKeysMap.hasOwnProperty(key)) { MissingKeysMap[key] = Array.from(MissingKeysMap[key]) } } await CommonUtils.doRequest2Sync({ url: "/Xt_grdAlignment_WMES/syncMissingKeyToDB", data: { missingObj: JSON.stringify(MissingKeysMap) } }) // 清除缺失字段缓存 MissingKeysMap = {} uni.hideLoading() // 重启应用 const systemInfo = uni.getSystemInfoSync(); const platform = systemInfo.platform; // #ifdef APP-PLUS // App 端:完全重启 plus.runtime.restart(); // #endif // #ifdef H5 console.log('H5回到登录页: '); uni.reLaunch({ url: '/pages/index/login' }) // #endif }