diff --git a/package.json b/package.json index 7c7ee3a..5e92dc8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "calibration-pc", - "version": "1.0.0", + "version": "1.0.2", "description": "谷云开发部开发的断路器标定软件", "main": "./out/main/index.js", "author": "example.com", diff --git a/resources/config/config.js b/resources/config/config.js new file mode 100644 index 0000000..457a20a --- /dev/null +++ b/resources/config/config.js @@ -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', +} diff --git a/src/main/index.js b/src/main/index.js index 6ff2476..9e867cb 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -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(); }); diff --git a/src/preload/index.js b/src/preload/index.js index 1717c90..695c4e0 100644 --- a/src/preload/index.js +++ b/src/preload/index.js @@ -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 diff --git a/src/renderer/src/stores/logWebSocket.js b/src/renderer/src/stores/logWebSocket.js index d738c9e..4179a4b 100644 --- a/src/renderer/src/stores/logWebSocket.js +++ b/src/renderer/src/stores/logWebSocket.js @@ -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', { diff --git a/src/renderer/src/util/socket.js b/src/renderer/src/util/socket.js index 5c1c864..722a1fb 100644 --- a/src/renderer/src/util/socket.js +++ b/src/renderer/src/util/socket.js @@ -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({ diff --git a/src/renderer/src/views/calibration/index.vue b/src/renderer/src/views/calibration/index.vue index fd0ed59..3ce2d86 100644 --- a/src/renderer/src/views/calibration/index.vue +++ b/src/renderer/src/views/calibration/index.vue @@ -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();