yusijie
5 天以前 21c2a7c125a5d07ad87fd7a266a697f61d0fdefd
main.js
@@ -15,7 +15,7 @@
    locale: uni.getLocale(), // 获取已设置的语言
    messages,
    silentTranslationWarn: true,
    // missing: function(locale, key, vm, values){
    //   // 页面中含有语言包中不存在的字段,需同步到数据库中
    //     console.log("[i18n]: 页面中key为: ", key ,"的字段在翻译文件中不存在,同步到数据库...")
@@ -26,6 +26,61 @@
    //   
    // }
}
// uniapp 全局请求拦截器
uni.addInterceptor('request', {
    invoke(args) {
        // 获取到当前调用的页面
        const pages = getCurrentPages()
        const currentPage = pages[pages.length - 1]
        // 拿到页面定义的 modName
        const modName = currentPage?.$vm.HModName || ''
        // 请求头带上 token
        let token = uni.getStorageSync('Token')
        if (token) {
            // 先初始化,防止 undefined 报错
            args.header = args.header || {}
            args.header.Authorization = `Bearer ${token}`
            args.header["X-HModName"] = modName
        }
    },
    success(res) {
        // 自动续期 token
        if (res.data && res.data.token) {
            uni.setStorageSync('Token', res.data.token)
        }
        // 401 自动跳登录
        if (res.statusCode === 401) {
            uni.removeStorageSync('Token')
            uni.showModal({
                title: '温馨提示',
                content: res.data.Message,
                showCancel: false,
                success: () => {
                    uni.reLaunch({
                        url: '/pages/index/login'
                    })
                }
            })
        }
        // 403 返回上一个页面
        if (res.statusCode === 403) {
            uni.showModal({
                title: '温馨提示',
                content: res.data.Message,
                showCancel: false,
                success: () => {
                    uni.navigateBack()
                }
            })
        }
    }
})
import App from './App'
import Printer from './plugins/printer'
@@ -53,8 +108,8 @@
Vue.prototype.$t = function(key, ...args) {
    // 调用原始 $t 执行翻译
    const translated = originalT.call(this, key, args);
    // console.log(i18n.getLocaleMessage(i18n.locale))
    if (translated == key) {
        // 翻译后的文本与传入的key相同,则表示翻译文本缺失 记录缺失的翻译文本
        let [moduleName, fieldCode] = key.split(".")