修改 nacos-federation 插件,实现 接口请求基地址从 配置中心获取,并注入到环境变量中

This commit is contained in:
编码猿
2025-09-23 03:26:10 +08:00
parent cbc2c25cf4
commit 84c4e8bdc7
22 changed files with 363 additions and 47 deletions

View File

@@ -0,0 +1,11 @@
# Nacos 配置中心ip:端口
VITE_NACOS_ADDRESS=192.168.31.101:8848
# traefik 网关的地址
VITE_TRAEFIK_ADDRESS=192.168.31.101
# 注册到 Nacos 的服务名称(微前端模块名称)
VITE_SERVER_NAME=app-shared
# 注册到 Nacos 的端口,也是前端运行的端口
VITE_SERVER_PORT=1333

View File

@@ -1,6 +1,9 @@
# Nacos 配置中心ip:端口
VITE_NACOS_ADDRESS=192.168.31.101:8848
# traefik 网关的地址
VITE_TRAEFIK_ADDRESS=192.168.31.101
# 注册到 Nacos 的服务名称(微前端模块名称)
VITE_SERVER_NAME=app-shared

View File

@@ -17,6 +17,7 @@
"nacos-federation": "link:../nacos-federation"
},
"devDependencies": {
"@vitejs/plugin-vue": "^6.0.1",
"@originjs/vite-plugin-federation": "^1.4.1",
"typescript": "~5.8.3",
"vite": "^7.1.6"

View File

@@ -1,5 +1,9 @@
export class HttpConfig {
public static DevBaseUrl: string = "http://127.0.0.1:3210/api"
public static prodBaseUrl: string = "http://127.0.0.1:3210/api/"
// public static DevBaseUrl: string = "http://127.0.0.1:3210/api"
public static DevBaseUrl: string = import.meta.env.VITE_DEVBASEURL
// public static ProdBaseUrl: string = "http://127.0.0.1:3210/api/"
public static ProdBaseUrl: string = import.meta.env.VITE_PRODBASEURL
public static apiList: string = ""
}

View File

@@ -1,5 +1,5 @@
import axios, {AxiosError, type AxiosInstance, type AxiosRequestConfig} from "axios";
import {UrlCheckUtils} from '../utils/index'
import axios, { AxiosError, type AxiosInstance, type AxiosRequestConfig } from "axios";
import { UrlCheckUtils } from '../utils/index'
class AxiosInfra {
@@ -32,7 +32,7 @@ class AxiosInfra {
/**
* Put 请求
* 请求参数请参考接口RequestParams
* 请求参数请参考接口RequestParams
* @param params
*/
public async put(params: AxiosRequestConfig): Promise<any> {
@@ -41,7 +41,7 @@ class AxiosInfra {
/**
* delete 请求
* 请求参数请参考接口RequestParams
* 请求参数请参考接口RequestParams
* @param params
*/
public async delete(params: AxiosRequestConfig): Promise<any> {
@@ -60,16 +60,16 @@ class AxiosInfra {
(error: AxiosError) => {
console.log(`
❌ 请求错误 ❌
1、基地址: ${error.config?.baseURL}
2、短地址: ${error.config?.url}
3、请求 方式: ${error.config?.method}
4、请求 参数: ${JSON.stringify(error.config?.params)}
5、请求 头 : ${JSON.stringify(error.config?.headers)}
1、基地址: ${ error.config?.baseURL }
2、短地址: ${ error.config?.url }
3、请求 方式: ${ error.config?.method }
4、请求 参数: ${ JSON.stringify(error.config?.params) }
5、请求 头 : ${ JSON.stringify(error.config?.headers) }
------------------------------------------------------------
6、错误信息为: ${error.message}
7、错误代码为: ${error.stack}
6、错误信息为: ${ error.message }
7、错误代码为: ${ error.stack }
`);
return Promise.reject(error)
})

View File

@@ -1,10 +1,10 @@
import {HttpConfig} from '../config'
import { HttpConfig } from '../config'
export class UrlCheckUtils {
public static check() {
if (import.meta.env.MODE === 'production') {
return HttpConfig.prodBaseUrl;
return HttpConfig.ProdBaseUrl;
} else {
return HttpConfig.DevBaseUrl;
}

View File

@@ -1,6 +1,8 @@
import { defineConfig, loadEnv, UserConfig } from 'vite'
import federation from "@originjs/vite-plugin-federation";
import { nacosRegVitePlugin } from "nacos-federation";
// @ts-ignore
import vue from '@vitejs/plugin-vue';
export default defineConfig(async ({ mode }): Promise<UserConfig> => {
const env = loadEnv(mode, process.cwd());
@@ -25,7 +27,8 @@ export default defineConfig(async ({ mode }): Promise<UserConfig> => {
shared: {
axios: { requiredVersion: '^1.12.2' }
}
})
}),
vue()
],
build: {
target: 'esnext',