Quellcode durchsuchen

fix(1.3.1->1.3.2):构建mqtt访问地址时,考虑electron运行环境,兼容浏览器环境

dengdx vor 1 Monat
Ursprung
Commit
176e835b0c
2 geänderte Dateien mit 31 neuen und 1 gelöschten Zeilen
  1. 1 1
      package.json
  2. 30 0
      src/utils/hostConfig.ts

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "zsis",
-  "version": "1.3.1",
+  "version": "1.3.2",
   "private": true,
   "description": "医学成像系统",
   "main": "main.js",

+ 30 - 0
src/utils/hostConfig.ts

@@ -1,3 +1,33 @@
+function isElectron(): boolean {
+    // 方法1: 检查window.electronPrint API
+    if (typeof window !== 'undefined' && window.electronPrint) {
+      return true;
+    }
+
+    // 方法2: 检查process.versions.electron
+    if (
+      typeof process !== 'undefined' &&
+      process.versions &&
+      process.versions.electron
+    ) {
+      return true;
+    }
+
+    // 方法3: 检查navigator.userAgent
+    if (
+      typeof navigator !== 'undefined' &&
+      navigator.userAgent.toLowerCase().indexOf('electron') > -1
+    ) {
+      return true;
+    }
+
+    return false;
+  }
 export function getHostConfig() {
+  if (isElectron()) {
+    // Electron 环境(包括开发和生产打包后)
+    // 强制走本地 loopback,最可靠
+    return 'ws://127.0.0.1:8083/mqtt'
+  }
     return `ws://${window.location.host}:8083/mqtt`
 }