let NFCAdapter = null;//NFCAdapter
let nowKey = '';
let mythis = null;
//读取 NFC 数据 uri 格式时,返回内容前缀会存在空字符(\u0000),
const removeNullCharacter = function(str) {
return str.replace(/\\u([0-9]|[a-fA-F])([0-9]|[a-fA-F])([0-9]|[a-fA-F])([0-9]|[a-fA-F])/g, '')
};
/**
* 字节对象转字符串
* @param {Object} arr
*/
const byteToString = function(arr) {
if (typeof arr === 'string') {
return arr;
}
var str = '',
_arr = arr;
for (var i = 0; i < _arr.length; i++) {
var one = _arr[i].toString(2),
v = one.match(/^1+?(?=0)/);
if (v && one.length == 8) {
var bytesLength = v[0].length;
var store = _arr[i].toString(2).slice(7 - bytesLength);
for (var st = 1; st < bytesLength; st++) {
store += _arr[st + i].toString(2).slice(2);
}
str += String.fromCharCode(parseInt(store, 2));
i += bytesLength - 1;
} else {
str += String.fromCharCode(_arr[i]);
}
}
return filterSpaces(str);
};
/**
* 格式化得到aid值
* @param {Object} buffer
*/
const ab2hex = function(buffer) {
var hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function(bit) {
return ('00' + bit.toString(16)).slice(-2);
}
);
return hexArr.join('');
};
function filterSpaces(str) {
return str.replace(/\s+/g, ''); // 使用正则表达式匹配空白字符,并替换为空字符串
}
export default {
components: {
CuCustom,
UploadFile,
Sigin,
},
data() {
return {
}
},
filters: {},
computed: {},
onLoad(opt) {
},
onShow() {
mythis = this;
},
mounted() {
if (NFCAdapter){
NFCAdapter.offDiscovered(res => {
console.log('mounted---->offDiscovered------->',res)
})
NFCAdapter.stopDiscovery(res => {
console.log('mounted---->stopDiscovery------->',res)
})
NFCAdapter = null
}
},
methods: {
devTestJumpNfc(){
//方便测试,所以在设置缓存键,值为1时,则绕过nfc验证
if (uni.getStorageSync("useNfc") == '1'){
this.nfcVerify = true;//验证通过,不使用nfc打开
}
return true
},
//打卡NFC识别--todo https://www.cnblogs.com/jopny/p/15309854.html
openNfcVerify() {
mythis.showNfcPopTips = true;
NFCAdapter = wx.getNFCAdapter();
console.log('NFCAdapter--->',NFCAdapter)
// 绑定监听 NFC 标签
NFCAdapter.onDiscovered(res => {
// 监听到数据进行返回 根据返回的数据在进行 处理
// console.log('绑定监听--->', res)
//参考 https://blog.csdn.net/qq_42230395/article/details/112770872
let aid = parseInt(ab2hex(res.id), 16);
console.log('aid--->',aid)
if (res.messages) {
let cordsArray = res.messages[0].records;
cordsArray.find(item => {
console.log('item--->',item)
let payload =byteToString(new Uint8Array(item.payload));
//场景一:zh123456,,文本内容其实是123456,前缀有zh代表中文
//场景二:123456,,文本内容无zh前缀
//
let id = byteToString(new Uint8Array(item.id));
let type = byteToString(new Uint8Array(item.type));
let txt = payload.split("zh");
console.log('txt--->',txt)
console.log('nowKey--->',nowKey)
if (txt.length>1){
if (nowKey != txt[1]) {
mythis.keyNum++;
mythis.$showToast('巡检点位不匹配,请检查');
} else {
setTimeout(() => {
mythis.nfcCheckSuccess(txt[1])
}, 50)
}
}else {
if (payload == nowKey){
setTimeout(()=>{
mythis.nfcCheckSuccess(payload)
},50)
}else {
mythis.$showToast('巡检点位不匹配,请检查');
}
}
console.log('-------开始读取标签-------')
console.log('payload--->',payload)
// console.log('nfcTxt--->',nfcTxt)
// console.log('id--->',id)
// console.log('type--->',type)
console.log('-------END-------')
});
}else {
console.log('no---->messages')
mythis.$showToast('设备识别为空,请检查');
}
})
// 开始监听
NFCAdapter.startDiscovery({
success(res) {
console.log('开始监听--->', res)
},
fail(err) {
console.log('NFC--->err:', err)
if (err.errCode == '13001'){
mythis.$showToast('系统NFC开关未打开,请检查');
mythis.tipsNfc = '系统NFC开关未打开,请检查'
}
if (err.errCode == '13000'){
mythis.$showToast('设备不支持NFC');
mythis.tipsNfc = '系设备不支持NFC'
}
}
})
},
nfcCheckSuccess(key){
//设置nfc状态
mythis.nfcVerify = true;
uni.showToast({
title: '识别成功',
//将值设置为 success 或者直接不用写icon这个参数
icon: 'success',
//显示持续时间为 2秒
duration: 2000
});
mythis.getSubInfoList(key);
mythis.closeNfcVerify();//关闭弹窗
return false;
},
//关闭nfc识别功能
closeNfcVerify(){
console.log('关闭closeNfcVerify--->',8888888)
mythis.keyNum++;
mythis.showNfcPopTips = false;
const listener = function (res) { console.log('关闭nfc功能--->',res) }
if (NFCAdapter){
NFCAdapter.offDiscovered(res => {
console.log('offDiscovered------->',res)
})
NFCAdapter.stopDiscovery(res => {
console.log('stopDiscovery------->',res)
})
}
NFCAdapter = null;
// this.$forceUpdate();
},
},
destroyed() {
if (NFCAdapter){
NFCAdapter.offDiscovered(res => {
console.log('destroyed--->offDiscovered------->',res)
})
NFCAdapter.stopDiscovery(res => {
console.log('destroyed--->stopDiscovery------->',res)
})
NFCAdapter = null
}
},
}
网友评论