This commit is contained in:
陆柯 2023-05-13 21:32:30 +08:00
parent e246e34e78
commit c30c4cc66d

View File

@ -30,15 +30,17 @@ export default {
thisPage () {
return this.__page__
? this // 1) constructor.name==='VueComponent' 只在 development 环境有用,在 production 环境会被简化成 'o'。2)对于组件内定义的 i18nText要使用 this 来获得组建内的 i18nText而不是 getCurrentPages[...] 去访问全局页面的 i18nText
? this // constructor.name==='VueComponent' 只在 development 环境有用,在 production 环境会被简化成 'o'。
: getCurrentPages()[getCurrentPages().length - 1] || {} // [20220401] 发现在 topWindow 里, getCurrentPages 是 undefined。 // 在 App.vue 中调用 getCurrentPages() 返回的是空数组 [],因此在这里默认 {} 做保护。
},
localizeText (i18nText) {
i18nText = i18nText?.__page__
? this.i18nText // 如果挂载到具体页面的 computed { lote: wo.localizeText } 那么 this 就是当前页面,直接取用 this.i18nText 即可。
: i18nText || this.thisPage()?.i18nText // 如果传入i18n参数 ({zhCN:'...', enUS:'...'}) // 如果不是挂载到 Vue.prototype 而是 挂载到 wo 下调用,那么 this.i18nText 就报错了。因此通过 thisPage().i18nText 访问。
const mylang = getApp().$store.state.i18n.mylang // this.thisPage() 有可能为空(例如在 topWindow 里,或者在 App.vue 里),所以用 getApp().$store 更安全
i18nText =
i18nText || // 如果传入i18n参数 ({zhCN:'...', enUS:'...'})
(this?.__page__
? this.i18nText // 1) 如果挂载到具体页面的 computed { lote: wo.localizeText } 那么 this 就是当前页面,直接取用 this.i18nText 即可。2) 对于组件内定义的 i18nText要使用 this 来获得组件内的 i18nText
: this.thisPage()?.i18nText) // 如果不是挂载到 Vue.prototype 而是 挂载到 wo 下调用,那么 this.i18nText 就报错了。因此通过 thisPage().i18nText 访问。
const mylang = wo?.ss?.i18n?.mylang // this.thisPage() 有可能为空(例如在 topWindow 里,或者在 App.vue 里),所以用 getApp().$store 更安全. 20230513: 发现在微信小程序模拟器里getApp().$store.state 未定义
return i18nText?.[mylang] || (typeof i18nText === 'string' ? i18nText : '') // 必须检测是否string如果直接返回 i18nText 可能返回{}等,导致依赖于返回空值的前端出错
},
@ -53,8 +55,8 @@ export default {
const navibarTitle =
pageTitle ||
pageNow?.i18nText?.[mylang]?.tPageTitle || // 页面.vue 的 i18nText 对象
pageNow?.i18nPageTitle?.[mylang] || // 页面.vue 的 i18nPageTitle 变量
pageNow?.i18nText?.[mylang]?.tPageTitle || // 页面.vue 的 i18nText 对象
pageNow?.pageTitle ||
pagesJson?.pages?.find((page) => page.path === pageNow?.route)?.i18nPageTitle?.[mylang] || // pages.json 的页面配置里
''
@ -68,14 +70,9 @@ export default {
}
// #ifdef H5
// navibarTitle 也会被用于浏览器的标签标题,因此要用 document.title 去覆盖。
// navibarTitle 也会被用于浏览器的标签标题,用 document.title 去覆盖。
// 必须放在 setNavigationBarTitle 之后。但这个方案,在电脑上,还是会显示 navibarTitle 在浏览器窗口顶栏,不知为何。
// document.title =
// windowTitle ||
// wo?.envar?.callname?.[mylang] ||
// pagesJson?.appInfo?.i18nText?.[mylang] ||
// pagesJson?.globalStyle?.navigationBarTitleText ||
// '' + (navibarTitle ? ` - ${navibarTitle}` : '')
// document.title = ???
// #endif
// 必须要在有 tab 的页面里 setTabBarItem 才有效果
@ -95,23 +92,34 @@ export default {
})
// uni.showTabBar({})
// #ifdef H5
// 响应式方案:仅仅根据当前设备类型,如果是 PC 大屏幕,则始终显示 topWindow 并且隐藏顶部 navibar 和底部 tabBar。
if (pagesJson?.topWindow || !envar?.showBarsOnPC) {
// 如果页头不是通过 pagesJson.topWindow 而是作为组件来引入个别页面,那么定义配置参数 showBarsOnPC 来控制。
if (uni.getSystemInfoSync().deviceType === 'pc') {
if (window.innerWidth > (pagesJson?.topWindow?.matchMedia?.minWidth || 0)) {
uni.hideTabBar()
// 不知为何,同一个二级页面,如果第二次进入,就仍然会显示 navibar, 必须通过 setTimeout 执行才能彻底隐藏。
setTimeout(() => {
document.getElementsByTagName('uni-page-head')?.[0]?.remove()
}, 0)
// 不再使用 topWindow 方案
// // #ifdef H5
// // 响应式方案:仅仅根据当前设备类型,如果是 PC 大屏幕,则始终显示 topWindow 并且隐藏顶部 navibar 和底部 tabBar。
// if (pagesJson?.topWindow || !envar?.showBarsOnPC) {
// // 如果页头不是通过 pagesJson.topWindow 而是作为组件来引入个别页面,那么定义配置参数 showBarsOnPC 来控制。
// if (uni.getSystemInfoSync().deviceType === 'pc') {
// if (window.innerWidth > (pagesJson?.topWindow?.matchMedia?.minWidth || 0)) {
// uni.hideTabBar()
// // 不知为何,同一个二级页面,如果第二次进入,就仍然会显示 navibar, 必须通过 setTimeout 执行才能彻底隐藏。
// setTimeout(() => {
// document.getElementsByTagName('uni-page-head')?.[0]?.remove()
// }, 0)
// }
// } else {
// document.getElementsByTagName('uni-top-window')?.[0]?.remove() // 强制隐藏 topWindow否则在手机浏览器里topWindow 会遮挡掉 navibar。
// }
// }
// // #endif
//#ifdef H5
//// 微信浏览器里,本身就显示了标题栏,和自有的导航栏形成功能重叠和混淆。
if (uni.getSystemInfoSync().deviceType === 'phone' && /MicroMessenger/i.test(globalThis.window?.navigator?.userAgent)) {
//// 设置标题栏为空
document.title = ''
//// 或者设置导航栏隐藏。但这样导致,用户容易误点微信浏览器标题栏的 X 关掉页面,所以还是显示导航栏吧。
// document.getElementsByTagName('uni-page-head')?.[0]?.remove() // 或者 [0]?.style?.display = 'none'
}
} else {
document.getElementsByTagName('uni-top-window')?.[0]?.remove() // 强制隐藏 topWindow否则在手机浏览器里topWindow 会遮挡掉 navibar。
}
}
// #endif
//#endif
},
makeServerUrl (route = '') {
@ -227,6 +235,7 @@ export default {
apiWho,
apiTodo,
apiWhat,
timeout,
url,
}
)