1.配置公众号环境
2.计算签名,详见“调用微信扫一扫”的 ‘/getSign’方法
3.前端:
3.1 加载配置
$.ajax({
type: 'get',
url: baseUrl+'/public/getSign',
data: {
'tokenUrl': location.href
},
async: false,
dataType: 'JSON',
contentType:'application/json',
success: function (res) {
AppId = res.appId;
console.log('这是AppId',AppId)
// 配置微信转发、调起相机等基本参数
wx.config({
debug: false,
appId: res.appId,
timestamp: res.timestamp, //todo later
nonceStr: res.nonceStr,
signature: res.signature,
jsApiList: [
// 所有要调用的 API 都要加到这个列表中
'updateAppMessageShareData',
'updateTimelineShareData',
'onMenuShareTimeline',
'onMenuShareAppMessage',
'onMenuShareQQ',
'scanQRCode',
'chooseImage',
'uploadImage',
'previewImage',
'downloadImage',
'getLocalImgData',
]
})
},
error: function () {
console.log('config fail')
}
})
3.2 调用后台获取上传图片base64
$('.camera').click(function () {
wx.chooseImage({
count: 1, // 默认9
sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
success : function(res) {
// alert("调用相机成功" + JSON.stringify(res));
var localIds = res.localIds[0];
wx.getLocalImgData({
localId: localIds.toString(),//图片的本地ID
success: function (res) {
localData = res.localData;//获取照片base64
// alert("base64 url:" + localData)
$.ajax({
type: 'post',
url: baseUrl+'/ocr/determineIdNum',
data: JSON.stringify({'IdNumImg': localData}),
dataType: 'JSON',
contentType:'application/json',
success: function (res) {
if(res.state == "Y"){
let data = res.data.userInfo
form.val('deliveryInfo', {
"customerName": data.Name
,"customerIdCardNumber": data.IdNum
});
layer.msg("识别成功!请确认姓名跟身份证号识别无误!",{icon:1})
}else{
layer.msg(res.msg,{icon:5})
}
},
error:function () {
}
});
}
});
},
error:function(res){
console.log(res)
}
});
})