// 远程获取语言包并加载
|
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
|
return data
|
} else {
|
enableCollectMissingKey = false
|
CommonUtils.showTips({
|
title: "温馨提示",
|
message: "[i18n]获取语言包失败: " + Message
|
})
|
return
|
}
|
} 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])
|
}
|
}
|
CommonUtils.doRequest2Sync({
|
url: "/Xt_grdAlignment_WMES/syncMissingKeyToDB",
|
data: {
|
missingObj: JSON.stringify(MissingKeysMap)
|
}
|
})
|
// 清除缺失字段缓存
|
MissingKeysMap = {}
|
// 重启应用
|
const systemInfo = uni.getSystemInfoSync();
|
const platform = systemInfo.platform;
|
|
if (platform === 'ios' || platform === 'android') {
|
// App 端:完全重启
|
plus.runtime.restart();
|
} else if (platform === 'h5') {
|
// H5 端:刷新页面
|
window.location.reload();
|
}
|
}
|