uniapp uni实人认证

news/2024/12/22 0:08:19/

uni实人认证依赖 目前仅支持App平台。

h5端活体人脸检测,使用的是百度云的h5人脸实名认证

使用要求

1、app端

在使用前,请确保您已注册DCloud账号,并已完成实名认证。

然后需要按文档开通服务 业务开通 | uni-app官网

2、h5端

 在使用前,需要根据方案接入要求先准备好 人脸实名认证H5端方案

开发

// index.vue<template><view class="page"><button class="btn" @click="$u.throttle(livenessDetection, 1000)">开始刷脸验证</button></view>
</template><script>import { getVerifyToken, h5VerifyIdentifyReturn } from '@/api/user.js'import { HTTP_REQUEST_URL, H5_URL } from '@/config.js'import permision from '@/utils/js_sdk/permission.js'import store from '@/store'export default {data() {return {// 终端idorgId: 0,// 姓名legalPerson: '',// 身份证idCard: '',}},onLoad(options) {this.orgId = options.orgId || 0this.legalPerson = options.legalPerson || ''this.idCard = options.idCard || ''// #ifdef H5if (options.verify_result && options.verify_result == 1) {this.h5VerifyIdentifyReturn()}// #endif},methods: {// 检查是否开启摄像头权限checkPermissionCamera() {return new Promise(async (resolve) => {switch (uni.getSystemInfoSync().platform) {case 'android':const val = await permision.requestAndroidPermission('android.permission.CAMERA')if (val == 1) {// 已获取授权resolve()} else if (val == -1) {// 被永久拒绝授权uni.showModal({title: '温馨提示',content: '摄像头权限已经被您永久拒绝,请前往设置页面打开',showCancel: false,confirmText: '前往设置',success() {// 打开当前App的权限设置界面permision.gotoAppPermissionSetting()}})}breakcase 'ios':// 是不是第一次开启相机const iosFirstCamera = uni.getStorageSync('iosFirstCamera');if (iosFirstCamera) {// 判断iOS上是否给予摄像头权限,有权限返回true,否则返回falseconst val2 = await permision.judgeIosPermission('camera')if (val2) {resolve()} else {uni.showModal({title: '温馨提示',content: '摄像头权限已经被您拒绝,请前往设置页面打开',showCancel: false,confirmText: '前往设置',success() {// 打开当前App的权限设置界面permision.gotoAppPermissionSetting()}})}} else {uni.setStorageSync('iosFirstCamera', true);resolve()}break}})},// 活体认证async livenessDetection() {// #ifdef MP-WEIXINuni.$u.toast('请使用app进行刷脸验证')// #endif// #ifdef APP-PLUSthis.appAuth()// #endif// #ifdef H5await this.checkPermissionCamera()this.h5Auth()// #endif},// #ifdef H5// h5端实名认证async h5Auth() {uni.showLoading({title: '认证中',mask: true})// 获取verify_tokenconst res = await getVerifyToken()const verify_token = res.verify_tokenconst successUrl = encodeURIComponent(`${ H5_URL }/pages/register/faceDetect?verify_result=1`)const failedUrl = encodeURIComponent(`${ H5_URL }/pages/register/faceDetect?verify_result=2`)// 跳转到百度云实名认证H5页window.location.href = `https://brain.baidu.com/face/print/?token=${ verify_token }&successUrl=${ successUrl }&failedUrl=${ failedUrl }`uni.hideLoading()},// 活体检测成功后的处理async h5VerifyIdentifyReturn() {uni.showLoading({title: '获取认证结果',mask: true})await h5VerifyIdentifyReturn()this.successAfter()},// #endif// #ifdef APP-PLUS// app端实名认证appAuth() {uni.showLoading({title: '认证中',mask: true})// 1、获取实人认证设备信息const metaInfo = uni.getFacialRecognitionMetaInfo()// 2、提交姓名、身份证号、metaInfo到云函数获取certifyIduniCloud.callFunction({name: 'livenessDetection',data: {step: 1,metaInfo,legalPerson: this.legalPerson,idCard: this.idCard}}).then(res => {if (res.result.code == 500) return uni.$u.toast(res.result.msg)const certifyId = res.result.data// 3、云函数获取certifyId后,调起实人认证界面uni.startFacialRecognitionVerify({certifyId,// 刷脸圈的颜色 加上这个参数会在ios端会提示 errCode: 10013 刷脸失败// progressBarColor: '#4a9f8b;',// 认证界面UI朝向 竖屏screenOrientation: 'port',success: async (e) => {if (e.errCode != 0) return uni.$u.toast(e.errMsg)// 4、获取认证结果const certResult = await uniCloud.callFunction({name: 'livenessDetection',data: {step: 2,orgId: this.orgId,certifyId,serversUrl: HTTP_REQUEST_URL}})if (certResult.result.code == 200) {this.successAfter()} else {uni.hideLoading()uni.showModal({title: '认证失败',content: certResult.result.msg,showCancel: false})}},fail: (e) => {uni.$u.toast(e.errMsg)}})}).catch(err => {uni.$u.toast('请求云函数失败:' + JSON.stringify(err))})},// #endif// 认证成功后的处理successAfter() {// 审核状态变更为:待审核store.commit('user/set_userStatus', 1)uni.showToast({title: '认证成功',icon: 'success',mask: true})setTimeout(() => {uni.switchTab({url: '/pages/index/index'})}, 1200)}}}
</script><style lang="scss" scoped>.btn {width: 440rpx;height: 88rpx;background: $linear-color;border-radius: 44rpx;font-size: 32rpx;color: #FFFFFF;line-height: 44rpx;margin-top: 102rpx;display: flex;flex-direction: row;align-items: center;justify-content: center;}
</style>

这边也贴出页面中有 import 的几个文件,可以参考下

1、改成你们自己服务端的接口地址

// @/api/user.js// 获取verify_token(百度云实名认证)
export function getVerifyToken() {return uni.$u.http.get('/szg-ynf/api/app/userDetail/verifyIdentify')
}// 实名认证成功
export function h5VerifyIdentifyReturn() {return uni.$u.http.get('/szg-ynf/api/app/userDetail/h5VerifyIdentifyReturn')
}

 2、改成你们自己的接口地址

// @/config.jsmodule.exports = {// 请求域名HTTP_REQUEST_URL: 'https://xxx.xxx.com',// h5地址H5_URL: 'https://xxx.xxx.com/h5',
}

3、该文件可以直接复制去使用,不用修改。这也是uniapp官方封装的

// @/utils/js_sdk/permission.js/*** 本模块封装了Android、iOS的应用权限判断、打开应用权限设置界面、以及位置系统服务是否开启*/
var isIos
// #ifdef APP-PLUS
isIos = (plus.os.name == 'iOS')
// #endif
// #ifdef H5
// 通过webview打开的h5中也是有plus环境的
isIos = uni.getSystemInfoSync().platform == 'ios'
// #endif// 判断推送权限是否开启
function judgeIosPermissionPush() {var result = falsevar UIApplication = plus.ios.import('UIApplication')var app = UIApplication.sharedApplication()var enabledTypes = 0if (app.currentUserNotificationSettings) {var settings = app.currentUserNotificationSettings()enabledTypes = settings.plusGetAttribute('types')console.log('enabledTypes1:' + enabledTypes)if (enabledTypes == 0) {console.log('推送权限没有开启')} else {result = trueconsole.log('已经开启推送功能!')}plus.ios.deleteObject(settings)} else {enabledTypes = app.enabledRemoteNotificationTypes()if (enabledTypes == 0) {console.log('推送权限没有开启!')} else {result = trueconsole.log('已经开启推送功能!')}console.log('enabledTypes2:' + enabledTypes)}plus.ios.deleteObject(app)plus.ios.deleteObject(UIApplication)return result
}// 判断定位权限是否开启
function judgeIosPermissionLocation() {var result = falsevar cllocationManger = plus.ios.import('CLLocationManager')var status = cllocationManger.authorizationStatus()result = (status != 2)console.log('定位权限开启:' + result)// 以下代码判断了手机设备的定位是否关闭,推荐另行使用方法 checkSystemEnableLocation/* var enable = cllocationManger.locationServicesEnabled();var status = cllocationManger.authorizationStatus();console.log("enable:" + enable);console.log("status:" + status);if (enable && status != 2) {result = true;console.log("手机定位服务已开启且已授予定位权限");} else {console.log("手机系统的定位没有打开或未给予定位权限");} */plus.ios.deleteObject(cllocationManger)return result
}// 判断麦克风权限是否开启
function judgeIosPermissionRecord() {var result = falsevar avaudiosession = plus.ios.import('AVAudioSession')var avaudio = avaudiosession.sharedInstance()var permissionStatus = avaudio.recordPermission()console.log('permissionStatus:' + permissionStatus)if (permissionStatus == 1684369017 || permissionStatus == 1970168948) {console.log('麦克风权限没有开启')} else {result = trueconsole.log('麦克风权限已经开启')}plus.ios.deleteObject(avaudiosession)return result
}// 判断相机权限是否开启
function judgeIosPermissionCamera() {var result = falsevar AVCaptureDevice = plus.ios.import('AVCaptureDevice')var authStatus = AVCaptureDevice.authorizationStatusForMediaType('vide')console.log('authStatus:' + authStatus)if (authStatus == 3) {result = trueconsole.log('相机权限已经开启')} else {console.log('相机权限没有开启')}plus.ios.deleteObject(AVCaptureDevice)return result
}// 判断相册权限是否开启
function judgeIosPermissionPhotoLibrary() {var result = falsevar PHPhotoLibrary = plus.ios.import('PHPhotoLibrary')var authStatus = PHPhotoLibrary.authorizationStatus()console.log('authStatus:' + authStatus)if (authStatus == 3) {result = trueconsole.log('相册权限已经开启')} else {console.log('相册权限没有开启')}plus.ios.deleteObject(PHPhotoLibrary)return result
}// 判断通讯录权限是否开启
function judgeIosPermissionContact() {var result = falsevar CNContactStore = plus.ios.import('CNContactStore')var cnAuthStatus = CNContactStore.authorizationStatusForEntityType(0)if (cnAuthStatus == 3) {result = trueconsole.log('通讯录权限已经开启')} else {console.log('通讯录权限没有开启')}plus.ios.deleteObject(CNContactStore)return result
}// 判断日历权限是否开启
function judgeIosPermissionCalendar() {var result = falsevar EKEventStore = plus.ios.import('EKEventStore')var ekAuthStatus = EKEventStore.authorizationStatusForEntityType(0)if (ekAuthStatus == 3) {result = trueconsole.log('日历权限已经开启')} else {console.log('日历权限没有开启')}plus.ios.deleteObject(EKEventStore)return result
}// 判断备忘录权限是否开启
function judgeIosPermissionMemo() {var result = falsevar EKEventStore = plus.ios.import('EKEventStore')var ekAuthStatus = EKEventStore.authorizationStatusForEntityType(1)if (ekAuthStatus == 3) {result = trueconsole.log('备忘录权限已经开启')} else {console.log('备忘录权限没有开启')}plus.ios.deleteObject(EKEventStore)return result
}// Android权限查询
function requestAndroidPermission(permissionID) {return new Promise((resolve, reject) => {plus.android.requestPermissions([permissionID], // 理论上支持多个权限同时查询,但实际上本函数封装只处理了一个权限的情况。有需要的可自行扩展封装function(resultObj) {var result = 0for (var i = 0; i < resultObj.granted.length; i++) {var grantedPermission = resultObj.granted[i]console.log('已获取的权限:' + grantedPermission)result = 1}for (var i = 0; i < resultObj.deniedPresent.length; i++) {var deniedPresentPermission = resultObj.deniedPresent[i]console.log('拒绝本次申请的权限:' + deniedPresentPermission)result = 0}for (var i = 0; i < resultObj.deniedAlways.length; i++) {var deniedAlwaysPermission = resultObj.deniedAlways[i]console.log('永久拒绝申请的权限:' + deniedAlwaysPermission)result = -1}resolve(result)// 若所需权限被拒绝,则打开APP设置界面,可以在APP设置界面打开相应权限// if (result != 1) {// gotoAppPermissionSetting()// }},function(error) {console.log('申请权限错误:' + error.code + ' = ' + error.message)resolve({code: error.code,message: error.message})})})
}// 使用一个方法,根据参数判断权限
function judgeIosPermission(permissionID) {if (permissionID == 'location') {return judgeIosPermissionLocation()} else if (permissionID == 'camera') {return judgeIosPermissionCamera()} else if (permissionID == 'photoLibrary') {return judgeIosPermissionPhotoLibrary()} else if (permissionID == 'record') {return judgeIosPermissionRecord()} else if (permissionID == 'push') {return judgeIosPermissionPush()} else if (permissionID == 'contact') {return judgeIosPermissionContact()} else if (permissionID == 'calendar') {return judgeIosPermissionCalendar()} else if (permissionID == 'memo') {return judgeIosPermissionMemo()}return false
}// 跳转到**应用**的权限页面
function gotoAppPermissionSetting() {if (isIos) {var UIApplication = plus.ios.import('UIApplication')var application2 = UIApplication.sharedApplication()var NSURL2 = plus.ios.import('NSURL')// var setting2 = NSURL2.URLWithString("prefs:root=LOCATION_SERVICES");var setting2 = NSURL2.URLWithString('app-settings:')application2.openURL(setting2)plus.ios.deleteObject(setting2)plus.ios.deleteObject(NSURL2)plus.ios.deleteObject(application2)} else {// console.log(plus.device.vendor);var Intent = plus.android.importClass('android.content.Intent')var Settings = plus.android.importClass('android.provider.Settings')var Uri = plus.android.importClass('android.net.Uri')var mainActivity = plus.android.runtimeMainActivity()var intent = new Intent()intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)var uri = Uri.fromParts('package', mainActivity.getPackageName(), null)intent.setData(uri)mainActivity.startActivity(intent)}
}// 检查系统的设备服务是否开启
// var checkSystemEnableLocation = async function () {
function checkSystemEnableLocation() {if (isIos) {var result = falsevar cllocationManger = plus.ios.import('CLLocationManager')var result = cllocationManger.locationServicesEnabled()console.log('系统定位开启:' + result)plus.ios.deleteObject(cllocationManger)return result} else {var context = plus.android.importClass('android.content.Context')var locationManager = plus.android.importClass('android.location.LocationManager')var main = plus.android.runtimeMainActivity()var mainSvr = main.getSystemService(context.LOCATION_SERVICE)var result = mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)console.log('系统定位开启:' + result)return result}
}module.exports = {judgeIosPermission: judgeIosPermission,requestAndroidPermission: requestAndroidPermission,checkSystemEnableLocation: checkSystemEnableLocation,gotoAppPermissionSetting: gotoAppPermissionSetting
}

因为uniapp 的实人认证需要配合 uniCloud服务空间使用,所以这边贴上服务空间上云函数的代码

目录结构:

// index.js 云函数'use strict'
const crypto = require('crypto')
exports.main = async (event, context) => {// 获取实人认证实例const frvManager = await uniCloud.getFacialRecognitionVerifyManager({ requestId: context.requestId })if (event.step == 1) {try {/*** 获取certifyId* @return certifyId 认证id,用于客户端调用认证接口及云函数获取认证结果*/const result = await frvManager.getCertifyId({realName: event.legalPerson,idCard: event.idCard,metaInfo: event.metaInfo})if (result.errCode == 0) return { code: 200, data: result.certifyId, msg: '获取certifyId成功' }return { code: 500, data: '', msg: `获取certifyId失败:${ result.errMsg }` }} catch (err) {return { code: 500, data: '', msg: '请检查您填写的姓名与身份证号是否正确' }}} else {// 获取认证结果const result = await frvManager.getAuthResult({ certifyId: event.certifyId })// 认证失败if (result.authState == 'FAIL') return { code: 500, data: result.certifyId, msg: `认证失败:${ result.errMsg }` }// 认证成功// 这里改成你们服务端的认证成功后的接口地址...// 服务端的接口为了安全起见,所以进行了加密验签const url = event.serversUrl + '/szg-ynf/oauth/userDetail/verifyIdentify'// md5加密方式:orgId 时间戳 私钥const orgId = event.orgIdconst timestamp = new Date().getTime()const signKey = '2bba3152-0b82-bd8e-2afd-453b0d925ea4'const sign = crypto.createHash('md5').update(orgId + timestamp + signKey).digest('hex')const res = await uniCloud.httpclient.request(url, {method: 'POST',data: {orgId,timestamp,sign},contentType: 'json',dataType: 'json',rejectUnauthorized: false})return { code: res.data.code, data: res.data.data, msg: res.data.msg }}
}
// package.json 云函数{"name": "livenessDetection","dependencies": {},"extensions": {"uni-cloud-verify": {}}
}

http://www.ppmy.cn/news/837475.html

相关文章

Centos7安装SDWebui

Centos7安装SDWebui 1.nvidia显卡驱动安装 #查看显卡编号 lspci | grep -i vga#查询显卡型号 http://pci-ids.ucw.cz/mods/PC/10de?actionhelp?helppci#安装依赖包 yum install kernel-devel gcc -y #查看nouveau是否已禁用&#xff0c;如果有内容说明没有禁用 lsmod | gre…

官网下载idea历史版本

1.进入官网 官网下载地址&#xff1a;https://www.jetbrains.com.cn/idea/ 进入后是这样的&#xff0c;如下图&#xff1a; 2.点击【下载】按钮&#xff0c;进入下载页面 &#xff08;上图中的两个地方的【下载】按钮是一样的&#xff09;。 3.选择右下角的【其他版本】就可…

显卡超频 linux arch 3090

需要的命令&#xff1a; https://www.flathub.org/apps/details/com.leinardi.gwe 安装前 加入setup guider 把其他需求安装了&#xff0c; 提示 先安装 flatpak 并重启电脑。

「B站焊武帝」再出圈!孤身爆肝造CPU,软硬件全自研,可玩游戏,基础器件成本不到1000元

两年时间&#xff0c;一个90后体制内小哥下班之后只干三件私务&#xff0c;那就是&#xff1a; 手搓CPU&#xff01;手搓CPU&#xff01;还是***手搓CPU&#xff01; 纯手工制作、全自主研发&#xff0c;于是一个名叫“初芯”的CPU终极形态终于诞生。 据UP主估计&#xff0c;…

大三下暑期前端找实习历程(已找到,经验版本)

本人情况&#xff1a; 四月份开始在武汉中国软件税务事业部实习两个月&#xff0c;六月底离职。六月初开始找的实习&#xff0c;七月头定下来了。 只用了boss直聘&#xff0c;目前沟通约500&#xff0c;已投简历68份。 六月开始找武汉的&#xff0c;一直很焦虑。 再说说我的…

【单片机】MSP430f5529单片机的矩阵键盘驱动,无阻塞方式的矩阵键盘读取

其他MSP430单片机也可使用&#xff0c;但需要更改引脚定义。 接线方式&#xff1a; //列0 P8.2 //列1 P3.7 //列2 P4.0 //列3 P4.3 //行0 P1.2 //行1 P1.3 //行2 P1.4 //行3 P1.5 这段程序是用来初始化按键&#xff0c;并读取矩阵键盘按键值的代码。 首先&#xff0c;在KEY_…

美眉都是可爱的…… (美图)

1.休闲MM 安静的午后 悄悄的想你 好想写什么点给你 纪念我们仿佛认识了 好久好久 2.公主MM 起风了 想起你低沉而温柔的话语 我的嘴角 情不自禁的上扬 风儿轻轻的拥住我 是你吗&#xff1f; 3.现代MM 落雪了 突然想起 你说过最爱的是茶 泡上一壶碧螺春 茶香四溢 暖暖的感觉 是你…

我被产品小姐姐的笔记本深深吸引了....

你看到我的笔记本了吗&#xff1f; 我刚整理过会议的记录本去哪里了呀&#xff1f; 啊。。这个图要怎么画&#xff1f;我笔记本好像有记 可是我找不到笔记本 这&#xff0c;就是我记笔记的日常 但是我不能因为找不到笔记本&#xff0c;就开始不用它 毕竟我的脑容量不允许我这样…