diff --git a/api/memorial/index.js b/api/memorial/index.js index 154ccf8..256abd2 100644 --- a/api/memorial/index.js +++ b/api/memorial/index.js @@ -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, + }); +} diff --git a/pages/memorial/nfcPairing.vue b/pages/memorial/nfcPairing.vue index 31b4f15..6862599 100644 --- a/pages/memorial/nfcPairing.vue +++ b/pages/memorial/nfcPairing.vue @@ -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(); + } + }, }, }; diff --git a/utils/request.js b/utils/request.js index 0310461..bfe54bd 100644 --- a/utils/request.js +++ b/utils/request.js @@ -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, },