微信小程序之生物識別
發表時間:2022-9-6
發布人:葵宇科技
瀏覽次數:65
今天閑來沒事,了解下生物識別。
生物識別有三個接口
1、wx.checkIsSupportSoterAuthentication 用來獲取本機支持的生物識別方式(人臉、指紋、聲紋)
2、wx.startSoterAuthentication 進行生物認證
3、wx.checkIsSoterEnrolledInDevice 檢測是否錄入生物信息
有興趣的童鞋可以拿代碼去玩玩試試看
wxml代碼
bindtap="checkIsFingerPrint">檢測是否可以指紋識別
bindtap="checkIsFacial">檢測是否可以人臉識別
bindtap="HaveFingerPrint">該設備是否錄入指紋
bindtap="FingerPrint">識別指紋
js代碼
Page({
/**
* 頁面的初始數據
*/
data: {
isfingerPrint : false, //可否使用指紋識別 默認false
isfacial: false, //可否使用人臉識別 默認false
},
/**
* 生命周期函數--監聽頁面加載
*/
onLoad: function (options) {
var that = this
//查看支持的生物認證 比如ios的指紋識別 安卓部分機器是不能用指紋識別的
wx.checkIsSupportSoterAuthentication({
success(res) {
for (var i in res.supportMode){
if (res.supportMode[i] == 'fingerPrint'){
console.log("支持指紋識別", res.supportMode[i]);
that.setData({
isfingerPrint : true
})
} else if (res.supportMode[i] == 'facial'){
console.log("支持人臉識別", res.supportMode[i]);
}
}
}
})
},
//是否可以指紋識別
checkIsFingerPrint:function(){
var boole = this.data.isfingerPrint
var txt = "不可以使用指紋識別"
if (boole) {
txt = "可以使用指紋識別"
}
show("提示",txt,false);
},
//是否可以人臉識別
checkIsFacial: function () {
var boole = this.data.isfacial
var txt = "不可以使用人臉識別"
if (boole){
txt = "可以使用人臉識別"
}
function SUCC() {
console.log("用戶點擊確定")
}
function FAIL() {
console.log("用戶點擊取消")
}
show("提示", txt, true,SUCC,FAIL);
},
//進行指紋識別
FingerPrint: function(){
wx.startSoterAuthentication({
requestAuthModes: ['fingerPrint'],
challenge: '123456',
authContent: '請用指紋',
success(res) {
console.log("識別成功",res)
show("提示", "識別成功", false);
},
fail(res){
console.log("識別失敗",res)
show("提示", "識別失敗", false);
}
})
},
//是否有指紋
HaveFingerPrint:function(){
wx.checkIsSoterEnrolledInDevice({
checkAuthMode: 'fingerPrint',
success(res) {
if (res.isEnrolled == 1){
show("提示", "有指紋", false);
} else if (res.isEnrolled == 0){
show("提示", "無指紋", false);
}
},
fail(res){
show("提示", "異常", fail);
}
})
}
})
/**
* 顯示提示信息
* tit 提示的標題
* msg 提示的內容
* q 是否有取消按鈕(布爾值)
* succ 用戶點擊確定的回調(非必須)
* fail 用戶點擊取消的回調(非必須)
*
*/
function show(tit,msg,q,succ,fail){
wx.showModal({
title: tit,
content: msg,
showCancel:q,
success: function (res) {
if (res.confirm) {
if (succ){
succ();
}
} else if (res.cancel) {
if (fail) {
fail();
}
}
}
})
}
tip: 1、如有遇到新問題,可以在下方留言或者加QQ群437729329 進行咨詢