使用插件指紋模板:
https://ext.dcloud.net.cn/plugin?id=358
Fingerprint模塊管理指紋識別
要使用指紋識別功能需要具備條件:
- 確認當前設備環境是否支持指紋識別,
- 當前設備是否設置密碼鎖屏,
- 當前設備是否已經錄入指紋。
(Android平台6.0及以上系統支持,只識別標准Android的指紋API,僅適配Google官方指紋識別的標准接口的設備)
以上條件都要滿足才可以使用識別功能,識別功能,指的是與手機中已錄入的指紋進行比對檢測,也就是說,只要與手機中錄入任意指紋比對成功,便可進入成功回調。
首先需要獲取得到權限:
在 manifest.json文件中配置
因為目前市場上還是有很多設備不支持指紋,所以要先使用 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
因項目需要,這里我將指紋識別封裝為一個方法
fingerprint: function() { // #ifdef APP-PLUS plus.fingerprint.authenticate(function() { console.log('匹配成功'); }, function(e) { switch (e.code) { case e.AUTHENTICATE_MISMATCH: plus.nativeUI.toast('指紋匹配失敗,請重新輸入'); break; case e.AUTHENTICATE_OVERLIMIT: plus.nativeUI.closeWaiting(); //兼容Android平台關閉等待框 plus.nativeUI.toast('指紋識別失敗次數超出限制,請使用其它方式進行認證'); break; case e.CANCEL: plus.nativeUI.toast('已取消識別'); break; default: plus.nativeUI.closeWaiting(); //兼容Android平台關閉等待框 plus.nativeUI.toast('指紋識別失敗,請重試'); break; } }); // #endif },
注:回調里this指向發生改變,可在回調里使用其他方法