From 2fcaa764c8aba5e8a80db41447817d248796343c Mon Sep 17 00:00:00 2001 From: WindowBird <13870814+windows-bird@user.noreply.gitee.com> Date: Thu, 20 Nov 2025 11:40:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0nfc=E9=85=8D=E5=AF=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/memorial/index.js | 2 +- pages/memorial/nfcPairing.vue | 102 +++++++++++++++++++--------------- 2 files changed, 59 insertions(+), 45 deletions(-) diff --git a/api/memorial/index.js b/api/memorial/index.js index f9b2cba..154ccf8 100644 --- a/api/memorial/index.js +++ b/api/memorial/index.js @@ -120,7 +120,7 @@ export function getDeceasedList(params) { * @returns {Promise} */ export function bindNfcCard(data) { - return post("/bst/nfc", data, { + return post("/bst/nfc/add", data, { showLoading: false, }); } diff --git a/pages/memorial/nfcPairing.vue b/pages/memorial/nfcPairing.vue index dadb0b5..53a35a3 100644 --- a/pages/memorial/nfcPairing.vue +++ b/pages/memorial/nfcPairing.vue @@ -27,23 +27,26 @@ 清空卡号 - 测试服务器 - - 设备 MAC 地址 - - + + + + + + + + + + NFC 卡号 { @@ -187,7 +190,7 @@ export default { } }, 10000); // 10秒超时 - this.socketTask = uni.connectSocket({ + this.socketTask = uni.connectSocket({ url, success: (res) => { console.log("uni.connectSocket success:", res); @@ -197,16 +200,16 @@ export default { clearTimeout(this.connectTimeout); this.connectionError = `连接失败: ${err.errMsg || "未知错误"}`; this.socketConnected = false; - } + }, }); - + if (!this.socketTask) { clearTimeout(this.connectTimeout); this.connectionError = "当前环境不支持WebSocket"; console.error("initSocket: socketTask 为 null"); return; } - + if (typeof this.socketTask.onOpen === "function") { console.log("使用 Task 级别事件绑定", this.socketTask); this.bindTaskSocketEvents(); @@ -226,7 +229,7 @@ export default { clearTimeout(this.connectTimeout); this.connectTimeout = null; } - + if (this.socketTask && typeof this.socketTask.close === "function") { try { this.socketTask.close(); @@ -276,31 +279,37 @@ export default { clearTimeout(this.connectTimeout); this.connectTimeout = null; } - + // 解析错误信息 let errorMsg = error.errMsg || error.message || "连接失败"; let userFriendlyMsg = "连接失败"; - + // 处理 Invalid HTTP status 错误 - if (errorMsg.includes("Invalid HTTP status") || error.errCode === 1004) { - userFriendlyMsg = "服务器不支持WebSocket或路径不存在\n请检查:\n1. 服务器是否正常运行\n2. WebSocket路径是否正确\n3. 服务器是否支持WebSocket协议"; + if ( + errorMsg.includes("Invalid HTTP status") || + error.errCode === 1004 + ) { + userFriendlyMsg = + "服务器不支持WebSocket或路径不存在\n请检查:\n1. 服务器是否正常运行\n2. WebSocket路径是否正确\n3. 服务器是否支持WebSocket协议"; console.error("WebSocket握手失败,可能原因:", { url: this.buildSocketUrl(), errorCode: error.errCode, errorMsg: errorMsg, - suggestion: "服务器可能返回了404或500错误,请检查服务器日志" + suggestion: "服务器可能返回了404或500错误,请检查服务器日志", }); } else if (errorMsg.includes("timeout")) { userFriendlyMsg = "连接超时,请检查网络连接"; } else if (errorMsg.includes("fail")) { userFriendlyMsg = "网络连接失败,请检查网络和服务器地址"; } - + this.connectionError = userFriendlyMsg; this.socketConnected = false; }); this.socketTask.onMessage((event) => { this.handleSocketMessage(event); + + console.log("WebSocket 接收到事件", event); }); }, bindGlobalSocketEvents() { @@ -334,26 +343,30 @@ export default { clearTimeout(this.connectTimeout); this.connectTimeout = null; } - + // 解析错误信息 let errorMsg = error.errMsg || error.message || "连接失败"; let userFriendlyMsg = "连接失败"; - + // 处理 Invalid HTTP status 错误 - if (errorMsg.includes("Invalid HTTP status") || error.errCode === 1004) { - userFriendlyMsg = "服务器不支持WebSocket或路径不存在\n请检查:\n1. 服务器是否正常运行\n2. WebSocket路径是否正确\n3. 服务器是否支持WebSocket协议"; + if ( + errorMsg.includes("Invalid HTTP status") || + error.errCode === 1004 + ) { + userFriendlyMsg = + "服务器不支持WebSocket或路径不存在\n请检查:\n1. 服务器是否正常运行\n2. WebSocket路径是否正确\n3. 服务器是否支持WebSocket协议"; console.error("WebSocket握手失败,可能原因:", { url: this.buildSocketUrl(), errorCode: error.errCode, errorMsg: errorMsg, - suggestion: "服务器可能返回了404或500错误,请检查服务器日志" + suggestion: "服务器可能返回了404或500错误,请检查服务器日志", }); } else if (errorMsg.includes("timeout")) { userFriendlyMsg = "连接超时,请检查网络连接"; } else if (errorMsg.includes("fail")) { userFriendlyMsg = "网络连接失败,请检查网络和服务器地址"; } - + this.connectionError = userFriendlyMsg; this.socketConnected = false; }, @@ -394,7 +407,8 @@ export default { this.lastMessage = parsed.msg || trimmed; return; } else { - this.cardNo = trimmed; + this.cardNo = trimmed.replace(/^['"]|['"]$/g, ""); + console.log("接收到的卡号", this.cardNo); this.lastMessage = "已接收卡号"; uni.showToast({ title: "收到卡号", icon: "success" }); return; @@ -426,7 +440,7 @@ export default { try { const { baseUrl } = getRequestConfig(); console.log("测试服务器连接:", baseUrl); - + const token = getToken(); if (!token) { uni.showModal({ @@ -436,17 +450,17 @@ export default { }); return; } - + // 测试 WebSocket 服务器是否可达 const isHttps = baseUrl.startsWith("https://"); const protocol = isHttps ? "wss" : "ws"; const host = baseUrl.replace(/^https?:\/\//, "").replace(/\/$/, ""); const testUrl = `${protocol}://${host}${WS_PATH}?token=${encodeURIComponent(token)}&mac=${FIXED_MAC}`; - + console.log("测试 WebSocket URL:", testUrl.replace(token, "***")); - + uni.showLoading({ title: "测试连接中...", mask: true }); - + // 尝试连接 WebSocket 来测试服务器 const testSocket = uni.connectSocket({ url: testUrl, @@ -463,7 +477,7 @@ export default { }); }, }); - + // 设置超时 const timeout = setTimeout(() => { testSocket.close(); @@ -474,7 +488,7 @@ export default { showCancel: false, }); }, 5000); - + testSocket.onOpen(() => { clearTimeout(timeout); testSocket.close(); @@ -485,7 +499,7 @@ export default { duration: 2000, }); }); - + testSocket.onError((err) => { clearTimeout(timeout); uni.hideLoading(); @@ -510,11 +524,11 @@ export default { uni.showLoading({ title: "提交中...", mask: true }); try { const payload = { - memorialMac: this.deviceMac, + // memorialMac: this.deviceMac, nfcMac: this.cardNo, }; if (this.unitId) { - payload.unitId = this.unitId; + payload.memorialId = this.unitId; } const res = await bindNfcCard(payload); if (res && (res.code === 200 || res.status === 200)) {