通过编译期间注入环境变量,以便支持切换API_HOST

This commit is contained in:
Allen 2025-05-17 23:12:41 +08:00
parent 58d3ea3577
commit ab5aa05bd7
8 changed files with 75 additions and 9 deletions

1
.env.development Normal file
View File

@ -0,0 +1 @@
EIAC_DESKTOP_API_HOST=http://1.12.73.211:8848

1
.env.production Normal file
View File

@ -0,0 +1 @@
EIAC_DESKTOP_API_HOST=http://1.12.73.211:8848

14
package-lock.json generated
View File

@ -28,6 +28,7 @@
"@typescript-eslint/eslint-plugin": "^5.62.0", "@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0", "@typescript-eslint/parser": "^5.62.0",
"cross-env": "^7.0.3", "cross-env": "^7.0.3",
"dotenv": "^16.5.0",
"electron": "36.0.0", "electron": "36.0.0",
"eslint": "^8.57.1", "eslint": "^8.57.1",
"eslint-plugin-import": "^2.31.0", "eslint-plugin-import": "^2.31.0",
@ -3470,6 +3471,19 @@
"node": ">=6.0.0" "node": ">=6.0.0"
} }
}, },
"node_modules/dotenv": {
"version": "16.5.0",
"resolved": "https://registry.npmmirror.com/dotenv/-/dotenv-16.5.0.tgz",
"integrity": "sha512-m/C+AwOAr9/W1UOIZUo232ejMNnJAJtYQjUbHoNTBNTJSvqzzDh7vnrei3o3r3m9blf6ZoDkvcw0VmozNRFJxg==",
"dev": true,
"license": "BSD-2-Clause",
"engines": {
"node": ">=12"
},
"funding": {
"url": "https://dotenvx.com"
}
},
"node_modules/dunder-proto": { "node_modules/dunder-proto": {
"version": "1.0.1", "version": "1.0.1",
"resolved": "https://registry.npmmirror.com/dunder-proto/-/dunder-proto-1.0.1.tgz", "resolved": "https://registry.npmmirror.com/dunder-proto/-/dunder-proto-1.0.1.tgz",

View File

@ -6,9 +6,10 @@
"main": ".vite/build/main.js", "main": ".vite/build/main.js",
"scripts": { "scripts": {
"start": "cross-env NODE_ENV=development electron-forge start", "start": "cross-env NODE_ENV=development electron-forge start",
"package": "electron-forge package", "start:prod": "cross-env NODE_ENV=production electron-forge start",
"make": "electron-forge make", "package": "cross-env NODE_ENV=production electron-forge package",
"publish": "electron-forge publish", "make": "cross-env NODE_ENV=production electron-forge make",
"publish": "cross-env NODE_ENV=production electron-forge publish",
"lint": "eslint --ext .ts,.tsx ." "lint": "eslint --ext .ts,.tsx ."
}, },
"keywords": [], "keywords": [],
@ -32,6 +33,7 @@
"@typescript-eslint/eslint-plugin": "^5.62.0", "@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0", "@typescript-eslint/parser": "^5.62.0",
"cross-env": "^7.0.3", "cross-env": "^7.0.3",
"dotenv": "^16.5.0",
"electron": "36.0.0", "electron": "36.0.0",
"eslint": "^8.57.1", "eslint": "^8.57.1",
"eslint-plugin-import": "^2.31.0", "eslint-plugin-import": "^2.31.0",

View File

@ -1,3 +1,6 @@
// 从环境变量中获取 API Host
const EIAC_DESKTOP_API_HOST = process.env.EIAC_DESKTOP_API_HOST || 'http://1.12.73.211:8848';
/** /**
* EIAC API * EIAC API
*/ */
@ -6,7 +9,7 @@ export class EIAC_Desktop_Api {
/** /**
* API Host * API Host
*/ */
constructor(host = 'http://1.12.73.211:8848/EIAC_Desktop_Api') { constructor(host = EIAC_DESKTOP_API_HOST) {
this.host = host; this.host = host;
} }
@ -21,7 +24,7 @@ export class EIAC_Desktop_Api {
* @returns * @returns
*/ */
LoginAsync: async (request: LoginRequest, init?: RequestInit) => { LoginAsync: async (request: LoginRequest, init?: RequestInit) => {
const response = await fetch(`${this.host}/api/Auth/Login`, { const response = await fetch(`${this.host}/EIAC_Desktop_Api/api/Auth/Login`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json; charset=utf-8' 'Content-Type': 'application/json; charset=utf-8'
@ -44,7 +47,7 @@ export class EIAC_Desktop_Api {
* @returns * @returns
*/ */
GetMenuAsync: async (init?: RequestInit) => { GetMenuAsync: async (init?: RequestInit) => {
const response = await fetch(`${this.host}/api/Menu/GetMenu`, { const response = await fetch(`${this.host}/EIAC_Desktop_Api/api/Menu/GetMenu`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json; charset=utf-8' 'Content-Type': 'application/json; charset=utf-8'
@ -60,7 +63,7 @@ export class EIAC_Desktop_Api {
* @returns * @returns
*/ */
GetConfigAsync: async (configName = 'TagResolution', init?: RequestInit) => { GetConfigAsync: async (configName = 'TagResolution', init?: RequestInit) => {
const response = await fetch(`${this.host}/api/Menu/GetConfig?configName=${configName}`, { const response = await fetch(`${this.host}/EIAC_Desktop_Api/api/Menu/GetConfig?configName=${configName}`, {
method: 'GET', method: 'GET',
headers: { headers: {
'Content-Type': 'application/json; charset=utf-8' 'Content-Type': 'application/json; charset=utf-8'
@ -83,7 +86,7 @@ export class EIAC_Desktop_Api {
* @returns * @returns
*/ */
FaultReportingAsync: async (request: FaultReportingRequest, init?: RequestInit) => { FaultReportingAsync: async (request: FaultReportingRequest, init?: RequestInit) => {
const response = await fetch(`${this.host}/api/Help/FaultReporting`, { const response = await fetch(`${this.host}/EIAC_Desktop_Api/api/Help/FaultReporting`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json; charset=utf-8' 'Content-Type': 'application/json; charset=utf-8'

View File

@ -5,6 +5,8 @@ import { initialize, preloadData } from './IpcMainHandler';
const isDevelopment: boolean = process.env.NODE_ENV === 'development'; const isDevelopment: boolean = process.env.NODE_ENV === 'development';
console.log('[Main] API Host:', process.env.EIAC_DESKTOP_API_HOST);
// Ensure only one instance is running // Ensure only one instance is running
const gotTheLock: boolean = app.requestSingleInstanceLock(); const gotTheLock: boolean = app.requestSingleInstanceLock();
if (!gotTheLock) { if (!gotTheLock) {

View File

@ -1,4 +1,26 @@
import { defineConfig } from 'vite'; import { defineConfig } from 'vite';
import dotenv from 'dotenv';
import path from 'path';
import fs from 'fs';
// 加载环境变量
dotenv.config();
// 然后根据 NODE_ENV 再次加载 .env.production 或 .env.staging
const mode = process.env.NODE_ENV || 'development';
const envPath = path.resolve(__dirname, `.env.${mode}`);
if (fs.existsSync(envPath)) {
dotenv.config({ path: envPath });
}
// https://vitejs.dev/config // https://vitejs.dev/config
export default defineConfig({}); export default defineConfig({
// 使用 Vite 的 env 配置
envPrefix: ['EIAC_DESKTOP_API_HOST', 'NODE_ENV'],
// 定义环境变量
define: {
// 使用 process.env 访问环境变量
'process.env.EIAC_DESKTOP_API_HOST': JSON.stringify(process.env.EIAC_DESKTOP_API_HOST || 'http://1.12.73.211:8848'),
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development')
}
});

View File

@ -1,4 +1,17 @@
import { defineConfig } from 'vite'; import { defineConfig } from 'vite';
import dotenv from 'dotenv';
import path from 'path';
import fs from 'fs';
// 加载环境变量
dotenv.config();
// 然后根据 NODE_ENV 再次加载 .env.production 或 .env.staging
const mode = process.env.NODE_ENV || 'development';
const envPath = path.resolve(__dirname, `.env.${mode}`);
if (fs.existsSync(envPath)) {
dotenv.config({ path: envPath });
}
// https://vitejs.dev/config // https://vitejs.dev/config
export default defineConfig({ export default defineConfig({
@ -9,5 +22,13 @@ export default defineConfig({
index: 'index.html' index: 'index.html'
} }
} }
},
// 使用 Vite 的 env 配置
envPrefix: ['EIAC_DESKTOP_API_HOST', 'NODE_ENV'],
// 定义环境变量
define: {
// 使用 process.env 访问环境变量
'process.env.EIAC_DESKTOP_API_HOST': JSON.stringify(process.env.EIAC_DESKTOP_API_HOST || 'http://1.12.73.211:8848'),
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development')
} }
}); });