首页 > 建站教程 > 前端框架 >  uni-app指纹识别正文

uni-app指纹识别

从HBuilderX 2.3.8起,uni-app已经统一了生物认证的实现,并且支持iOS App端的faceID首先需要获取得到权限。官方生物认证说明:https: //uniapp.dcloud.io/api/system/authentication。
在使用官方生物认证之前,需要在manifest.json文件中进行如下的配置:





实现方式:

uni.checkIsSupportSoterAuthentication: 获取本机支持认证方式,res.supportMode = ['fingerPrint'] 只支持指纹识别,res.supportMode = ['fingerPrint', 'facial'] 支持指纹识别和人脸识别。
uni.checkIsSoterEnrolledInDevice: 获取设备内是否录入指纹信息
uni.startSoterAuthentication:开始SOTER生物认证

具体实现代码及注释如下:
001<template>
002    <view>
003        <view>{{ result }}</view>
004    </view>
005 </template>
006 
007<script>
008export default {
009    data() {
010        return {
011            result: ''
012        }
013    },
014    onLoad() {
015        this.checkIsSupportSoterAuthentication()
016    },
017    methods: {
018        /**
019         * uni.checkIsSupportSoterAuthentication:  获取本机支持认证方式(
020         *         res.supportMode = ['fingerPrint'] 只支持指纹识别
021         *         res.supportMode = [] 不具备任何被SOTER支持的生物识别方式
022         *         res.supportMode = ['fingerPrint', 'facial'] 支持指纹识别和人脸识别
023         * )
024         * 需求:当前业务只要求指纹识别功能,(如你的业务中需要人脸识别,此方法也可以验证)
025         *
026        */
027        checkIsSupportSoterAuthentication() {
028            // #ifdef APP-PLUS || MP-WEIXIN
029            uni.checkIsSupportSoterAuthentication({
030                success(res) {
031                    console.log(res);
032                    // 如果当前设备支持生物识别方式,且支持指纹识别方式
033                    if (res.supportMode && res.supportMode.includes('fingerPrint')) {
034                        /**
035                         * uni.checkIsSoterEnrolledInDevice : 获取设备内是否录入指纹信息
036                         *  checkAuthMode: 'fingerPrint', // 检验指纹信息
037                         * */
038                        uni.checkIsSoterEnrolledInDevice({
039                            checkAuthMode: 'fingerPrint', // 检验指纹信息
040                            success(res) {
041                                console.log(res.isEnrolled)
042                                if (res.isEnrolled == true) {
043                                    /**
044                                     * 开始 SOTER 生物认证
045                                     * 执行成功,进行后续操作
046                                     * */
047                                    uni.startSoterAuthentication({
048                                        requestAuthModes: ['fingerPrint'],
049                                        challenge: '123456',
050                                        authContent: '请用指纹解锁',
051                                        success(res) {
052                                            console.log(res);
053                                            uni.showToast({
054                                                title: "识别成功",
055                                                duration: 5000,
056                                                icon: 'none'
057                                            })
058                                            //指纹识别成功后,进行后续工作
059                                        },
060                                        fail(err) {
061                                            console.log(err, '66666666666666666');
062                                        },
063                                        complete(res) {
064                                            console.log(res);
065                                        }
066                                    })
067                                } else {
068                                    this.result = '此设备未录入指纹,请到设置中开启';
069                                }
070                            },
071                            fail(err) {
072                                uni.showModal({
073                                    title: '温馨提示',
074                                    content: '此设备未录入指纹,请到设置中开启',
075                                    showCancel: false,
076                                    success: function(res) {
077                                        // 进行后续逻辑
078                                    }
079                                })
080                            }
081                        })
082                    } else {
083                        this.result = "此设备不支持指纹识别功能"
084                    }
085                },
086                fail(err) {
087                    uni.showModal({
088                        title: '温馨提示',
089                        content: '此设备不支持指纹识别功能',
090                        showCancel: false,
091                        success: function(res) {
092                            // 进行后续逻辑
093                        }
094                    })
095                }
096            })
097            // #endif
098 
099            // #ifndef APP-PLUS || MP-WEIXIN
100            this.result = '此平台不支持指纹识别';
101            // #endif
102        }
103    }
104}
105</script>