From 767fa447a2b9218afbfb63b1f24a574fdcf88cab Mon Sep 17 00:00:00 2001 From: Luk Date: Tue, 17 Dec 2024 09:46:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20globalThis=20=E7=BB=99=20g?= =?UTF-8?q?etApp,=20getCurrentPages=EF=BC=8C=E4=BB=A5=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=9C=A8=E5=90=8E=E5=8F=B0=20nodejs=20=E9=87=8C=E4=BD=BF?= =?UTF-8?q?=E7=94=A8;=20=E8=B0=83=E6=95=B4=20localizeText=EF=BC=8C?= =?UTF-8?q?=E5=8F=AA=E8=A6=81=E6=98=AF=20object,=20=E6=89=BE=E4=B8=8D?= =?UTF-8?q?=E5=88=B0=E6=8C=87=E5=AE=9A=E5=8F=8A=E9=BB=98=E8=AE=A4=E8=AF=AD?= =?UTF-8?q?=E7=A7=8D=E7=9A=84key=E5=B0=B1=E8=BF=94=E5=9B=9E=E7=AC=AC?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E5=85=83=E7=B4=A0=E7=9A=84=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- unitool.js | 55 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/unitool.js b/unitool.js index d07108f..ddfa9b5 100644 --- a/unitool.js +++ b/unitool.js @@ -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