From 000e45648b5f245bfca6498e131c85e1a085beb0 Mon Sep 17 00:00:00 2001
From: doudou201703 <84085676+doudou201703@users.noreply.github.com>
Date: Wed, 12 Aug 2026 20:40:44 +0800
Subject: [PATCH] xiao_002
---
electron.vite.config.ts | 3 +-
package.json | 2 +
src/main/index.ts | 27 ++--
src/main/{key.ts => 全局键盘监听.ts} | 0
src/main/监听电子秤.ts | 43 +++++
src/renderer/index.html | 2 +-
src/renderer/src/App.vue | 22 ++-
src/renderer/src/utils/request.ts | 4 +-
src/renderer/src/页面目录/学习页面/学习页.vue | 25 ++-
src/renderer/src/页面目录/授权页面/授权页.vue | 7 +-
.../src/页面目录/登陆页面/登陆组件.vue | 3 +-
src/renderer/src/页面目录/登陆页面/登陆页.vue | 2 +-
yarn.lock | 153 +++++++++++++++++-
13 files changed, 251 insertions(+), 42 deletions(-)
rename src/main/{key.ts => 全局键盘监听.ts} (100%)
create mode 100644 src/main/监听电子秤.ts
diff --git a/electron.vite.config.ts b/electron.vite.config.ts
index aceabb2..9c4c924 100644
--- a/electron.vite.config.ts
+++ b/electron.vite.config.ts
@@ -18,6 +18,7 @@ export default defineConfig({
vue(),
AutoImport({
resolvers: [ElementPlusResolver()]
+
}),
Components({
resolvers: [ElementPlusResolver()]
@@ -27,7 +28,7 @@ export default defineConfig({
proxy: {
"/api": {
// target: "http://127.0.0.1:8000/", // 目标服务器地址
- target: "https://six.sixgou.com/", // 目标服务器地址
+ target: "https://xiao.sixgou.com/", // 目标服务器地址
changeOrigin: true, // 开启跨域
// 重写路径,将 /api/six_demo/demo 重写为 /six_demo/demo
rewrite: (path) => path.replace(/^\/api/, "")
diff --git a/package.json b/package.json
index 7726d14..3181d3f 100644
--- a/package.json
+++ b/package.json
@@ -31,8 +31,10 @@
"electron-updater": "^6.3.9",
"element-plus": "^2.14.2",
"node-global-key-listener": "^0.3.0",
+ "node-hid": "^3.4.0",
"pinia": "^3.0.4",
"pinia-plugin-persistedstate": "^4.7.1",
+ "serialport": "^13.0.0",
"unplugin-element-plus": "^0.11.2",
"vue-echarts": "^8.0.1",
"vue-next-wxlogin": "^1.0.4",
diff --git a/src/main/index.ts b/src/main/index.ts
index 8e4bf72..f3797e9 100644
--- a/src/main/index.ts
+++ b/src/main/index.ts
@@ -2,8 +2,7 @@ import { app, BrowserWindow, ipcMain, screen, shell } from "electron";
import { join } from "path";
import { electronApp, is, optimizer } from "@electron-toolkit/utils";
import icon from "../../resources/icon.png?asset";
-
-import { initGlobalScanner } from "./key";
+import { OninitScannerSerialPort监听电子秤 } from "./监听电子秤";
//
@@ -32,11 +31,15 @@ app.on("second-instance", (_, argv) => {
const url = argv.find(arg => arg.startsWith("myapp://"));
if (url) {
console.log("✅ Windows/Linux 收到 myapp:", url);
+ let uri = new URL(url);
+ const paramsObj = Object.fromEntries(uri.searchParams.entries());
+ if (mainWindow && mainWindow.webContents) {
+ mainWindow.webContents.send("wx", paramsObj);
+ }
}
});
function createWindow(windowWidth: number = 1920, windowHeight: number = 997): void {
-
mainWindow = new BrowserWindow({
width: windowWidth, // 设置窗口宽度
height: windowHeight, //设置窗口高度
@@ -58,7 +61,8 @@ function createWindow(windowWidth: number = 1920, windowHeight: number = 997): v
webSecurity: true
}
});
- initGlobalScanner(mainWindow); // ★ 现在 mainWindow 已经有值了
+ // initScanner();
+ OninitScannerSerialPort监听电子秤(mainWindow);
// 可以打开,开发者工具
mainWindow.webContents.openDevTools();
// 关键:修改 CSP 允许连接 six.sixgou.com
@@ -76,7 +80,7 @@ function createWindow(windowWidth: number = 1920, windowHeight: number = 997): v
mainWindow.loadURL(process.env["ELECTRON_RENDERER_URL"]).then();
} else {
mainWindow.loadFile(join(__dirname, "../renderer/index.html")).then(() => {
- console.log("加 载成功了");
+ console.log("加载成功了");
}).catch((reason) => {
console.log("打包后的状态错误回调", reason);
});
@@ -136,14 +140,5 @@ app.on("window-all-closed", () => {
// app.quit(); // ✅ macOS / Windows 都退出
});
-//在这个文件里,你可以包含你应用的具体主要流程的其他部分
-// code. You can also put them in separate files and require them here.
-// app.on("browser-window-blur", (event) => {
-// console.log(event, "失去光标会触发的");
-// });
-// app.on("browser-window-focus", () => {
-// console.log("获取光标时得到光标");
-// });
-// app.on("did-resign-active", () => {
-// console.log("切换时的触发 实测,切换了别的app的会触发的事件");
-// });
+// 在应用入口处注册退出事件
+
diff --git a/src/main/key.ts b/src/main/全局键盘监听.ts
similarity index 100%
rename from src/main/key.ts
rename to src/main/全局键盘监听.ts
diff --git a/src/main/监听电子秤.ts b/src/main/监听电子秤.ts
new file mode 100644
index 0000000..d3bb2e0
--- /dev/null
+++ b/src/main/监听电子秤.ts
@@ -0,0 +1,43 @@
+import { DelimiterParser, SerialPort } from "serialport";
+import { BrowserWindow } from "electron";
+
+export function OninitScannerSerialPort监听电子秤(mainWindow: BrowserWindow) {
+ SerialPort.list().then((ports) => {
+ console.log(ports, typeof ports);
+ const scalePort = ports.find(p => {
+ return p.vendorId === "0403" && p.productId === "6001";
+ }
+ );
+ if (!scalePort) {
+ console.error("❌ 未找到电子秤串口");
+ return;
+ }
+
+ console.log("✅ 找到电子秤串口:", scalePort.path);
+ const port = new SerialPort({
+ // path: "/dev/tty.usbserial-A5069RR4",
+ path: scalePort.path, // ✅ 动态获取,自动适配各平台
+ baudRate: 9600
+ }, (res) => {
+ console.log(res, "为null时,说明没有报错");
+ });
+
+// 以 0x03 0x04 作为帧分隔符
+ const parser = port.pipe(new DelimiterParser({ delimiter: [0x03, 0x04] }));
+ // const parser = port.pipe(new DelimiterParser({ delimiter: [0x60] }));
+
+ parser.on("data", (frame) => {
+ const text = frame.toString("ascii").trim();
+ // 提取重量数值
+ const match = text.match(/[+-]?\d+\.\d+/);
+ if (match) {
+ mainWindow.webContents.send("电子秤", parseFloat(match[0]));
+ }
+ });
+
+ parser.on("error", (err) => {
+ console.error("❌ 串口错误:", err.message);
+ });
+
+ });
+}
diff --git a/src/renderer/index.html b/src/renderer/index.html
index a2344d1..efd42b8 100644
--- a/src/renderer/index.html
+++ b/src/renderer/index.html
@@ -14,7 +14,7 @@
img-src 'self' data: *;
font-src *;
frame-src 'self' https://open.weixin.qq.com https://*.open.weixin.qq.com;
- connect-src 'self' https://six.sixgou.com http://localhost:5173 https://res.wx.qq.com https://at.alicdn.com https://open.weixin.qq.com https://datav.aliyun.com ws://127.0.0.1:8000;
+ connect-src 'self' https://six.sixgou.com http://localhost:5173 https://res.wx.qq.com https://at.alicdn.com https://open.weixin.qq.com https://datav.aliyun.com ws://127.0.0.1:8000 https://xiao.sixgou.com;
"
/>
diff --git a/src/renderer/src/App.vue b/src/renderer/src/App.vue
index 8e23a9d..0370ccb 100644
--- a/src/renderer/src/App.vue
+++ b/src/renderer/src/App.vue
@@ -2,24 +2,22 @@
import { onMounted, ref } from "vue";
import { useRouter } from "vue-router";
- import { useMainXiaoStore } from "@renderer/stores";
- import { instance } from "@renderer/utils/request";
- let xiaoMain = useMainXiaoStore();
let loading = ref(false);
const router_path = useRouter();
// router_path.push({ name: "图表页" });
onMounted(() => {
// router_path.push({ name: "登陆页" });
- instance.post("https://six.sixgou.com/demo/xiaozizi/", {},
- { headers: { "Authorization": xiaoMain.weChatUserInfo?.token } }
- ).then((data) => {
- console.log(data);
- router_path.replace({ name: "学习页面" });
- }).catch((error) => {
- console.log("错误被捕获了", error);
- router_path.replace({ name: "登陆页" });
- });
+ router_path.replace({ name: "学习页面" });
+ // instance.post("https://six.sixgou.com/demo/xiaozizi/", {},
+ // { headers: { "Authorization": xiaoMain.weChatUserInfo?.token } }
+ // ).then((data) => {
+ // console.log(data);
+ // router_path.replace({ name: "学习页面" });
+ // }).catch((error) => {
+ // console.log("错误被捕获了", error);
+ // router_path.replace({ name: "登陆页" });
+ // });
});
diff --git a/src/renderer/src/utils/request.ts b/src/renderer/src/utils/request.ts
index ff929d3..29dae26 100644
--- a/src/renderer/src/utils/request.ts
+++ b/src/renderer/src/utils/request.ts
@@ -1,9 +1,9 @@
import axios from "axios";
// 判断是否为 Electron 生产环境
-console.log(import.meta.env.MODE);
+console.log(import.meta.env.MODE, "URL import.meta.env.PROD 这里的值,是生产环境,判断用的");
export const instance = axios.create({
// 开发环境用相对路径(走 Vite 代理)
// 生产环境用完整 URL import.meta.env.PROD 这里的值,是生产环境,判断用的
- baseURL: import.meta.env.PROD ? "https://six.sixgou.com" : "/api"
+ baseURL: import.meta.env.PROD ? "https://xiao.sixgou.com" : "/api"
});
diff --git a/src/renderer/src/页面目录/学习页面/学习页.vue b/src/renderer/src/页面目录/学习页面/学习页.vue
index fa8b020..8e4f8f8 100644
--- a/src/renderer/src/页面目录/学习页面/学习页.vue
+++ b/src/renderer/src/页面目录/学习页面/学习页.vue
@@ -1,17 +1,35 @@
@@ -21,12 +39,17 @@
向主进程发送ping事件
+
+ 测试请求
+
-
+
+ 电子秤重量{{ 电子秤重量 }}
+