詳解微信小程序官方人臉核身認(rèn)證
小程序收集了下用戶個(gè)人信息上傳被打回來說:
你好,小程序頁面功能涉及:采集用戶生物特征(人臉照片或視頻)及其他敏感信息,用于身份認(rèn)識(shí)或識(shí)別,
為保障用戶敏感隱私身份信息,平臺(tái)暫不支持此功能。請(qǐng)去除相關(guān)功能后重新提交。
然后就去找度娘搜了下需要申請(qǐng)
wx.startFacialRecognitionVerify({})
https://api.weixin.qq.com/cgi-bin/token?appid=appid&secret=secret&grant_type=client_credential
https://api.weixin.qq.com/cityservice/face/identify/getinfo?access_token=access_token
https://api.weixin.qq.com/cityservice/face/identify/getimage?access_token=access_token
首先要給申請(qǐng)下來的接口發(fā)送倆個(gè)參數(shù):名字、身份證號(hào)碼
photo.js
data: {
openid: '',
custName: '姓名',
custIdCard: '身份證號(hào)碼',
verifyImg: ''
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面加載
*/
onLoad: function (options) {
this.setData({
custName: options.custName,
custIdCard: options.custIdCard
});
var _this = this;
wx.checkIsSupportFacialRecognition({
checkAliveType: 2,
success: function (res) {
if (res.errCode === 0 || res.errMsg === "checkIsSupportFacialRecognition:ok") {
//調(diào)用人臉識(shí)別
_this.startface(_this.data.custName.replace(/(^\s*)|(\s*)$/g, ""), _this.data.custIdCard); //身份證名稱,身份證號(hào)碼
return;
}
wx.showToast('微信版本過低,暫時(shí)無法使用此功能,請(qǐng)升級(jí)微信最新版本')
},
fail: function(res){
wx.showToast('微信版本過低,暫時(shí)無法使用此功能,請(qǐng)升級(jí)微信最新版本')
}
})
},
startface(name, idcard) {
console.log('我進(jìn)來了?。?!');
var _this = this;
wx.startFacialRecognitionVerify({
name: _this.data.custName, //身份證名稱
idCardNumber: _this.data.custIdCard, //身份證號(hào)碼
success: function (res) {
var verifyResult = res.verifyResult; //認(rèn)證結(jié)果
//調(diào)用接口
wx.request({
url: 'https://api.weixin.qq.com/cgi-bin/token?appid=wx2cafec51ec4c2153&secret=8d3e68a5a2c702673340d72d1c7db4cc&grant_type=client_credential',
data: {
},
method: 'POST',
header: {
'content-type': 'application/json;charset=utf-8'
},
success: function (res) {
console.log(res.data);
console.log(res.data.access_token)
var token = res.data.access_token;
wx.request({
url: 'https://api.weixin.qq.com/cityservice/face/identify/getinfo?access_token=' + res.data.access_token,
data: {
verify_result: verifyResult
},
method: 'POST',
header: {
'content-type': 'application/json;charset=utf-8'
},
success: function (res) {
console.log(res.data)
console.log('我終于成功了。。。。')
wx.request({
url: 'https://api.weixin.qq.com/cityservice/face/identify/getimage?access_token=' + token,
data: {
verify_result: verifyResult
},
method: 'POST',
responseType: 'arraybuffer',
header: {
'content-type': 'application/json;charset=utf-8',
},
success: (res) => {
// console.log('data:image/png;base64,'+wx.arrayBufferToBases64(res))
console.log(res.data);
var base64 = wx.arrayBufferToBase64(res.data);
_this.setData({ verifyImg:'data:image/png;base64,'+ base64})
wx.navigateTo({
url: '../msg/msg?msg=恭喜您信息核驗(yàn)成功&verifyImg=' + _this.data.verifyImg
});
},
fail: function (res) {
console.log('失敗', res.data)
}
})
},
fail: function (res) {
}
})
},
fail: function (res) {
}
})
console.log(verifyResult)
// wx.navigateTo({
// url: '../msg/msg?msg=人臉核身認(rèn)證成功'
// });
},
checkAliveType: 2, //屏幕閃爍(人臉核驗(yàn)的交互方式,默認(rèn)0,讀數(shù)字)
fail: err => {
wx.showToast('請(qǐng)保持光線充足,面部正對(duì)手機(jī),且無遮擋')
wx.navigateTo({
url: '../msg/msg?msg=請(qǐng)保持光線充足,面部正對(duì)手機(jī),且無遮擋,請(qǐng)退出重新操作'
});
}
})
}
主要坑的是這樣下來得申請(qǐng)好幾次接口到最后業(yè)務(wù)還需要unionid所以還得去微信開放平臺(tái)申請(qǐng)認(rèn)證
然后想要拉取核身結(jié)果的圖片的時(shí)候就需要黨上面的https://api.weixin.qq.com/cityservice/face/identify/getimage?access_token=access_token
返回的數(shù)據(jù)需要轉(zhuǎn)成base64碼然后顯示在image標(biāo)簽上我是直接傳給后臺(tái)的
下面上我msg的js代碼
msg.js
const app = getApp();
Page({
/**
* 頁面的初始數(shù)據(jù)
*/
data: {
msg:'',
verifyImg:'',
url:app.globalData.PostData
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面加載
*/
onLoad: function (options) {
var timestamp = Date.parse(new Date());
timestamp = timestamp/1000
console.log(options)
var that = this;
that.setData({
msg:options.msg,
verifyImg:options.verifyImg
});
console.log(that.data.url)
console.log(that.data.verifyImg)
wx.request({
url: that.data.url+'fileUpload!upBase64.do', //僅為示例,非真實(shí)的接口地址
data: {
file:that.data.verifyImg,
filename:timestamp,
filedata:that.data.verifyImg
},
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
},
success:function (res){
const data = res.data
console.log('成功',data);
//do something
},
fail:function(res){
console.log('失敗',res)
}
})
}
后臺(tái)上傳base64轉(zhuǎn)換的代碼
public void upBase64() {
System.out.println("======開始上傳圖片====");
System.out.println(file);
Json j = new Json();
String FilePath = ServletActionContext.getServletContext().getRealPath(Constants.IMGPATH+"/"+Constants.PHOTOPATH);
File PathFile = new File(FilePath);
try {
// 如果是IE,那么需要設(shè)置為text/html,否則會(huì)彈框下載
// response.setContentType("text/html;charset=UTF-8");
response.setContentType("application/json;charset=UTF-8");
String FileName = request.getParameter("filename");
String FileData = request.getParameter("filedata");
System.out.println(FileName+"**************"+FileData);
if (null == FileData || FileData.length() < 50) {
j.setMsg("上傳失敗,數(shù)據(jù)太短或不存");
j.setSuccess(false);
} else {
// 去除開頭不合理的數(shù)據(jù)
FileData = FileData.substring(30);
FileData = URLDecoder.decode(FileData, "UTF-8");
System.out.println("FileData="+FileData);
byte[] data = FileUtil.decode(FileData);
/*if (null == FileName || FileName.length() < 1) {
FileName = System.currentTimeMillis() + ".jpg";
}*/
// 寫入到文件
FileOutputStream outputStream = new FileOutputStream(new File(PathFile,FileName));
outputStream.write(data);
outputStream.flush();
outputStream.close();
System.out.println(FileName+"**************"+FileData);
j.setMsg("上傳成功");
j.setSuccess(true);
}
} catch (Exception err) {
j.setMsg("上傳失敗");
j.setSuccess(false);
err.printStackTrace();
}
writeJson(j);
}
以上就是詳解微信小程序官方人臉核身認(rèn)證的詳細(xì)內(nèi)容,更多關(guān)于微信小程序官方人臉核身認(rèn)證的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
js或jquery動(dòng)態(tài)輸出select option的實(shí)現(xiàn)代碼
在 JavaScript 中動(dòng)態(tài)生成 <option> 元素并添加到 <select> 下拉列表中,可以通過以下幾種方式實(shí)現(xiàn),以下是不同場景下的完整代碼示例2025-03-03
JavaScript?中URL?查詢字符串(query?string)的序列與反序列化的方法
在 JavaScript 中,可以使用?URLSearchParams?對(duì)象來處理 URL 中的查詢字符串,這篇文章主要介紹了JavaScript?中URL查詢字符串(query?string)的序列與反序列化,需要的朋友可以參考下2023-01-01
使用原生js+canvas實(shí)現(xiàn)模擬心電圖的實(shí)例
下面小編就為大家?guī)硪黄褂迷鷍s+canvas實(shí)現(xiàn)模擬心電圖的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
JS組件Bootstrap實(shí)現(xiàn)彈出框效果代碼
這篇文章主要介紹了JS組件Bootstrap實(shí)現(xiàn)彈出框效果代碼的相關(guān)資料,對(duì)彈出框感興趣的小伙伴們可以參考一下2016-04-04
詳解JavaScript Alert函數(shù)執(zhí)行順序問題
本文主要介紹了Javascript的Alert函數(shù)執(zhí)行順序問題,對(duì)此感興趣的同學(xué),可以實(shí)驗(yàn)一下,以便解決平時(shí)遇到的一些奇怪的問題。2021-05-05
javascript實(shí)現(xiàn)window.print()去除頁眉頁腳
這篇文章主要介紹了javascript實(shí)現(xiàn)window.print()去除頁眉頁腳的方法以及各參數(shù)的設(shè)置技巧,需要的朋友可以參考下2014-12-12
基于MVC4+EasyUI的Web開發(fā)框架形成之旅之界面控件的使用
一般Web界面包括的界面控件有:單行文本框、多行文本框、密碼文本框、下拉列表Combobox、日期輸入控件、數(shù)值輸入控件、單項(xiàng)選擇、復(fù)選框、表格控件DataGrid、樹形控件、布局控件、彈出式對(duì)話框、提示信息、列表控件等,這些界面控件的操作都有哪些不同,下面逐一介紹2015-12-12
詳解JavaScript中浮點(diǎn)數(shù)的精度計(jì)算
這篇文章主要來和大家介紹一下JavaScript中浮點(diǎn)數(shù)精度計(jì)算的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-06-06

