feat(config): 动态加载配置文件并支持远程和本地路径
- 在 preload 中添加 readConfig函数,动态加载 config.js - 修改 logWebSocket 和 socket 以使用动态加载的配置 - 更新 calibration 页面以使用新配置 - 添加资源目录下的 config.js 文件,包含不同环境的 API URL
This commit is contained in:
parent
530033e2b7
commit
67b005f77c
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "calibration-pc",
|
"name": "calibration-pc",
|
||||||
"version": "1.0.0",
|
"version": "1.0.2",
|
||||||
"description": "谷云开发部开发的断路器标定软件",
|
"description": "谷云开发部开发的断路器标定软件",
|
||||||
"main": "./out/main/index.js",
|
"main": "./out/main/index.js",
|
||||||
"author": "example.com",
|
"author": "example.com",
|
||||||
|
|
|
@ -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',
|
||||||
|
}
|
|
@ -2,7 +2,6 @@ import { app, shell, BrowserWindow, ipcMain, globalShortcut, dialog } from 'elec
|
||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
import { electronApp, optimizer, is } from '@electron-toolkit/utils';
|
import { electronApp, optimizer, is } from '@electron-toolkit/utils';
|
||||||
import icon from '../../resources/icon.png?asset';
|
import icon from '../../resources/icon.png?asset';
|
||||||
// const fs = require('fs');
|
|
||||||
|
|
||||||
//关闭exe命令
|
//关闭exe命令
|
||||||
function killProcessByName(processName) {
|
function killProcessByName(processName) {
|
||||||
|
@ -54,8 +53,8 @@ function createWindow() {
|
||||||
shell.openExternal(details.url);
|
shell.openExternal(details.url);
|
||||||
return { action: 'deny' };
|
return { action: 'deny' };
|
||||||
});
|
});
|
||||||
// HMR for renderer base on electron-vite cli.
|
//基于electronic-vite-cli的渲染器HMR。
|
||||||
// Load the remote URL for development or the local html file for production.
|
//加载用于开发的远程URL或用于生产的本地html文件。
|
||||||
if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
|
if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
|
||||||
mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL']);
|
mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL']);
|
||||||
} else {
|
} else {
|
||||||
|
@ -146,9 +145,9 @@ if (!gotTheLock) {
|
||||||
// Set app user model id for windows
|
// Set app user model id for windows
|
||||||
electronApp.setAppUserModelId('com.electron');
|
electronApp.setAppUserModelId('com.electron');
|
||||||
|
|
||||||
// Default open or close DevTools by F12 in development
|
//开发中默认按F12打开或关闭DevTools
|
||||||
// and ignore CommandOrControl + R in production.
|
//在生产中忽略CommandOrControl+R。
|
||||||
// see https://github.com/alex8088/electron-toolkit/tree/master/packages/utils
|
//看https://github.com/alex8088/electron-toolkit/tree/master/packages/utils
|
||||||
app.on('browser-window-created', (_, window) => {
|
app.on('browser-window-created', (_, window) => {
|
||||||
optimizer.watchWindowShortcuts(window);
|
optimizer.watchWindowShortcuts(window);
|
||||||
});
|
});
|
||||||
|
@ -159,8 +158,8 @@ if (!gotTheLock) {
|
||||||
myWindow = createWindow();
|
myWindow = createWindow();
|
||||||
|
|
||||||
app.on('activate', function () {
|
app.on('activate', function () {
|
||||||
// On macOS it's common to re-create a window in the app when the
|
//在macOS上,当出现以下情况时,通常会在应用程序中重新创建窗口
|
||||||
// dock icon is clicked and there are no other windows open.
|
//单击dock图标后,没有其他打开的窗口。
|
||||||
if (BrowserWindow.getAllWindows().length === 0) createWindow();
|
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
|
//关闭所有窗口后退出,macOS除外。在那里,这很常见
|
||||||
// for applications and their menu bar to stay active until the user quits
|
//让应用程序及其菜单栏保持活动状态,直到用户退出
|
||||||
// explicitly with Cmd + Q.
|
//显式使用Cmd+Q。
|
||||||
app.on('window-all-closed', () => {
|
app.on('window-all-closed', () => {
|
||||||
if (process.platform !== 'darwin') {
|
if (process.platform !== 'darwin') {
|
||||||
// 循环关闭插件 EXE 列表
|
// 循环关闭插件 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', () => {
|
ipcMain.handle('get-app-version', () => {
|
||||||
return app.getVersion();
|
return app.getVersion();
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
import { contextBridge, ipcRenderer } from 'electron'
|
import { contextBridge, ipcRenderer } from 'electron'
|
||||||
import { electronAPI } from '@electron-toolkit/preload'
|
import { electronAPI } from '@electron-toolkit/preload'
|
||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
// Custom APIs for renderer
|
// Custom APIs for renderer
|
||||||
// 自定义的主进程方法API
|
// 自定义的主进程方法API
|
||||||
|
@ -21,6 +23,20 @@ import { electronAPI } from '@electron-toolkit/preload'
|
||||||
// return app.getVersion(); // 返回当前应用版本(来自 package.json)
|
// 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
|
// Use `contextBridge` APIs to expose Electron APIs to
|
||||||
// renderer only if context isolation is enabled, otherwise
|
// renderer only if context isolation is enabled, otherwise
|
||||||
// just add to the DOM global.
|
// just add to the DOM global.
|
||||||
|
@ -31,6 +47,15 @@ if (process.contextIsolated) {
|
||||||
...electronAPI,
|
...electronAPI,
|
||||||
getAppVersion: async () => {
|
getAppVersion: async () => {
|
||||||
return await ipcRenderer.invoke('get-app-version');
|
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)
|
// contextBridge.exposeInMainWorld('api', api)
|
||||||
|
@ -42,6 +67,15 @@ if (process.contextIsolated) {
|
||||||
...electronAPI,
|
...electronAPI,
|
||||||
getAppVersion: async () => {
|
getAppVersion: async () => {
|
||||||
return await ipcRenderer.invoke('get-app-version');
|
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
|
// window.api = api
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import { defineStore } from 'pinia'
|
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连接的状态和操作
|
// 定义一个名为logWebSocketStore的store,用于管理WebSocket连接的状态和操作
|
||||||
export const logWebSocketStore = defineStore('websocket', {
|
export const logWebSocketStore = defineStore('websocket', {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import { ElMessage as message } from 'element-plus'
|
import { ElMessage as message } from 'element-plus'
|
||||||
// import storage from 'store'
|
// 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
|
let receiveMessage = null
|
||||||
import { reactive } from 'vue'
|
import { reactive } from 'vue'
|
||||||
const socket = reactive({
|
const socket = reactive({
|
||||||
|
|
|
@ -313,7 +313,9 @@ import { ElMessage, ElMessageBox } from 'element-plus';
|
||||||
import { throttle } from 'lodash-es';
|
import { throttle } from 'lodash-es';
|
||||||
import dayjs from 'dayjs';
|
import dayjs from 'dayjs';
|
||||||
import axios from 'axios';
|
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';
|
import { logWebSocketStore } from '@renderer/stores/logWebSocket.js';
|
||||||
const webSocketStore = logWebSocketStore();
|
const webSocketStore = logWebSocketStore();
|
||||||
|
@ -647,6 +649,7 @@ onMounted(() => {
|
||||||
version.value = v;
|
version.value = v;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// generateMockDevices();
|
// generateMockDevices();
|
||||||
getSchemeDefaultConf();
|
getSchemeDefaultConf();
|
||||||
getSchemeDefaultProp();
|
getSchemeDefaultProp();
|
||||||
|
|
Loading…
Reference in New Issue