// pages/ocr/ocr.js const tip_en = require("../../international/ocr_scence/index") const app = getApp() const upload_image = require("../../utils/upload/upload_image") const { getPassportDetail, BaiDuFaceComparison } = require("../../utils/api/make_appointment") Page({ /** * 页面的初始数据 */ data: { // 中英文 isChinese: true, // 翻译包 tip: tip_en, // 证件类型 type_list: [{ name_ch: '护照', name_en: 'passport', type: 13 }, // {name_ch: '往来港澳通行证', name_en: 'Hong Kong and Macao Travel Permit', type: 22}, // {name_ch: '往来台湾通行证', name_en: 'Taiwan Travel Permit', type: 29}, // {name_ch: '香港居民身份证', name_en: 'Hong Kong Identity Card', type: 1001}, ], type_index: 0, type: 13, needPhoto: false, }, // 选择证件类型 chooseType(e) { this.setData({ type_index: e.detail.value, type: this.data.type_list[e.detail.value].type }) }, // 提交护照验证 doOCR() { var that = this; if (!this.data.type) { wx.showToast({ title: this.data.isChinese ? '请先选择证件类型~' : 'Please select the id type~', icon: 'none' }); return; } wx.chooseMedia({ count: 1, mediaType: ['image'], sourceType: ['album', 'camera'], sizeType: ['original'], success(res) { var image = { url: res.tempFiles[0].tempFilePath } wx.showLoading({ title: that.data.isChinese ? '上传中~' : 'uploading...', }) //上传照片 upload_image(image).then((img) => { wx.hideLoading() var data = { passportUrl: img.url, type: that.data.type } that.checkInfo(data) }).catch(() => { wx.hideLoading() }) } }) }, //处理结果 checkInfo(data) { let that = this; wx.showLoading({ title: that.data.isChinese ? '识别中...' : 'recognizing..', }) //ocr 识别 getPassportDetail(data).then(res => { wx.hideLoading(); //接口层错误 if (res.code != 200) { wx.showModal({ content: res.msg, showCancel: false }); return; } //数据识别不完整 if (!res.data.avatar) { wx.showModal({ content: 'Sorry, OCR Failed. Please make sure the photo is clear and complete, then try again。', showCancel: false, }); return; } //识别成功,暂存识别信息 that.data.ocrInfo = res.data; that.data.ocrInfo.passportUrl = data.passportUrl; //需要抓拍人像(实名一定需要抓拍人像) if (that.data.needRealname || that.data.needPhoto) { that.data.flag = 3; wx.navigateTo({ url: '/pages/faceCheck/faceCheck' }); return; } //无需进行人像抓拍 app.data.ocrInfo = that.data.ocrInfo; wx.navigateBack({ delta: 1 }) }).catch(err => { wx.hideLoading() }) }, //比对照片 comparePhoto() { const that = this; let img = app.data.catchFaceUrl; app.data.catchFaceUrl = null; //拍摄人脸照片失败 if(!img) { wx.showModal({ content: that.data.isChinese ? '抓拍失败, 再试一次吧' : 'Sorry,failed to capture a profile picture,pleas try again。', showCancel: false, complete: (res) => {} }) return; } //拍摄人脸照片成功 //无需 1:1 比对 if(!this.data.needRealname) { that.data.ocrInfo.faceImage = img app.data.ocrInfo = that.data.ocrInfo; wx.navigateBack({ delta: 1 }) return; } //需要 1:1 比对 let data = { faceImage: img, cardImage: that.data.ocrInfo.avatar || '', } BaiDuFaceComparison(data).then(res => { //1:1比对成功 if(res.data) { that.data.ocrInfo.faceImage = img app.data.ocrInfo = that.data.ocrInfo; wx.navigateBack({ delta: 1 }) //1;1 比对失败 } else { wx.showModal({ content: that.data.isChinese ? '比对失败, 请保持光线良好,护照照片清晰,再试一次吧' : 'Sorry,the comparison failed,keep the light good and the passport clear,then try again。', showCancel: false, complete: (res) => {} }) } }) }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { this.setData({ needPhoto: options.needPhoto === 'true' ? true:false, needRealname: options.needRealname === 'true'? true:false }) if (app.data.language == 'ch') { this.setData({ isChinese: true }) } else { this.setData({ isChinese: false }) } // (过审专用,禁止删除)- start app.data.fake = false // (过审专用,禁止删除)- end }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { //抓拍照片完成返回 if(this.data.flag == 3) { this.data.flag = null; this.comparePhoto() } }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })