記錄--Uni-app接入騰訊人臉核身
這里給大家分享我在網上總結出來的一些知識,希望對大家有所幫助
人臉核身功能有多種接入方式,其中包含微信H5、微信小程序、APP、獨立H5、PC端、API接入6種方式。
? 我們的產品是使用uni-app來開發,所以第一時間考慮使用H5方式接入,但是通過與官方技術人員對接后得知,uni-app是有原生插件可以用的,所以可以使用app的方式接入,原生的插件方式接入會讓用戶體驗更好,所以本文也是圍繞著APP原生插件的方式接入。
準備工作
首先需要申請服務,此服務并不是直接購買,而是需要提交申請,通過人工審核后才可以使用(申請鏈接)
申請通過后,在控制臺創建應用,如圖

添加官方技術人員微信(vx:faceid001),索要license,后面需要用到
uni-app插件市場添加人臉核身(DC-WBOCRService)和ocr識別插件(DC-WBOCRService)
至此,前期接入準備工作已經完成。
接入步驟
獲取AccessToken(官方文檔)
接口地址:https://idasc.webank.com/api/oauth2/access_token
參數:
app_id: _this.app_id, secret: _this.secret, grant_type: 'client_credential', version: '1.0.0'
請求代碼:
uni.request({
url: 'https://idasc.webank.com/api/oauth2/access_token',
data: {
app_id: _this.app_id,
secret: _this.secret,
grant_type: 'client_credential',
version: '1.0.0'
},
success(res) {
_this.access_token = res.data.access_token;
console.log(res.data);
console.log('access_token:' + _this.access_token);
},
fail(e) {
console.log(e);
},
complete() {
}
});
此處的grant_type和version為固定參數
響應結果:
{
"code":"0","msg":"請求成功",
"transactionTime":"20151022043831",
"access_token":"accessToken_string",
"expire_time":"20151022043831",
"expire_in":"7200"
}
獲取NONCE ticket(官方文檔)
接口地址:https://idasc.webank.com/api/oauth2/api_ticket
參數:
app_id: _this.app_id, access_token: _this.access_token, type: 'NONCE', version: _this.version, user_id: _this.userId
請求代碼:
uni.request({
url: 'https://idasc.webank.com/api/oauth2/api_ticket',
data: {
app_id: _this.app_id,
access_token: _this.access_token,
type: 'NONCE',
version: _this.version,
user_id: _this.userId
},
success(res) {
_this.showToast(res.data);
_this.ticket = res.data.tickets[0].value;
console.log('ticket:' + _this.ticket);
},
fail(e) {
console.log(e);
_this.showToast(e.code);
},
complete() {
uni.hideLoading();
}
});
響應結果:
{
"code": "0",
"msg": "請求成功",
"transactionTime": "20151022044027",
"tickets": [{
"value": "ticket_string",
"expire_in": "120",
"expire_time": "20151022044027"
}]
}
獲取簽名(官方文檔)
從文檔上來看是需要將wbappid userId nonceStr version ticket放在數組中進行排序,然后使用sha1算法進行加密得到一串40位的簽名。
我從本地使用sha1庫進行加密,然而返回結果一直報錯,通過與官方技術人員溝通得知此步驟加密必須在服務端進行,所以下方列出java和php的加密代碼
Java:
public static String sign(List<String> values, String ticket) {
if (values == null) {
throw new NullPointerException("values is null");
}
values.removeAll(Collections.singleton(null));// remove null
values.add(ticket);
java.util.Collections.sort(values);
StringBuilder sb = new StringBuilder();
for (String s : values) {
sb.append(s);
}
return Hashing.sha1().hashString(sb, Charsets.UTF_8).toString().toUpperCase();
}
PHP:
<?php
$arr_test =
array('TIDApint','kHoSxvLZGxSoFsjxlbzEoUzh5PAnTU7T','xxx','xxxxxxxx','kHoSxvLZGxSoFsjxlbzEoUzh5PAnTU7T','1.0.0','jMgg28AVjLmmzUUU5bFS4jhhpzi9HUbp8ggtvGyAIIsn8aedN68xs88GYxvnEjp6');
print_r('</br>');
print_r('參加字典排序的參數為 ');
print_r($arr_test);
$arr_test = array_values($arr_test);
asort($arr_test);
$arr_test =implode('',$arr_test);
print_r('</br>');
print_r('字典排序為 ');
print_r($arr_test);
$sign = sha1($arr_test);
print_r('</br>');
print_r('簽名值為 ');
print_r($sign);
?>
注意:這一步必須在服務端進行處理
獲取FaceId(官方文檔)
請求地址:https://idasc.webank.com/api/server/getfaceid
參數:
webankAppId: _this.app_id, orderNo: _this.orderNo, //訂單號,由合作方上送,每次唯一,不能超過32位 name: _this.idCardInfo.name, //姓名 idNo: _this.idCardInfo.cardNum, //證件號碼 userId: _this.userId, //用戶 ID ,用戶的唯一標識(不能帶有特殊字符) sourcePhotoStr: '', //比對源照片,注意:原始圖片不能超過500KB,且必須為 JPG 或 PNG 格式;參數有值:使合作伙伴提供的比對源照片進行比對,必須注照片是正臉可信照片,照片質量由合作方保證;參數為空 :根據身份證號+姓名使用權威數據源比對 sourcePhotoType: '2', //比對源照片類型,注意: 如合作方上送比對源則必傳,使用權威數據源可不傳;參數值為1:水紋正臉照;參數值為2:高清正臉照 version: _this.version, //默認參數值為:1.0.0 sign: _this.sign //簽名:使用上文 生成的簽名
請求代碼:
uni.request({
url: 'https://idasc.webank.com/api/server/getfaceid',
method: 'POST',
data: {
webankAppId: _this.app_id,
orderNo: _this.orderNo, //訂單號,由合作方上送,每次唯一,不能超過32位
name: _this.idCardInfo.name, //姓名
idNo: _this.idCardInfo.cardNum, //證件號碼
userId: _this.userId, //用戶 ID ,用戶的唯一標識(不能帶有特殊字符)
sourcePhotoStr: '', //比對源照片,注意:原始圖片不能超過500KB,且必須為 JPG 或 PNG 格式;參數有值:使合作伙伴提供的比對源照片進行比對,必須注照片是正臉可信照片,照片質量由合作方保證;參數為空 :根據身份證號+姓名使用權威數據源比對
sourcePhotoType: '2', //比對源照片類型,注意: 如合作方上送比對源則必傳,使用權威數據源可不傳;參數值為1:水紋正臉照;參數值為2:高清正臉照
version: _this.version, //默認參數值為:1.0.0
sign: _this.sign //簽名:使用上文 生成的簽名
},
success(res) {
_this.faceId = res.data.result.faceId;
console.log(res.data);
},
fail(e) {
console.log(e);
},
complete() {}
});
響應結果:
{
"code": 0,
"msg": "成功",
"result": {
"bizSeqNo":"業務流水號",
"orderNo":"合作方訂單號",
"faceId":"cc1184c3995c71a731357f9812aab988"
}
}
通過上面4個步驟已經獲取到了我們需要的所有參數,接下來就可以調用原生插件來實現人臉認證了。
uni-app中調用人臉核身插件進行人臉認證
const face = uni.requireNativePlugin('DC-WBFaceService');
face.startWbFaceVerifyService(
{
userId: this.userId,
nonce: this.nonceStr,
sign: this.sign,
appId: this.app_id,
orderNo: this.orderNo,
apiVersion: this.version,
licence: this.licence,
faceType: '1',
compareType: '0',
faceId: this.faceId,
sdkConfig: {
//Android和iOS共有的配置參數
showSuccessPage: true, //是否展示成功頁面
showFailurePage: true, //是否展示失敗頁面
recordVideo: true, //是否錄制視頻
playVoice: true, //是否播放語音提示
detectCloseEyes: true, //是否檢測用戶閉眼
theme: '1', //sdk皮膚設置,0黑色,1白色
//android獨有的配置參數
isEnableLog: true, //是否打開刷臉native日志,請release版本關閉!!!
//iOS獨有的配置參數
windowLevel: '1', //sdk中拉起人臉活體識別界面中使用UIWindow時的windowLevel配置
manualCookie: true //是否由SDK內部處理sdk網絡請求的cookie
}
},
result => {
console.log('【uni log】face SDK callback ================> result.');
console.log(result);
}
);
到這一步,就可以實現人臉核身了。



浙公網安備 33010602011771號