vant+vue 上传身份证正面和反面 Ocr识别校验反显

本文主要是介绍vant+vue 上传身份证正面和反面 Ocr识别校验反显,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

页面:

代码:

//1 用vant的上传组件实现上传入口
//下面是否必传校验此处注释了,按需使用<!-- 上传身份影像组件 --><div class="upload_ocr_upload"><div class="upload_ocr_upload_box"><!-- :rules="[{ required: true, message: '请上传身份证正面' }]" --><van-field label="上传证件" name="idFrontFile"><template #input><van-uploaderv-model="applicant.idFrontFile":after-read="afterRead('applicant.idFrontFile', 1)":preview-full-image="false"max-count="1"><template #default><imgsrc="@/assets/images/face/preIdFImg.png"alt=""style="width: 110px; height: 80px"/></template></van-uploader></template></van-field></div><div class="upload_ocr_upload_box"><!-- :rules="[{ required: true, message: '请上传身份证反面' }]" --><van-field name="idBackFile"><template #input><van-uploaderv-model="applicant.idBackFile":after-read="afterRead('applicant.idBackFile', 2)":preview-full-image="false"max-count="1"><template #default><imgsrc="@/assets/images/face/preIdBImg.png"alt=""style="width: 110px; height: 80px"/></template></van-uploader></template></van-field></div></div>//data中声明
data:{isShowID: false,imageList: [],newOCRXT: true, // 嵌入新ocr系统标识businessNo: "", // ocr系统业务号imageList: [],applicant:{idFrontFile:[],idBackFile:[],beneWay:"",//证件有效期 1-非长期  0-长期applicantName:"",//证件姓名applicantIdentityId:"",//身份证号applicantAddress:"",//证件地址applicantEndID:"",//证件止期
}
}//methods中事件methods:{// 上传身份证afterRead(fileList, type) {return async (file) => {this[fileList] = [];//注释--imageCompress写在js文件中的,根据import引入,代码在下面file.file = await imageCompress(file.file);if (this.newOCRXT) {let fd = new FormData();fd.append("file", file.file);this.$loading("识别中...");//注释--getOCR为识别接口,根据接口路径,自行封装,参数按需修改const { code, msg, data } = await getOCR({params: fd,query: {type: type == "1" ? "0" : "1",serviceNo: this.businessNo,},});this.$cls();if (code !== "0000") {return this.$notify_err(msg);}//正面if (type == 1) {if (!data.name || !data.idcard) {return this.$notify_err("请上传正确的证件信息");} else {//获取到身份证信息  绑定到对应数据上即可this.applicant.applicantName = data.name;this.applicant.applicantIdentityId = data.idcard;this.applicant.applicantAddress = data.address;this[fileList] = [file];this.isShowID = true;}//反面} else {// console.log("上传了背面");let urlP = {name: this.applicant.applicantName,cardNumber: this.applicant.applicantIdentityId,cardType: "1",};//这一步拿到身份证号的有效期  截取出截止日期let resDate = data.closeDate;let dataNow = new Date();let resDateStr =resDate.substring(0, 4) +"-" +resDate.substring(4, 6) +"-" +resDate.substring(6, 8);this.applicant.applicantEndID = resDateStr;if (resDate.indexOf("长期") > -1 ||new Date(resDateStr) > new Date(dataNow)) {//注释--getOcrOssUrl为回显接口,根据接口路径,自行封装,参数按需修改getOcrOssUrl(urlP).then((resOCR) => {if (resOCR.code !== "0000") {return this.$notify_err(resOCR.msg);}// 正面this.uploadParams(1, resOCR.data[0].imageUrl);// 反面this.uploadParams(2, resOCR.data[1].imageUrl);this[fileList] = [file];this.isShowID = true;});} else {return this.$notify_err("证件有效期有误");}}} else {var paramsObj = {file: file.file,type: type == 1 ? "第二代身份证" : "第二代身份证背面",channel: this.$route.query.Channel,};var params = new FormData();Object.keys(paramsObj).forEach((attr) => {params.append(attr, paramsObj[attr]);});this.$loading("识别中...");//注释--uploadToOss为影像上传oss接口 按需引用封装const { data } = await uploadToOss(params);this.$cls("识别中...");console.log(data);if (!data) {this.$notify_err("影像不符合规范,请检查图片是否清晰,如果确认无误仍上传失败,请联系客服:8880231");return;}//正面if (type == 1) {if (!data.idCard.idNumber || !data.idCard.name) {return this.$notify_err("请上传正确的证件信息");} else {this.applicant.applicantName = data.idCard.name;this.applicant.applicantIdentityId = data.idCard.idNumber;this[fileList] = [file];this.isShowID = true;}//反面} else {console.log("上传了背面");var resDate = data.idCard.validity.replace(/[. -]/g, "");if (resDate.indexOf("长期") > -1 || resDate) {this[fileList] = [file];this.isShowID = true;} else {return this.$notify_err("证件有效期有误");}}this.uploadParams(type, data.imageUrl);}};},// 上传参数uploadParams(type, imageUrl) {let pamasImg = {type,imageUrl,};if (this.imageList.length > 0) {for (let i = 0; i < this.imageList.length; i++) {if (this.imageList[i].type == type) {this.imageList.splice(i, 1);}}}this.imageList.push(pamasImg);},
}//引入的js文件中写了图片压缩的方法
我是js文件:{
/*** * @param {Blob|base64Data} file 图片数据 * @param {Number} maxSize 文件最大字节控制* @description 图片压缩处理*/export const imageCompress = function (file, maxSize = 1024 * 500) {console.log(file);if (!file.size) {file = base64ToFile(file);}let ratio = Number(((maxSize * 0.8) / file.size).toFixed(2));let img = new Image();let name = file.name;let canvas = document.createElement('canvas');let compompressCount = 1;let ctx = canvas.getContext('2d');let handler = function (file) {return new Promise((res, rej) => {console.log(file.size, maxSize);if (file.size <= maxSize) {res(file);} else {ratio = Number((maxSize / file.size).toFixed(2));console.log(ratio);img.src = URL.createObjectURL(file);img.onload = function () {canvas.width = this.width;canvas.height = this.height;if (compompressCount > 1) {canvas.width = this.width * (1 / compompressCount * 1.8);canvas.height = this.height * (1 / compompressCount * 1.8);}compompressCount++;ctx.drawImage(this, 0, 0, canvas.width, canvas.height);res(handler(base64ToFile(canvas.toDataURL("image/jpeg", ratio), name)));};}});};return handler(file);
}
}//上传框样式
<style lang='less' scoped>.upload_ocr_upload {display: flex;justify-content: space-between;.upload_ocr_upload_box {text-align: center;.van-uploader__preview-image {width: 110px !important;height: 80px !important;}}.van-cell {padding-left: 0px;padding-right: 0px;}
}
</style>

