手机获取nfc-id数组中的数据
This commit is contained in:
parent
b51f87790f
commit
cb37e2355f
|
|
@ -19,9 +19,7 @@
|
||||||
</view>
|
</view>
|
||||||
<view v-if="nfcSupported" class="nfc-status">
|
<view v-if="nfcSupported" class="nfc-status">
|
||||||
<view class="nfc-status-header">
|
<view class="nfc-status-header">
|
||||||
<view
|
<view :class="['status-dot', nfcEnabled ? 'online' : 'offline']" />
|
||||||
:class="['status-dot', nfcEnabled ? 'online' : 'offline']"
|
|
||||||
/>
|
|
||||||
<text class="nfc-status-title">{{ nfcStatusText }}</text>
|
<text class="nfc-status-title">{{ nfcStatusText }}</text>
|
||||||
</view>
|
</view>
|
||||||
<view v-if="nfcError" class="nfc-error-text">
|
<view v-if="nfcError" class="nfc-error-text">
|
||||||
|
|
@ -609,7 +607,7 @@ export default {
|
||||||
this.nfcError = error.message || "初始化失败";
|
this.nfcError = error.message || "初始化失败";
|
||||||
}
|
}
|
||||||
// #endif
|
// #endif
|
||||||
|
|
||||||
// #ifndef MP-WEIXIN
|
// #ifndef MP-WEIXIN
|
||||||
// 非微信小程序环境,不支持 NFC
|
// 非微信小程序环境,不支持 NFC
|
||||||
console.warn("NFC 功能仅在微信小程序中支持");
|
console.warn("NFC 功能仅在微信小程序中支持");
|
||||||
|
|
@ -637,7 +635,7 @@ export default {
|
||||||
console.error("启动 NFC 发现失败:", err);
|
console.error("启动 NFC 发现失败:", err);
|
||||||
this.nfcEnabled = false;
|
this.nfcEnabled = false;
|
||||||
this.nfcError = err.errMsg || "启动失败";
|
this.nfcError = err.errMsg || "启动失败";
|
||||||
|
|
||||||
// 如果是权限问题,给出提示
|
// 如果是权限问题,给出提示
|
||||||
if (err.errMsg && err.errMsg.includes("permission")) {
|
if (err.errMsg && err.errMsg.includes("permission")) {
|
||||||
this.nfcError = "需要 NFC 权限,请在设置中开启";
|
this.nfcError = "需要 NFC 权限,请在设置中开启";
|
||||||
|
|
@ -655,7 +653,7 @@ export default {
|
||||||
},
|
},
|
||||||
handleNfcDiscovered(res) {
|
handleNfcDiscovered(res) {
|
||||||
console.log("发现 NFC 标签:", res);
|
console.log("发现 NFC 标签:", res);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let cardNo = "";
|
let cardNo = "";
|
||||||
|
|
||||||
|
|
@ -672,21 +670,22 @@ export default {
|
||||||
// 将 ArrayBuffer 转换为十六进制字符串
|
// 将 ArrayBuffer 转换为十六进制字符串
|
||||||
const uint8Array = new Uint8Array(res.id);
|
const uint8Array = new Uint8Array(res.id);
|
||||||
const bytes = Array.from(uint8Array);
|
const bytes = Array.from(uint8Array);
|
||||||
|
|
||||||
console.log("=== NFC 卡号提取详情 ===");
|
console.log("=== NFC 卡号提取详情 ===");
|
||||||
console.log("ArrayBuffer 字节长度:", res.id.byteLength);
|
console.log("ArrayBuffer 字节长度:", res.id.byteLength);
|
||||||
console.log("ArrayBuffer 字节数组(十进制):", bytes);
|
console.log("ArrayBuffer 字节数组(十进制):", bytes);
|
||||||
console.log("ArrayBuffer 字节数组(十六进制):", bytes.map(b => "0x" + b.toString(16).padStart(2, "0").toUpperCase()));
|
console.log(
|
||||||
|
"ArrayBuffer 字节数组(十六进制):",
|
||||||
// 转换为十六进制字符串(每字节两位,不足补0)
|
bytes.map(
|
||||||
|
(b) => "0x" + b.toString(16).padStart(2, "0").toUpperCase(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
// 转换为十六进制字符串(每字节两位,不足不补0)
|
||||||
// NFC 卡号通常按照字节顺序(大端序)存储
|
// NFC 卡号通常按照字节顺序(大端序)存储
|
||||||
cardNo = bytes
|
cardNo = bytes.join("").toUpperCase();
|
||||||
.join("")
|
|
||||||
.toUpperCase();
|
console.log("转换后的卡号(十进制字符串):", cardNo);
|
||||||
|
|
||||||
// 如果字节数组是 [0, 0, 5, 32],则卡号为 "00000520"
|
|
||||||
// 如果字节数组是 [32, 5, 0, 0],则卡号为 "20050000"
|
|
||||||
console.log("转换后的卡号(十六进制字符串):", cardNo);
|
|
||||||
console.log("卡号长度:", cardNo.length, "字符");
|
console.log("卡号长度:", cardNo.length, "字符");
|
||||||
console.log("========================");
|
console.log("========================");
|
||||||
} else if (typeof res.id === "string") {
|
} else if (typeof res.id === "string") {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user