Page({
data: {
accessToken: '',
frontImg: '../../images/idfront.jpg',
backImg: '../../images/idback.jpg',
info: {
name: '',
zu: '',
birth: '',
sex: '',
addr: '',
id: '',
dtRange: '',
auth: ''
},
isHidden: true
},
// 文件读取后
afterRead(event) {
let key = event.currentTarget.dataset.key
let imgkey = `${key}Img`
let file = {
url: event.detail.file.url,
name: '身份证人像面'
}
this.setData({
[imgkey]: file.url,
})
this.invokeService(file, key)
// console.log("文件信息", this.data);
},
async invokeService(file, key) {
// 调用ocr
try {
const invokeRes = await wx.serviceMarket.invokeService({
service: 'wx79ac3de8be320b71',
api: 'OcrAllInOne',
data: {
// 用 CDN 方法标记要上传并转换成 HTTP URL 的文件
img_url: new wx.serviceMarket.CDN({
type: 'filePath',
filePath: file.url,
}),
data_type: 3,
ocr_type: 1
},
})
// console.log('invokeService success::', invokeRes)
if (key == "front") {
let { address, birth, gender, id, name, nationality } = invokeRes.data.idcard_res
this.setData({
['info.name']: name.text,
['info.zu']: nationality.text,
['info.birth']: birth.text,
['info.sex']: gender.text,
['info.addr']: address.text,
['info.id']: id.text,
isHidden: false,
})
}
if (key == "back") {
let { authority, valid_date } = invokeRes.data.idcard_res
this.setData({
['info.auth']: authority.text,
['info.dtRange']: valid_date.text,
isHidden: false,
})
}
} catch (error) {
console.log(error, 'error')
}
},
// 进行验证
getAuth() {
if (!this.data.isHidden) {//如果信息显示,说明成功读取身份证信息
let { name, id } = this.data.info
//获取输入用户的信息
let app = getApp()
let inptName = app.globaldata.usrInfo.userName
let inptID = app.globaldata.usrInfo.userID
// 验证
if (inptName == name && inptID == id) {
console.log("身份信息认证匹配!");
} else {
console.log("身份信息认证不匹配!");
}
}
},
})