improve makeServerUrl to eliminate ':port' when port is empty

This commit is contained in:
陆柯 2023-01-16 15:10:50 +08:00
parent 64527f727d
commit 3e186ad46a

View File

@ -151,24 +151,11 @@ export default {
return route return route
} }
let hostname = envar.servHostname const hostname = envar.servHostname || globalThis.window?.location?.hostname || 'localhost'
let port = envar.servPort const port = envar.servPort || globalThis.window?.location?.port || ''
// #ifdef H5
if (!hostname) {
hostname = window?.location?.hostname
}
if (!port) {
port = window?.location?.port?.replace(':', '')
}
// #endif
if (!hostname) {
// 如果没有配置 envar.servHostname又不是在 H5 环境,则最后的默认值是 localhost
hostname = 'localhost'
}
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}/${route.replace(/^\//, '')}` return `${protocol}://${hostname}${port ? ':' : ''}${port}/${route.replace(/^\//, '')}`
}, },
makeBgUrl (path) { makeBgUrl (path) {