通过编译期间注入环境变量,以便支持切换API_HOST
This commit is contained in:
parent
58d3ea3577
commit
ab5aa05bd7
1
.env.development
Normal file
1
.env.development
Normal file
@ -0,0 +1 @@
|
||||
EIAC_DESKTOP_API_HOST=http://1.12.73.211:8848
|
1
.env.production
Normal file
1
.env.production
Normal file
@ -0,0 +1 @@
|
||||
EIAC_DESKTOP_API_HOST=http://1.12.73.211:8848
|
14
package-lock.json
generated
14
package-lock.json
generated
@ -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",
|
||||
|
@ -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",
|
||||
|
@ -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'
|
||||
|
@ -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) {
|
||||
|
@ -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')
|
||||
}
|
||||
});
|
||||
|
@ -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')
|
||||
}
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user