首先Dcloud创建云服务空间,开启一键登录并充值
下一步
1. 右键项目 》 创建uniCloud云开发环境 》右键uniCloud》关联云服务空间
2. cloudfunctions右键 新建云函数,任意命名(例:veify),然后右键项目》管理公告模块或扩展库依赖》选择
3.点击 云函数veify》目录下的 “ index.js ” 改名 “ index.obj.js ” 否则获取不到
3.1 index.obj.js 内代码如下
'use strict' ;
module.exports = { // __before调用其他方法前会先调用这个方法_before( context) { // 创建实人认证实例this.frvManager = uniCloud.getFacialRecognitionVerifyManager( { requestId: this.getUniCloudRequestId( ) } ) } ,async getCertifyId( { realName, idCard, metaInfo } ) { // 会先调用_beforetry { const result = await this.frvManager.getCertifyId( { realName, idCard, metaInfo } ) ; return result} catch ( e) { throw { errCode: 500 , message: '获取认证失败,请检查信息的正确性' } ; } } ,// 获取认证的结果async getAuthResult( certifyId) { try { const result = await this.frvManager.getAuthResult( { certifyId } ) ; return result} catch ( e) { throw { errCode: 500 , message: '认证失败' } ; } }
}
3.2 打包自定义包测试
4.在页面上获取云函数
// 注册实人认证
// veify为云函数文件名const uniCloudVerify = uniCloud.importObject( 'veify' ) ;
5,封装认证代码
//点击认证
submit ( ) { let data = { realName:this.params.realName,idCard:this.params.idNumber} this.uniCloudVerifyFunction( data) .then( res = > { console.log( '实名认证yes' ,res) ; if( res.authState == 'SUCCESS' ) { //成功} // 提示成功} ) .catch( err = > { console.log( '实名认证no' ,err) ; // 提示失败} )
} ,
//封装
async uniCloudVerifyFunction( { realName, idCard } ) { // 获取设备信息const metaInfo = uni.getFacialRecognitionMetaInfo( ) ; // 获取CertifyIdconst certifyidResult = await uniCloudVerify.getCertifyId( { realName, idCard, metaInfo } ) if ( certifyidResult.errCode != 0 ) return Promise.reject( { code: 500 , msg: '获取certifyid失败,请联系管理员处理!' } ) const certifyId = certifyidResult.certifyId; // 开始认证return new Promise(( resolve, reject) = > { uni.startFacialRecognitionVerify( { certifyId: certifyId,success: async res = > { console.log( res) ; const result = await uniCloudVerify.getAuthResult( certifyId) ; resolve( result) ; } ,fail: err = > { reject( { code: err.errCode, msg: err.errMsg } ) } } ) ; } )
}
完成
下面是我的demo,供参考
< template> < view> < view class = "navBbutPart" > < button class = "butsty font30" hover-class= "butstyHover" @click= "$u .throttle(submit, 1000)" > 人脸验证 < /button> < /view> < /view>
< /template> < script> import { accountAuthenticationedit,accountAuthenticationadd } from "@/api/all.js" // 注册实人认证const uniCloudVerify = uniCloud.importObject( 'getFaceRecognition' ) ; export default { name:"getBack" ,props:{ params:{ } ,//是否修改edit:{ default:false} } ,data ( ) { return { } ; } ,onLoad ( ) { // console.log( uni.getFacialRecognitionMetaInfo( )) ; } ,methods:{ async uniCloudVerifyFunction( { realName, idCard } ) { // 获取设备信息const metaInfo = uni.getFacialRecognitionMetaInfo( ) ; // 获取CertifyIdconst certifyidResult = await uniCloudVerify.getCertifyId( { realName, idCard, metaInfo } ) if ( certifyidResult.errCode != 0 ) return Promise.reject( { code: 500 , msg: '获取certifyid失败,请联系管理员处理!' } ) const certifyId = certifyidResult.certifyId; // 开始认证return new Promise(( resolve, reject) = > { uni.startFacialRecognitionVerify( { certifyId: certifyId,success: async res = > { console.log( res) ; const result = await uniCloudVerify.getAuthResult( certifyId) ; resolve( result) ; } ,fail: err = > { reject( { code: err.errCode, msg: err.errMsg } ) } } ) ; } ) } ,submit ( ) { let data = { realName:this.params.realName,idCard:this.params.idNumber} this.uniCloudVerifyFunction( data) .then( res = > { console.log( '实名认证yes' ,res) ; if( res.authState == 'SUCCESS' ) { if( ! this.edit) { accountAuthenticationadd( this.params) .then( resapi= > { console.log( '实名认证接口' ,resapi) ; this.com.back( ) //返回} ) } else{ accountAuthenticationedit( this.params) .then( resapi= > { this.com.back( ) } ) } } // 提示成功} ) .catch( err = > { console.log( '实名认证no' ,err) ; // 提示失败} ) } } }
< /script> < style scoped lang = "scss" >
.navBbutPart{ width: 90 %; position: fixed; bottom: 200rpx; padding: 0 5 %; button{ border-radius: 50rpx; } }
< /style>