uniapp使用指紋識別功能,一般有2種方式:官方api驗證或用plus.fingerprint進行驗證,下面分別講解一下。
使用指紋識別的基礎是需要開啟指紋識別的權限:首先需要獲取得到權限,在 manifest.json文件中配置
一、官方生物認證API
官方生物認證說明:https://uniapp.dcloud.io/api/system/authentication
實現方式:
第一步:uni.checkIsSupportSoterAuthentication:
獲取本機支持認證方式,res.supportMode = ['fingerPrint']
只支持指紋識別,res.supportMode = ['fingerPrint', 'facial']
支持指紋識別和人臉識別。
第二步: uni.checkIsSoterEnrolledInDevice
: 獲取設備內是否錄入指紋信息
第三步:uni.startSoterAuthentication
開始 SOTER 生物認證
具體實現代碼及注釋如下:
<template>
<view>
<view>{{ result }}</view>
</view>
</template>
<script> export default { data() { return { result: '' } }, onLoad() { this.checkIsSupportSoterAuthentication() }, methods: { /** * uni.checkIsSupportSoterAuthentication: 獲取本機支持認證方式( * res.supportMode = ['fingerPrint'] 只支持指紋識別 * res.supportMode = [] 不具備任何被SOTER支持的生物識別方式 * res.supportMode = ['fingerPrint', 'facial'] 支持指紋識別和人臉識別 * ) * 需求: 當前業務只要求指紋識別功能,(如你的業務中需要人臉識別,此方法也可以驗證) * */ checkIsSupportSoterAuthentication(){ // #ifdef APP-PLUS || MP-WEIXIN
uni.checkIsSupportSoterAuthentication({ success(res) { console.log(res); // 如果當前設備支持生物識別方式,且支持指紋識別方式
if(res.supportMode && res.supportMode.includes('fingerPrint')){ /** * uni.checkIsSoterEnrolledInDevice : 獲取設備內是否錄入指紋信息 * checkAuthMode: 'fingerPrint', // 檢驗指紋信息 * */ uni.checkIsSoterEnrolledInDevice({ checkAuthMode: 'fingerPrint', // 檢驗指紋信息
success(res) { console.log(res.isEnrolled) if(res.isEnrolled == true){ /** * 開始 SOTER 生物認證 * 執行成功,進行后續操作 * */ uni.startSoterAuthentication({ requestAuthModes: ['fingerPrint'], challenge: '123456', authContent: '請用指紋解鎖', success(res) { console.log(res); uni.showToast({ title: "識別成功", duration: 5000, icon:'none' }) //指紋識別成功后,進行后續工作
}, fail(err) { console.log(err,'66666666666666666'); }, complete(res) { console.log(res); } }) }else{ this.result = '此設備未錄入指紋,請到設置中開啟'; } }, fail(err) { uni.showModal({ title:'溫馨提示', content:'此設備未錄入指紋,請到設置中開啟', showCancel: false, success:function(res){ // 進行后續邏輯
} }) } }) } else{ this.result = "此設備不支持指紋識別功能" } }, fail(err) { uni.showModal({ title:'溫馨提示', content:'此設備不支持指紋識別功能', showCancel: false, success:function(res){ // 進行后續邏輯
} }) } }) // #endif // #ifndef APP-PLUS || MP-WEIXIN
this.result = '此平台不支持指紋識別'; // #endif
} } } </script>
其實比較簡單,就是獲取生物認證方式,判斷是否有指紋識別,再進行指紋識別即可。
二、使用Fingerprint模塊管理指紋識別
使用插件指紋模板:https://ext.dcloud.net.cn/plugin?id=358,Fingerprint模塊管理指紋識別,要使用指紋識別功能需要具備條件:
- 確認當前設備環境是否支持指紋識別,
- 當前設備是否設置密碼鎖屏,
- 當前設備是否已經錄入指紋。
(Android平台6.0及以上系統支持,只識別標准Android的指紋API,僅適配Google官方指紋識別的標准接口的設備)
以上條件都要滿足才可以使用識別功能。識別功能指的是與手機中已錄入的指紋進行比對檢測,也就是說,只要與手機中錄入任意指紋比對成功,便可進入成功回調。
因為目前市場上還是有很多設備不支持指紋,所以要先使用 plus.fingerprint.isSupport() 方法判斷(以下三個方法均返回值為Boolean類型)
// #ifdef APP-PLUS
if (!plus.fingerprint.isSupport()) { plus.nativeUI.toast('此設備不支持指紋識別'); console.log('此設備不支持指紋識別') } // #endif
再使用plus.fingerprint.isKeyguardSecure() 判斷是否開啟密碼鎖屏
// #ifdef APP-PLUS
if (!plus.fingerprint.isKeyguardSecure()) { plus.nativeUI.toast('此設備未設置密碼鎖屏'); console.log('此設備未設置密碼鎖屏') } // #endif
然后使用 plus.fingerprint.isEnrolledFingerprints() 判斷是否錄入指紋
// #ifdef APP-PLUS
if (!plus.fingerprint.isEnrolledFingerprints()) { plus.nativeUI.toast('此設備未錄入指紋'); console.log('此設備未錄入指紋') } // #endif
指紋識別方法代碼:
<template>
<view>
<button type="primary" @tap="fingerprint()" :disabled="disabled">按下開始識別指紋</button>
<view style="width: 100%;text-align: center;"> {{result}} </view>
</view>
</template>
<script> export default { data() { return { result:"", disabled:true } }, onLoad() { // #ifdef APP-PLUS
if (!plus.fingerprint.isSupport()) { this.result = '此設備不支持指紋識別'; this.disabled = true; } else if (!plus.fingerprint.isKeyguardSecure()) { this.result = '此設備未設置密碼鎖屏,無法使用指紋識別'; this.disabled = true; } else if (!plus.fingerprint.isEnrolledFingerprints()) { this.result = '此設備未錄入指紋,請到設置中開啟'; this.disabled = true; } else { this.result = '此設備支持指紋識別'; this.disabled = false; } // #endif // #ifdef MP-WEIXIN
this.disabled = false; this.result = '請在微信真機中使用,模擬器不支持'; // #endif // #ifndef APP-PLUS || MP-WEIXIN
this.result = '此平台不支持指紋識別'; // #endif
}, methods: { fingerprint: function() { // #ifdef APP-PLUS
plus.fingerprint.authenticate(function() { plus.nativeUI.closeWaiting(); //兼容Android平台關閉等待框
plus.nativeUI.alert('指紋識別成功'); }, function(e) { switch (e.code) { case e.AUTHENTICATE_MISMATCH: plus.nativeUI.toast('指紋匹配失敗,請重新輸入'); break; case e.AUTHENTICATE_OVERLIMIT: plus.nativeUI.closeWaiting(); //兼容Android平台關閉等待框
plus.nativeUI.alert('指紋識別失敗次數超出限制,請使用其它方式進行認證'); break; case e.CANCEL: plus.nativeUI.toast('已取消識別'); break; default: plus.nativeUI.closeWaiting(); //兼容Android平台關閉等待框
plus.nativeUI.alert('指紋識別失敗,請重試'); break; } }); // Android平台手動彈出等待提示框
if ('Android' == plus.os.name) { plus.nativeUI.showWaiting('指紋識別中...').onclose = function(){ plus.fingerprint.cancel(); } } // #endif // #ifdef MP-WEIXIN
wx.startSoterAuthentication({ requestAuthModes: ['fingerPrint'], challenge: '123456', authContent: '請用指紋解鎖', success(res) { uni.showToast({ title: '識別成功', mask: false, duration: 1500 }); } }) // #endif
}, } } </script>