前端代码部分
复制代码
<template>
<view class="wrap">
<view class="box">
<view class="item flex-row align-items-center space-between">
<view class="name"><text style="color:#FF4D4D">*</text>姓名</view>
<view class="value">
<input type="text" placeholder="请输入姓名" placeholder-class="plc" v-model="realName">
</view>
</view>
<view class="item flex-row align-items-center space-between">
<view class="name"><text style="color:#FF4D4D">*</text>身份证号码</view>
<view class="value">
<input type="text" placeholder="请输入身份证号码" placeholder-class="plc" v-model="idCard">
</view>
</view>
</view>
<view class="btn" @click="submit">确认并进行人脸认证</view>
</view>
</template>
<script>
import {
error,
jumpPage,
isLogin,
success
} from "@/utils/tools"
import {
prefectUserInfo
} from "@/utils/api/api"
export default {
data() {
return {
baseUrl: getApp().globalData.baseUrl,
userInfo:{},
realName: '',
idCard: ''
}
},
onLoad() {
this.userInfo = uni.getStorageSync('userInfo')
},
methods: {
submit() {
const _this = this
// 1. 获取设备信息
const metaInfo = uni.getFacialRecognitionMetaInfo()
if (!metaInfo) {
error('提示当前设备不支持刷脸')
return
}
//在调用之前可以自己加一些获取摄像头权限等操作...
uniCloud.callFunction({
name: 'startFaceVerify',
data: {
metaInfo,
realName: _this.realName,
idCard: _this.idCard,
step: 1
},
success({result}) {
if(result.code === 500){
error(result.msg)
return
}
// 3. 客户端调起sdk刷脸认证
uni.startFacialRecognitionVerify({
certifyId: result.data,
success(success) {
//获取认证结果
uniCloud.callFunction({
name: 'startFaceVerify',
data: { certifyId: result.data },
success({result}) {
const params = {
id: _this.userInfo.id,
user_face_image: result.data.pictureUrl,
realname: _this.realName,
idcard: _this.idCard
}
prefectUserInfo(params).then(response =>{
if(response.errorCode === 0){
uni.navigateBack()
}else{
error('认证失败')
}
})
},
fail(e) { error('认证失败') }
})
},
fail(e) { error('认证失败') }
})
},
fail(e) { error('认证失败') }
})
}
}
}
</script>
<style>
page {
background: #F7F7F7;
}
</style>
<style lang="scss">
.wrap {
background: #F7F7F7;
padding: 15px;
display: flex;
flex-direction: column;
align-items: center;
.box {
width: 100%;
padding: 0px 15px;
border-radius: 10px;
background: #FFFFFF;
.item {
width: 100%;
height: 50px;
.name {
font-size: 14px;
font-weight: 500;
color: #121212;
}
.value {
display: flex;
align-items: center;
input {
width: 225px;
font-size: 12px;
font-weight: 500;
color: #666666;
text-align: right;
}
.plc {
font-size: 12px;
font-weight: 500;
color: #666666;
}
}
}
}
.btn {
width: 305px;
height: 44px;
border-radius: 22px;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
background: linear-gradient(106deg, #5EBBFF 21%, #5670FE 68%);
box-shadow: 0px 4px 10px 0px #BECCFF;
font-size: 14px;
font-weight: bold;
color: #FFFFFF;
margin-top: 365px;
}
}
</style>
云函数部分
复制代码
// index.js 云函数
'use strict'
const crypto = require('crypto')
exports.main = async (event, context) => {
// console.log('这个是什么',event.step);
// return
// 获取实人认证实例
const frvManager = await uniCloud.getFacialRecognitionVerifyManager({
requestId: context.requestId
})
if (event.step == 1) {
try {
/**
* 获取certifyId
* @return certifyId 认证id,用于客户端调用认证接口及云函数获取认证结果
*/
const result = await frvManager.getCertifyId({
realName: event.realName,
idCard: event.idCard,
metaInfo: event.metaInfo,
needPicture: true //是否采集人脸照片
})
if (result.errCode == 0) return {
code: 200,
data: result.certifyId,
msg: '获取certifyId成功'
}
return {
code: 500,
data: '',
msg: `获取certifyId失败:${ result.errMsg }`
}
} catch (err) {
console.log('err', err);
return {
code: 500,
data: '',
msg: '请检查您填写的姓名与身份证号是否正确'
}
}
} else {
// 获取认证结果
const result = await frvManager.getAuthResult({
certifyId: event.certifyId
})
if (result.authState == 'SUCCESS') return {
code: 200,
data: result
}
// 认证失败
if (result.authState == 'FAIL') return {
code: 500,
data: result.certifyId,
msg: `认证失败:${ result.errMsg }`
}
}
}