一.主代码如下
//身份证的orc
export function idcardOcr(fileInfo, uploadFile) {
return new Promise((resolve, reject) => {
let _judge = judgeFile(fileInfo);
if (!_judge.pass) {
showToast(_judge.msg, 'error');
reject({
msg: _judge.msg
})
return; //不return的话会继续执行下去
}
idcardRecognize(uploadFile).then(res => {
if (typeof (res) == 'string') {
res = JSON.parse(res);
}
resolve({
data: res.data,
msg: ''
})
}).catch((err) => {
console.log(err)
reject({
msg: '身份证识别失败!'
})
})
})
}
judgeFile:对文件进行合规性校验函数
showToast:全局提示函数
idcardRecognize:后端返回接口
二.judgeFile代码如下
//对文件进行拦截
function judgeFile(file) {
if (!file.name || !file.size || !file.type) {
return {
pass: false,
msg: '文件参数缺失'
}
}
if (["image/png", "image/jpg", "image/jpeg", "image/bmp"].indexOf(file.type) == -1) {
return {
pass: false,
msg: '仅支持.jpeg,.jpg,.png,.bmp文件的识别'
}
}
let size = file.size / 1024 / 1024;
console.log('图片大小为:', size, 'M');
if (size > 10) {
return {
pass: false,
msg: '文件大小不能大于10M'
}
}
return {
pass: true,
msg: ''
}
}