chenhaozhe
2026-01-16 72fd20db54200971976d22db77e2a11f0bafdb9f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// 远程获取语言包并加载
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();
    }
}