From 3e186ad46ad687bb00ce1bf8ea06744b999a04f8 Mon Sep 17 00:00:00 2001 From: "luk.lu" Date: Mon, 16 Jan 2023 15:10:50 +0800 Subject: [PATCH] improve makeServerUrl to eliminate ':port' when port is empty --- unitool.js | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/unitool.js b/unitool.js index 38cf7cd..0553507 100644 --- a/unitool.js +++ b/unitool.js @@ -151,24 +151,11 @@ export default { return route } - let hostname = envar.servHostname - let port = envar.servPort - // #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 hostname = envar.servHostname || globalThis.window?.location?.hostname || 'localhost' + const port = envar.servPort || globalThis.window?.location?.port || '' 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) {