Commit 243664de by Jaxon

fix: 从环境变量读取配置中心地址,避免硬编码

1 parent b8d163a7
Showing 1 changed file with 61 additions and 49 deletions
......@@ -115,58 +115,70 @@ Vue.directive("preventReClick", {
});
let baseConfig = config;
// 如果要改地址, 优先去 .env.development 中配置
// 如果要创建自己的配置 可以 创建.env.development.local 文件. 该文件已被 git忽略, 不会上传, 优先级比.env.development更高, 满足本地开发的需求.
// 生产环境和测试环境用的都是 .env文件里面的内容. 不会串
// 这样配置, 不会再出现 改了地址 , 上传到git 甚至 上到测试环境 的情况
// 唯一需要注意的是, 改了 .env.development.local 文件的内容, 需要重新 npm run serve
/**
* 另: 在本地开发时,优先级是 .env.development.local > .env.development > .env.production > .env
* 以 VUE_APP_A 这个变量为例
* 如果 .env.development.local 文件没有, 就会取 .env.development 中的, 以此类推直到.env, 都没有 则返回 undefined
* 如果 所有 文件都有这个变量, 优先取 .env.development.local的
* 在生产环境中:
* 只取 .env.production .env 两个文件中的.
*/
const configCenterUrl = process.env.VUE_APP_CONFIG_CENTER_URL || "https://config-center-backend.glsx.com.cn/glsxAdmin/config?moduleUrl=dj-car-life-system-config";
console.log("configCenterUrl", configCenterUrl);
// 使用fetch 解决axios提前实例化导致sign里面的secret不能通过配置中心动态获取
fetch(
"https://config-center-backend.glsx.com.cn/glsxAdmin/config?moduleUrl=dj-car-life-system-config",
)
.then(
(response) => {
// 处理http响应
return response.json();
},
(error) => {
// 处理错误
console.log(error);
},
)
.then((response) => {
const { code, data } = response;
console.log("response", code, response);
if (code == "200") {
console.log(data);
baseConfig = data;
// console.log("baseConfig", baseConfig)
// window.$fetchConfig = data
} else {
baseConfig = response;
window.$fetchConfig = response;
console.log(response);
window.$gscApi = new Request({
...response.gsc,
// baseURL:'http://192.168.3.145:9898/gsc-vcp-web-facade',
// baseURL: 'http://gsc.test.glsx.com.cn/gsc-vcp-web-facade',
session: {
requiresSession: true,
sessionId: _ => util.cookies.get("token"),
fetch(configCenterUrl).then(
(response) => {
// 处理http响应
return response.json();
},
(error) => {
// 处理错误
console.log(error);
},
).then((response) => {
const { code, data } = response;
console.log("response", code, response);
if (code == "200") {
console.log(data);
baseConfig = data;
// console.log("baseConfig", baseConfig)
// window.$fetchConfig = data
} else {
baseConfig = response;
window.$fetchConfig = response;
console.log(response);
window.$gscApi = new Request({
...response.gsc,
// baseURL:'http://192.168.3.145:9898/gsc-vcp-web-facade',
// baseURL: 'http://gsc.test.glsx.com.cn/gsc-vcp-web-facade',
session: {
requiresSession: true,
sessionId: _ => util.cookies.get("token"),
},
events: {
error: (error, config) => {
Vue.prototype.$message.error(error.msg);
},
events: {
error: (error, config) => {
Vue.prototype.$message.error(error.msg);
},
logout: (response, config) => {
store.dispatch("d2admin/user/clear");
util.cookies.remove("token");
router.replace("/login");
Vue.prototype.$message.error("登录超时,请重新登录");
},
logout: (response, config) => {
store.dispatch("d2admin/user/clear");
util.cookies.remove("token");
router.replace("/login");
Vue.prototype.$message.error("登录超时,请重新登录");
},
});
console.log(window.$gscApi, "========");
}
})
.finally(() => {
init();
});
},
});
console.log(window.$gscApi, "========");
}
}).finally(() => {
init();
});
function init() {
new Vue({
router,
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!