feat(config): 动态加载配置文件并支持远程和本地路径

- 在 preload 中添加 readConfig函数,动态加载 config.js
- 修改 logWebSocket 和 socket 以使用动态加载的配置
- 更新 calibration 页面以使用新配置
- 添加资源目录下的 config.js 文件,包含不同环境的 API URL
This commit is contained in:
fhysy 2025-06-30 16:39:32 +08:00
parent f697d427ad
commit 9646a4ae8c
7 changed files with 74 additions and 19 deletions

View File

@ -1,6 +1,6 @@
{
"name": "calibration-pc",
"version": "1.0.0",
"version": "1.0.2",
"description": "谷云开发部开发的断路器标定软件",
"main": "./out/main/index.js",
"author": "example.com",

View File

@ -0,0 +1,16 @@
module.exports = {
// url: 'http://127.0.0.1:8000',
// wsUrl: 'ws://127.0.0.1:8000',
// swichWsUrl: 'ws://127.0.0.1:8001',
// serialPortUrl: 'http://127.0.0.1:8000',
url: 'http://192.168.1.17:8000',
wsUrl: 'ws://192.168.1.17:8000',
swichWsUrl: 'ws://192.168.1.17:8001',
serialPortUrl: 'http://192.168.1.17:8000',
// 远程连接串口使用
// serialPortUrl: 'http://120.77.172.42:7202',
// serialPortUrl: 'http://192.168.1.17:8888',
}

View File

@ -2,7 +2,6 @@ import { app, shell, BrowserWindow, ipcMain, globalShortcut, dialog } from 'elec
import { join } from 'path';
import { electronApp, optimizer, is } from '@electron-toolkit/utils';
import icon from '../../resources/icon.png?asset';
// const fs = require('fs');
//关闭exe命令
function killProcessByName(processName) {
@ -54,8 +53,8 @@ function createWindow() {
shell.openExternal(details.url);
return { action: 'deny' };
});
// HMR for renderer base on electron-vite cli.
// Load the remote URL for development or the local html file for production.
//基于electronic-vite-cli的渲染器HMR。
//加载用于开发的远程URL或用于生产的本地html文件。
if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL']);
} else {
@ -92,8 +91,8 @@ if (!gotTheLock) {
});
//当Electron完成时将调用此方法
//初始化并准备创建浏览器窗口。
//某些API只能在此事件发生后使用。
//初始化并准备创建浏览器窗口。
//某些API只能在此事件发生后使用。
const { spawn } = require('child_process');
let child = null;
let exePluginsExeList = [];
@ -146,9 +145,9 @@ if (!gotTheLock) {
// Set app user model id for windows
electronApp.setAppUserModelId('com.electron');
// Default open or close DevTools by F12 in development
// and ignore CommandOrControl + R in production.
// see https://github.com/alex8088/electron-toolkit/tree/master/packages/utils
//开发中默认按F12打开或关闭DevTools
//在生产中忽略CommandOrControl+R。
//看https://github.com/alex8088/electron-toolkit/tree/master/packages/utils
app.on('browser-window-created', (_, window) => {
optimizer.watchWindowShortcuts(window);
});
@ -159,8 +158,8 @@ if (!gotTheLock) {
myWindow = createWindow();
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
//在macOS上当出现以下情况时通常会在应用程序中重新创建窗口
//单击dock图标后没有其他打开的窗口。
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
@ -203,9 +202,9 @@ if (!gotTheLock) {
// });
// });
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
//关闭所有窗口后退出macOS除外。在那里这很常见
//让应用程序及其菜单栏保持活动状态,直到用户退出
//显式使用Cmd+Q。
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
// 循环关闭插件 EXE 列表
@ -221,9 +220,10 @@ if (!gotTheLock) {
});
}
// In this file you can include the rest of your app"s specific main process
// code. You can also put them in separate files and require them here.
//在此文件中,您可以包含应用程序的其他特定主进程
//代码。您也可以将它们放在单独的文件中,并在此处要求它们。
//获取应用程序版本
ipcMain.handle('get-app-version', () => {
return app.getVersion();
});

View File

@ -1,5 +1,7 @@
import { contextBridge, ipcRenderer } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'
const fs = require('fs');
const path = require('path');
// Custom APIs for renderer
// 自定义的主进程方法API
@ -21,6 +23,20 @@ import { electronAPI } from '@electron-toolkit/preload'
// return app.getVersion(); // 返回当前应用版本(来自 package.json
// });
// 获取config.js的绝对路径
function getConfigPath() {
// __dirname 是 win-unpacked/resources/app
let configPath = path.resolve('./resources/app.asar.unpacked/resources/config/config.js');
//单独配置测试环境
if (process.env.NODE_ENV === 'development') {
configPath = path.resolve('./resources/config/config.js');
}
return configPath;
}
// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
@ -31,6 +47,15 @@ if (process.contextIsolated) {
...electronAPI,
getAppVersion: async () => {
return await ipcRenderer.invoke('get-app-version');
},
readConfig: () => {
const configPath = getConfigPath();
if (fs.existsSync(configPath)) {
// 用require动态加载确保每次都是最新内容
delete require.cache[require.resolve(configPath)];
return require(configPath);
}
return {};
}
})
// contextBridge.exposeInMainWorld('api', api)
@ -42,6 +67,15 @@ if (process.contextIsolated) {
...electronAPI,
getAppVersion: async () => {
return await ipcRenderer.invoke('get-app-version');
},
readConfig: () => {
const configPath = getConfigPath();
if (fs.existsSync(configPath)) {
// 用require动态加载确保每次都是最新内容
delete require.cache[require.resolve(configPath)];
return require(configPath);
}
return {};
}
}
// window.api = api

View File

@ -1,5 +1,6 @@
import { defineStore } from 'pinia'
import config from '@renderer/util/config.js'
// import config from '@renderer/util/config.js'
const config = window.electron.readConfig();
// 定义一个名为logWebSocketStore的store用于管理WebSocket连接的状态和操作
export const logWebSocketStore = defineStore('websocket', {

View File

@ -1,6 +1,7 @@
import { ElMessage as message } from 'element-plus'
// import storage from 'store'
import config from '@renderer/util/config.js'
// import config from '@renderer/util/config.js'
const config = window.electron.readConfig();
let receiveMessage = null
import { reactive } from 'vue'
const socket = reactive({

View File

@ -313,7 +313,9 @@ import { ElMessage, ElMessageBox } from 'element-plus';
import { throttle } from 'lodash-es';
import dayjs from 'dayjs';
import axios from 'axios';
import config from '@renderer/util/config.js';
// import config from '@renderer/util/config.js';
const config = window.electron.readConfig();
console.log("当前获取config",config);
import { logWebSocketStore } from '@renderer/stores/logWebSocket.js';
const webSocketStore = logWebSocketStore();
@ -647,6 +649,7 @@ onMounted(() => {
version.value = v;
});
}
// generateMockDevices();
getSchemeDefaultConf();
getSchemeDefaultProp();