实现nfc配对

This commit is contained in:
WindowBird 2025-11-20 11:40:31 +08:00
parent 2a58e93f96
commit 2fcaa764c8
2 changed files with 59 additions and 45 deletions

View File

@ -120,7 +120,7 @@ export function getDeceasedList(params) {
* @returns {Promise} * @returns {Promise}
*/ */
export function bindNfcCard(data) { export function bindNfcCard(data) {
return post("/bst/nfc", data, { return post("/bst/nfc/add", data, {
showLoading: false, showLoading: false,
}); });
} }

View File

@ -27,23 +27,26 @@
<view v-if="cardNo" class="text-btn" @click="resetCard" <view v-if="cardNo" class="text-btn" @click="resetCard"
>清空卡号 >清空卡号
</view> </view>
<view v-if="connectionError" class="text-btn" @click="testServerConnection" <view
v-if="connectionError"
class="text-btn"
@click="testServerConnection"
>测试服务器 >测试服务器
</view> </view>
</view> </view>
</view> </view>
<view class="form-card"> <view class="form-card">
<view class="field"> <!-- <view class="field">-->
<text class="label">设备 MAC 地址</text> <!-- <text class="label">设备 MAC 地址</text>-->
<input <!-- <input-->
v-model.trim="deviceMac" <!-- v-model.trim="deviceMac"-->
class="input" <!-- class="input"-->
maxlength="32" <!-- maxlength="32"-->
placeholder="请输入设备 MAC" <!-- placeholder="请输入设备 MAC"-->
placeholder-class="placeholder" <!-- placeholder-class="placeholder"-->
/> <!-- />-->
</view> <!-- </view>-->
<view class="field"> <view class="field">
<text class="label">NFC 卡号</text> <text class="label">NFC 卡号</text>
<input <input
@ -102,7 +105,7 @@ export default {
}, },
computed: { computed: {
canSubmit() { canSubmit() {
return !!this.deviceMac && !!this.cardNo; return !!this.cardNo;
}, },
connectionText() { connectionText() {
if (this.connectionError) { if (this.connectionError) {
@ -197,7 +200,7 @@ export default {
clearTimeout(this.connectTimeout); clearTimeout(this.connectTimeout);
this.connectionError = `连接失败: ${err.errMsg || "未知错误"}`; this.connectionError = `连接失败: ${err.errMsg || "未知错误"}`;
this.socketConnected = false; this.socketConnected = false;
} },
}); });
if (!this.socketTask) { if (!this.socketTask) {
@ -282,13 +285,17 @@ export default {
let userFriendlyMsg = "连接失败"; let userFriendlyMsg = "连接失败";
// Invalid HTTP status // Invalid HTTP status
if (errorMsg.includes("Invalid HTTP status") || error.errCode === 1004) { if (
userFriendlyMsg = "服务器不支持WebSocket或路径不存在\n请检查\n1. 服务器是否正常运行\n2. WebSocket路径是否正确\n3. 服务器是否支持WebSocket协议"; errorMsg.includes("Invalid HTTP status") ||
error.errCode === 1004
) {
userFriendlyMsg =
"服务器不支持WebSocket或路径不存在\n请检查\n1. 服务器是否正常运行\n2. WebSocket路径是否正确\n3. 服务器是否支持WebSocket协议";
console.error("WebSocket握手失败可能原因", { console.error("WebSocket握手失败可能原因", {
url: this.buildSocketUrl(), url: this.buildSocketUrl(),
errorCode: error.errCode, errorCode: error.errCode,
errorMsg: errorMsg, errorMsg: errorMsg,
suggestion: "服务器可能返回了404或500错误请检查服务器日志" suggestion: "服务器可能返回了404或500错误请检查服务器日志",
}); });
} else if (errorMsg.includes("timeout")) { } else if (errorMsg.includes("timeout")) {
userFriendlyMsg = "连接超时,请检查网络连接"; userFriendlyMsg = "连接超时,请检查网络连接";
@ -301,6 +308,8 @@ export default {
}); });
this.socketTask.onMessage((event) => { this.socketTask.onMessage((event) => {
this.handleSocketMessage(event); this.handleSocketMessage(event);
console.log("WebSocket <UNK>接收到事件", event);
}); });
}, },
bindGlobalSocketEvents() { bindGlobalSocketEvents() {
@ -340,13 +349,17 @@ export default {
let userFriendlyMsg = "连接失败"; let userFriendlyMsg = "连接失败";
// Invalid HTTP status // Invalid HTTP status
if (errorMsg.includes("Invalid HTTP status") || error.errCode === 1004) { if (
userFriendlyMsg = "服务器不支持WebSocket或路径不存在\n请检查\n1. 服务器是否正常运行\n2. WebSocket路径是否正确\n3. 服务器是否支持WebSocket协议"; errorMsg.includes("Invalid HTTP status") ||
error.errCode === 1004
) {
userFriendlyMsg =
"服务器不支持WebSocket或路径不存在\n请检查\n1. 服务器是否正常运行\n2. WebSocket路径是否正确\n3. 服务器是否支持WebSocket协议";
console.error("WebSocket握手失败可能原因", { console.error("WebSocket握手失败可能原因", {
url: this.buildSocketUrl(), url: this.buildSocketUrl(),
errorCode: error.errCode, errorCode: error.errCode,
errorMsg: errorMsg, errorMsg: errorMsg,
suggestion: "服务器可能返回了404或500错误请检查服务器日志" suggestion: "服务器可能返回了404或500错误请检查服务器日志",
}); });
} else if (errorMsg.includes("timeout")) { } else if (errorMsg.includes("timeout")) {
userFriendlyMsg = "连接超时,请检查网络连接"; userFriendlyMsg = "连接超时,请检查网络连接";
@ -394,7 +407,8 @@ export default {
this.lastMessage = parsed.msg || trimmed; this.lastMessage = parsed.msg || trimmed;
return; return;
} else { } else {
this.cardNo = trimmed; this.cardNo = trimmed.replace(/^['"]|['"]$/g, "");
console.log("接收到的卡号", this.cardNo);
this.lastMessage = "已接收卡号"; this.lastMessage = "已接收卡号";
uni.showToast({ title: "收到卡号", icon: "success" }); uni.showToast({ title: "收到卡号", icon: "success" });
return; return;
@ -510,11 +524,11 @@ export default {
uni.showLoading({ title: "提交中...", mask: true }); uni.showLoading({ title: "提交中...", mask: true });
try { try {
const payload = { const payload = {
memorialMac: this.deviceMac, // memorialMac: this.deviceMac,
nfcMac: this.cardNo, nfcMac: this.cardNo,
}; };
if (this.unitId) { if (this.unitId) {
payload.unitId = this.unitId; payload.memorialId = this.unitId;
} }
const res = await bindNfcCard(payload); const res = await bindNfcCard(payload);
if (res && (res.code === 200 || res.status === 200)) { if (res && (res.code === 200 || res.status === 200)) {