这篇关于vant+vue 上传身份证正面和反面 Ocr识别校验反显的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/348447

相关文章

Vue和React受控组件的区别小结

《Vue和React受控组件的区别小结》本文主要介绍了Vue和React受控组件的区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录背景React 的实现vue3 的实现写法一:直接修改事件参数写法二:通过ref引用 DOMVu

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

SpringBoot实现不同接口指定上传文件大小的具体步骤

《SpringBoot实现不同接口指定上传文件大小的具体步骤》:本文主要介绍在SpringBoot中通过自定义注解、AOP拦截和配置文件实现不同接口上传文件大小限制的方法,强调需设置全局阈值远大于... 目录一  springboot实现不同接口指定文件大小1.1 思路说明1.2 工程启动说明二 具体实施2

Vue3绑定props默认值问题

《Vue3绑定props默认值问题》使用Vue3的defineProps配合TypeScript的interface定义props类型,并通过withDefaults设置默认值,使组件能安全访问传入的... 目录前言步骤步骤1:使用 defineProps 定义 Props步骤2:设置默认值总结前言使用T

Springboot项目登录校验功能实现

《Springboot项目登录校验功能实现》本文介绍了Web登录校验的重要性,对比了Cookie、Session和JWT三种会话技术,分析其优缺点,并讲解了过滤器与拦截器的统一拦截方案,推荐使用JWT... 目录引言一、登录校验的基本概念二、HTTP协议的无状态性三、会话跟android踪技术1. Cook

基于Python Playwright进行前端性能测试的脚本实现

《基于PythonPlaywright进行前端性能测试的脚本实现》在当今Web应用开发中,性能优化是提升用户体验的关键因素之一,本文将介绍如何使用Playwright构建一个自动化性能测试工具,希望... 目录引言工具概述整体架构核心实现解析1. 浏览器初始化2. 性能数据收集3. 资源分析4. 关键性能指

SpringBoot 异常处理/自定义格式校验的问题实例详解

《SpringBoot异常处理/自定义格式校验的问题实例详解》文章探讨SpringBoot中自定义注解校验问题,区分参数级与类级约束触发的异常类型,建议通过@RestControllerAdvice... 目录1. 问题简要描述2. 异常触发1) 参数级别约束2) 类级别约束3. 异常处理1) 字段级别约束

从入门到精通详解LangChain加载HTML内容的全攻略

《从入门到精通详解LangChain加载HTML内容的全攻略》这篇文章主要为大家详细介绍了如何用LangChain优雅地处理HTML内容,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录引言:当大语言模型遇见html一、HTML加载器为什么需要专门的HTML加载器核心加载器对比表二

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建