添加 globalThis 给 getApp, getCurrentPages,以支持在后台 nodejs 里使用; 调整 localizeText,只要是 object, 找不到指定及默认语种的key就返回第一个元素的值

This commit is contained in:
Luk 2024-12-17 09:46:25 +08:00
parent fa4bcb4fc6
commit 767fa447a2

View File

@ -7,8 +7,8 @@ const path = require('path')
const BASE_TYPE_DEFAULT = 'SERVER' // one of { SERVER: 服务器, UNICLOUD_FUNC: 云函数, UNICLOUD_OBJECT: 云对象 }
const my = {
langNow () {
// getCurrentPages() 在 topWindow/App.vue 里有可能为空,所以用 getApp().$store 更安全. 20230513: 发现在微信小程序模拟器里getApp().$store.state 未定义,所以还是用 globalThis.wo?.ss
return globalThis.wo?.ss?.i18n?.mylang || getApp()?.$store?.state?.i18n?.mylang
// globalThis.getCurrentPages?.() 在 topWindow/App.vue 里有可能为空,所以用 getApp().$store 更安全. 20230513: 发现在微信小程序模拟器里getApp().$store.state 未定义,所以还是用 globalThis.wo?.ss
return globalThis.wo?.ss?.i18n?.mylang || globalThis.getApp?.()?.$store?.state?.i18n?.mylang
},
}
@ -53,22 +53,31 @@ module.exports = {
thisPage () {
return this.__page__
? this // constructor.name==='VueComponent' 只在 development 环境有用,在 production 环境会被简化成 'o'。
: getCurrentPages()?.pop?.() || {} // [20220401] 发现在 topWindow 里,或者在 App.vue 里, getCurrentPages() 是 undefined 和 空数组 [],因此在这里默认 {} 做保护。
: globalThis.getCurrentPages?.()?.pop?.() || {} // [20220401] 发现在 topWindow 里,或者在 App.vue 里, getCurrentPages() 是 undefined 和 空数组 [],因此在这里默认 {} 做保护。
},
localizeText (i18nText) {
localizeText (i18nText, langCode) {
i18nText =
i18nText || // 如果传入i18n参数 ({zhCN:'...', enUS:'...'})
this.i18nText || // 1) 如果挂载到具体页面的 computed { lote: wo?.localizeText } 那么 this 就是当前页面,直接取用 this.i18nText 即可。2) 对于组件内定义的 i18nText要使用 this 来获得组件内的 i18nText
getCurrentPages()?.pop()?.i18nText // 如果不是挂载到 Vue.prototype 而是 挂载到 wo 下调用,那么 this.i18nText 就不存在了。因此通过 pageNow.i18nText 访问。
return (
i18nText?.[my.langNow()] ||
i18nText?.earTH ||
i18nText?.defLAN ||
i18nText?.gloBAL ||
i18nText?.enUS ||
(['string', 'number', 'boolean'].includes(typeof i18nText) ? i18nText : '')
) // 必须检测是否标量值,如果直接返回 i18nText 可能返回{}等,导致依赖于返回空值的前端出错
if (['string', 'number', 'boolean'].includes(typeof i18nText)) {
// 必须先检测是否标量值,如果直接返回 i18nText 可能返回{}等,导致依赖于返回空值的前端出错
return i18nText
} else if (typeof i18nText === 'object' && i18nText) {
return (
i18nText?.[langCode] ||
i18nText?.[my.langNow()] ||
i18nText?.earTH ||
i18nText?.defLAN ||
i18nText?.gloBAL ||
i18nText?.enUS ||
Object.values(i18nText)[0] ||
''
)
} else {
return ''
}
},
localeText () {
@ -78,7 +87,7 @@ module.exports = {
setBarTitles ({ windowTitle, pageTitle, wo = globalThis.wo } = {}) {
const langNow = my.langNow()
const pageNow = getCurrentPages()?.pop?.()
const pageNow = globalThis.getCurrentPages?.()?.pop?.()
// 在ios/android app里pageNow.route 是正确的,但是 pageNow.xxx 等自定义属性 都 undefined必须 pageNow.$vm.$data.xxx 才可以。
// 注意,$vm.$data 不包括 computed 属性,而 pageNow 里包括。因此不能把 i18nPageTitle 放在 computed 里。
@ -189,7 +198,7 @@ module.exports = {
apiWhat = {},
timeout,
}) {
const thisRoute = getCurrentPages()?.pop?.()?.route || 'VueApp' // 立刻保存 route因为在调用后台后可能已切换到了其他页面。
const thisRoute = globalThis.getCurrentPages?.()?.pop?.()?.route || 'VueApp' // 立刻保存 route因为在调用后台后可能已切换到了其他页面。
const startTime = new Date().toJSON()
let apiurl = undefined
apiWhat._clientInfo = {
@ -630,7 +639,7 @@ module.exports = {
} else if (inWebview) {
wo.ss.webviewUrl = url
wo.ss.webviewTitle = title
uni.navigateTo({ url: 'tool-webview' })
uni.navigateTo({ url: 'show-webview' })
} else {
// #ifdef APP
plus.runtime.openURL(url)
@ -656,7 +665,7 @@ module.exports = {
} else {
wo.ss.webviewUrl = url
wo.ss.webviewTitle = title
uni.navigateTo({ url: 'tool-webview' })
uni.navigateTo({ url: 'show-webview' })
}
},
@ -701,8 +710,16 @@ module.exports = {
if (!title) {
return
}
const mypopup = getCurrentPages()?.pop()?.mypopup || getCurrentPages()?.pop()?.$refs?.mypopup || getApp().globalData?.mypopup || wo?.mypopup
const mytoast = getCurrentPages()?.pop()?.mytoast || getCurrentPages()?.pop()?.$refs?.mytoast || getApp().globalData?.mytoast || wo?.mytoast
const mypopup =
globalThis.getCurrentPages?.()?.pop()?.mypopup ||
globalThis.getCurrentPages?.()?.pop()?.$refs?.mypopup ||
globalThis.getApp().globalData?.mypopup ||
wo?.mypopup
const mytoast =
globalThis.getCurrentPages?.()?.pop()?.mytoast ||
globalThis.getCurrentPages?.()?.pop()?.$refs?.mytoast ||
globalThis.getApp().globalData?.mytoast ||
wo?.mytoast
if (mypopup) {
wo.ss.popMessage = title
wo.ss.popType = type // success/error/warning/info
@ -877,7 +894,7 @@ module.exports = {
},
next_focus (currentFocus, focusList) {
focusList = focusList || getCurrentPages()?.pop()?.focusList
focusList = focusList || globalThis.getCurrentPages?.()?.pop()?.focusList
if (focusList) {
for (let n in focusList) {
// 不论对数组或对象都有效n 是数组的index 或对象的key