diff --git a/.env.development b/.env.development new file mode 100644 index 0000000..5ab511d --- /dev/null +++ b/.env.development @@ -0,0 +1 @@ +EIAC_DESKTOP_API_HOST=http://1.12.73.211:8848 \ No newline at end of file diff --git a/.env.production b/.env.production new file mode 100644 index 0000000..5ab511d --- /dev/null +++ b/.env.production @@ -0,0 +1 @@ +EIAC_DESKTOP_API_HOST=http://1.12.73.211:8848 \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 1757834..000f897 100644 --- a/package-lock.json +++ b/package-lock.json @@ -28,6 +28,7 @@ "@typescript-eslint/eslint-plugin": "^5.62.0", "@typescript-eslint/parser": "^5.62.0", "cross-env": "^7.0.3", + "dotenv": "^16.5.0", "electron": "36.0.0", "eslint": "^8.57.1", "eslint-plugin-import": "^2.31.0", @@ -3470,6 +3471,19 @@ "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": { "version": "1.0.1", "resolved": "https://registry.npmmirror.com/dunder-proto/-/dunder-proto-1.0.1.tgz", diff --git a/package.json b/package.json index e39df0e..63d7464 100644 --- a/package.json +++ b/package.json @@ -6,9 +6,10 @@ "main": ".vite/build/main.js", "scripts": { "start": "cross-env NODE_ENV=development electron-forge start", - "package": "electron-forge package", - "make": "electron-forge make", - "publish": "electron-forge publish", + "start:prod": "cross-env NODE_ENV=production electron-forge start", + "package": "cross-env NODE_ENV=production electron-forge package", + "make": "cross-env NODE_ENV=production electron-forge make", + "publish": "cross-env NODE_ENV=production electron-forge publish", "lint": "eslint --ext .ts,.tsx ." }, "keywords": [], @@ -32,6 +33,7 @@ "@typescript-eslint/eslint-plugin": "^5.62.0", "@typescript-eslint/parser": "^5.62.0", "cross-env": "^7.0.3", + "dotenv": "^16.5.0", "electron": "36.0.0", "eslint": "^8.57.1", "eslint-plugin-import": "^2.31.0", diff --git a/src/EIAC_Desktop_Api.ts b/src/EIAC_Desktop_Api.ts index 3179c8e..fa6d92f 100644 --- a/src/EIAC_Desktop_Api.ts +++ b/src/EIAC_Desktop_Api.ts @@ -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 */ @@ -6,7 +9,7 @@ export class EIAC_Desktop_Api { /** * 构造函数,传入API Host */ - constructor(host = 'http://1.12.73.211:8848/EIAC_Desktop_Api') { + constructor(host = EIAC_DESKTOP_API_HOST) { this.host = host; } @@ -21,7 +24,7 @@ export class EIAC_Desktop_Api { * @returns 登录结果 */ 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', headers: { 'Content-Type': 'application/json; charset=utf-8' @@ -44,7 +47,7 @@ export class EIAC_Desktop_Api { * @returns 菜单列表 */ 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', headers: { 'Content-Type': 'application/json; charset=utf-8' @@ -60,7 +63,7 @@ export class EIAC_Desktop_Api { * @returns 配置列表 */ 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', headers: { 'Content-Type': 'application/json; charset=utf-8' @@ -83,7 +86,7 @@ export class EIAC_Desktop_Api { * @returns 故障报告结果 */ 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', headers: { 'Content-Type': 'application/json; charset=utf-8' diff --git a/src/main.ts b/src/main.ts index 4f69bc4..845ba5b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,6 +5,8 @@ import { initialize, preloadData } from './IpcMainHandler'; 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 const gotTheLock: boolean = app.requestSingleInstanceLock(); if (!gotTheLock) { diff --git a/vite.main.config.ts b/vite.main.config.ts index 690be5b..db5ff8e 100644 --- a/vite.main.config.ts +++ b/vite.main.config.ts @@ -1,4 +1,26 @@ 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 -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') + } +}); diff --git a/vite.renderer.config.ts b/vite.renderer.config.ts index 959f6c6..4a6488d 100644 --- a/vite.renderer.config.ts +++ b/vite.renderer.config.ts @@ -1,4 +1,17 @@ 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 export default defineConfig({ @@ -9,5 +22,13 @@ export default defineConfig({ 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') } });