makeServerUrl 支持 envar.servUrl

This commit is contained in:
陆柯 2023-05-14 23:58:20 +08:00
parent 076a42f86f
commit 274dc1bd29

View File

@ -61,18 +61,25 @@ export default {
pagesJson?.pages?.find((page) => page.path === pageNow?.route)?.i18nPageTitle?.[mylang] || // pages.json 的页面配置里 pagesJson?.pages?.find((page) => page.path === pageNow?.route)?.i18nPageTitle?.[mylang] || // pages.json 的页面配置里
'' ''
windowTitle = windowTitle || wo?.envar?.callname?.[mylang] || pagesJson?.appInfo?.i18nText?.[mylang] || pagesJson?.globalStyle?.navigationBarTitleText || ''
if (uni.getSystemInfoSync().deviceType === 'pc') { if (uni.getSystemInfoSync().deviceType === 'pc') {
windowTitle =
windowTitle || wo?.envar?.callname?.[mylang] || pagesJson?.appInfo?.i18nText?.[mylang] || pagesJson?.globalStyle?.navigationBarTitleText || ''
uni.setNavigationBarTitle({ title: windowTitle + (navibarTitle ? ` - ${navibarTitle}` : '') }) uni.setNavigationBarTitle({ title: windowTitle + (navibarTitle ? ` - ${navibarTitle}` : '') })
} else { } else {
uni.setNavigationBarTitle({ title: navibarTitle }) uni.setNavigationBarTitle({ title: navibarTitle })
} }
//#ifdef H5 //#ifdef H5
// navibarTitle 也会被用于浏览器的标签标题,可用 document.title 去覆盖。 //// 设置窗口标题栏 document.title
// 必须放在 setNavigationBarTitle 之后。但这个方案,在电脑上,还是会显示 navibarTitle 在浏览器窗口顶栏,不知为何。 //// navibarTitle 也会被用于浏览器的标签标题,可用 document.title 去覆盖。必须放在 setNavigationBarTitle 之后。
// document.title = ??? //// 但这个方案,在电脑上,还是会显示 navibarTitle 在浏览器窗口顶栏,不知为何。
if (uni.getSystemInfoSync().deviceType === 'phone' && /MicroMessenger/i.test(globalThis.window?.navigator?.userAgent)) {
//// 微信浏览器里,本身就显示了标题栏,和自有的导航栏形成功能重叠和混淆。
//// 设置标题栏为空或覆盖
document.title = windowTitle
//// 或者设置导航栏隐藏。但这样导致,用户容易误点微信浏览器标题栏的 X 关掉页面,所以还是显示导航栏吧。
// document.getElementsByTagName('uni-page-head')?.[0]?.remove() // 或者 [0]?.style?.display = 'none'
}
//#endif //#endif
if (uni.getSystemInfoSync().deviceType === 'pc') { if (uni.getSystemInfoSync().deviceType === 'pc') {
@ -94,32 +101,26 @@ export default {
} }
}) })
} }
//#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'
}
//#endif
}, },
makeServerUrl (route = '') { makeServerUrl (route = '') {
const envar = this.envar || wo?.envar || {}
if (typeof route !== 'string') route = '' // 防止 route 为 null, undefined 等由于后台数据库默认值而造成的异常。
route = route.replace('\\', '/')
if (/^https?:\/\//.test(route)) { if (/^https?:\/\//.test(route)) {
return route return route
} }
if (typeof route !== 'string') route = '' // 防止 route 为 null, undefined 等由于后台数据库默认值而造成的异常。
route = route.replace('\\', '/').replace(/^\//, '')
const envar = this.envar || wo?.envar || {}
if (envar.servUrl) {
return `${envar.servUrl}/${route}`
} else {
const hostname = envar.servHostname /*|| globalThis.window?.location?.hostname*/ || 'localhost' const hostname = envar.servHostname /*|| globalThis.window?.location?.hostname*/ || 'localhost'
const port = envar.servPort /*|| globalThis.window?.location?.port*/ || '' const port = envar.servPort /*|| globalThis.window?.location?.port*/ || ''
const protocol = hostname === 'localhost' ? 'http' : envar.servProtocol || (process.env.NODE_ENV === 'production' ? 'https' : 'http') const protocol = hostname === 'localhost' ? 'http' : envar.servProtocol || (process.env.NODE_ENV === 'production' ? 'https' : 'http')
return `${protocol}://${hostname}${port ? ':' : ''}${port}/${route}`
return `${protocol}://${hostname}${port ? ':' : ''}${port}/${route.replace(/^\//, '')}` }
}, },
makeBgUrl (path) { makeBgUrl (path) {