//生产域名
const url1 = “demo.ecce.cn”;
//开发环境域名
const url2 = “dev-demo.ecce.cn”;
//测试环境域名
const url3 = “test-demo.ecce.cn”;
//uat环境域名
const url4 = “uat-demo.ecce.cn”;
function getToDomain(url ) {
//window.location.hostname 获取当前域名
let arr = window.location.hostname.split("-");
let domainPerfix = arr.length === 1 ? "" : arr[0] + "-";
//window.location.protocol 获取当前地址栏协议: http: 或者 https:
//domainPerfix 代表 dev test uat等前缀
if (arr.length > 1) {
return "http:" + "//" + domainPerfix + url;
} else {
return window.location.protocol + "//" + domainPerfix + url;
}
}
如果当前访问的是:https://dev-demo.ecce.cn/src/index 地址栏,可以使用以上方法自动检测和获取当前动态的协议和域名
const url = getToDomain(url1 ) ; // getToDomain(url1 ) 相当于当前地址栏路径 https://dev-demo.ecce.cn
这时候你可以调用接口:
例如: https://dev-demo.ecce.cn/demo/home/index
动态写成:url + '/demo/home/index'
注意:请勿使用 process.env.NODE_ENV获取协议、域名 等在各个打包发布后的环境中使用,因为无法获取准确的动态域名,只能是在本地调试node环境下使用(爬过的坑)