绑定后,再次配对控制设备

This commit is contained in:
WindowBird 2025-11-20 16:37:14 +08:00
parent 50f7b329ab
commit b9f55dbacb
3 changed files with 86 additions and 16 deletions

View File

@ -124,3 +124,18 @@ export function bindNfcCard(data) {
showLoading: false,
});
}
/**
* 手机端读取卡号后上报
* @param {Object} data
* @param {string} data.cardId - 卡片唯一标识
* @returns {Promise}
*/
export function swipeNfcCard(params) {
return request({
url: "/bst/nfc/swipeCard",
method: "POST",
params,
showLoading: false,
});
}

View File

@ -90,9 +90,9 @@
import BaseBackground from "@/components/base-background/base-background.vue";
import CustomNavbar from "@/components/custom-navbar/custom-navbar.vue";
import { getRequestConfig, getToken } from "@/utils/request.js";
import { bindNfcCard } from "@/api/memorial/index.js";
import { bindNfcCard, swipeNfcCard } from "@/api/memorial/index.js";
const WS_PATH = "/ws/device";
const WS_PATH = "/ws/ws/device";
const FIXED_MAC = "111111111111";
export default {
@ -118,6 +118,7 @@ export default {
nfcSupported: false,
nfcEnabled: false,
nfcError: "",
reportingSwipe: false,
};
},
computed: {
@ -172,8 +173,8 @@ export default {
}
// baseUrl ws wss
const isHttps = baseUrl.startsWith("https://");
const protocol = isHttps ? "wss" : "ws";
const protocol = "wss";
const host = baseUrl.replace(/^https?:\/\//, "").replace(/\/$/, "");
// token mac
@ -558,16 +559,18 @@ export default {
payload.memorialId = this.unitId;
}
const res = await bindNfcCard(payload);
if (res && (res.code === 200 || res.status === 200)) {
uni.showToast({ title: res.msg || "绑定成功", icon: "success" });
setTimeout(() => {
uni.navigateBack({ delta: 1 });
}, 800);
} else {
uni.showToast({
title: (res && res.msg) || "绑定失败",
icon: "none",
});
const success = res && (res.code === 200 || res.status === 200);
const message = (res && res.msg) || "";
const displayMessage = message || (success ? "读取成功" : "读取失败");
this.lastMessage = displayMessage;
const toastDuration = success ? 2600 : 2600;
await this.showStableToast({
title: displayMessage,
icon: success ? "success" : "none",
duration: toastDuration,
});
if (success) {
uni.navigateBack({ delta: 1 });
}
} catch (error) {
console.error("提交绑定失败", error);
@ -748,6 +751,7 @@ export default {
duration: 1500,
});
console.log("NFC 读取到的卡号:", cardNo);
this.reportSwipeCard(cardNo);
} else {
console.warn("无法从 NFC 标签中提取卡号:", res);
uni.showToast({
@ -784,6 +788,57 @@ export default {
}
this.nfcEnabled = false;
},
async showStableToast(options = {}) {
const { duration = 2000, ...rest } = options;
return new Promise((resolve) => {
uni.showToast({
duration,
...rest,
complete: () => {
setTimeout(resolve, duration);
},
});
});
},
async reportSwipeCard(cardNo) {
if (!cardNo || this.reportingSwipe) {
return;
}
this.reportingSwipe = true;
uni.showLoading({
title: "处理中...",
mask: true,
});
try {
const res = await swipeNfcCard({ cardId: cardNo });
if (res && res.code === 200) {
const message = (res && res.msg) || "";
const displayMessage = message || "读取成功";
this.lastMessage = displayMessage;
await this.showStableToast({
title: displayMessage,
icon: "success",
duration: 2600,
});
} else {
await this.showStableToast({
title: (res && res.msg) || "操作失败",
icon: "none",
duration: 2600,
});
}
} catch (error) {
console.error("NFC 卡号上报失败:", error);
uni.showToast({
title: "请求失败,请重试",
icon: "none",
duration: 2000,
});
} finally {
this.reportingSwipe = false;
uni.hideLoading();
}
},
},
};
</script>

View File

@ -6,8 +6,8 @@ import debounce from "uview-ui/libs/function/debounce";
const ENV_CONFIG = {
release: {
// 正式版
baseUrl: "http://192.168.1.4:4501",
// baseUrl: "https://tech-ape.top/prod-api",
// baseUrl: "http://192.168.1.4:4501",
baseUrl: "https://tech-ape.top/prod-api",
// baseUrl: "https://tech-ape.top/prod-api",
appId: 1,
},