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", { ...@@ -115,58 +115,70 @@ Vue.directive("preventReClick", {
}); });
let baseConfig = config; 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 解决axios提前实例化导致sign里面的secret不能通过配置中心动态获取
fetch( fetch(configCenterUrl).then(
"https://config-center-backend.glsx.com.cn/glsxAdmin/config?moduleUrl=dj-car-life-system-config", (response) => {
) // 处理http响应
.then( return response.json();
(response) => { },
// 处理http响应 (error) => {
return response.json(); // 处理错误
}, console.log(error);
(error) => { },
// 处理错误 ).then((response) => {
console.log(error); const { code, data } = response;
}, console.log("response", code, response);
) if (code == "200") {
.then((response) => { console.log(data);
const { code, data } = response; baseConfig = data;
console.log("response", code, response); // console.log("baseConfig", baseConfig)
if (code == "200") { // window.$fetchConfig = data
console.log(data); } else {
baseConfig = data; baseConfig = response;
// console.log("baseConfig", baseConfig) window.$fetchConfig = response;
// window.$fetchConfig = data console.log(response);
} else { window.$gscApi = new Request({
baseConfig = response; ...response.gsc,
window.$fetchConfig = response; // baseURL:'http://192.168.3.145:9898/gsc-vcp-web-facade',
console.log(response); // baseURL: 'http://gsc.test.glsx.com.cn/gsc-vcp-web-facade',
window.$gscApi = new Request({ session: {
...response.gsc, requiresSession: true,
// baseURL:'http://192.168.3.145:9898/gsc-vcp-web-facade', sessionId: _ => util.cookies.get("token"),
// baseURL: 'http://gsc.test.glsx.com.cn/gsc-vcp-web-facade', },
session: { events: {
requiresSession: true, error: (error, config) => {
sessionId: _ => util.cookies.get("token"), Vue.prototype.$message.error(error.msg);
}, },
events: { logout: (response, config) => {
error: (error, config) => { store.dispatch("d2admin/user/clear");
Vue.prototype.$message.error(error.msg); util.cookies.remove("token");
}, router.replace("/login");
logout: (response, config) => { Vue.prototype.$message.error("登录超时,请重新登录");
store.dispatch("d2admin/user/clear");
util.cookies.remove("token");
router.replace("/login");
Vue.prototype.$message.error("登录超时,请重新登录");
},
}, },
}); },
console.log(window.$gscApi, "========"); });
} console.log(window.$gscApi, "========");
}) }
.finally(() => { }).finally(() => {
init(); init();
}); });
function init() { function init() {
new Vue({ new Vue({
router, router,
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!