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

相关文章

SpringBoot3.4配置校验新特性的用法详解

《SpringBoot3.4配置校验新特性的用法详解》SpringBoot3.4对配置校验支持进行了全面升级,这篇文章为大家详细介绍了一下它们的具体使用,文中的示例代码讲解详细,感兴趣的小伙伴可以参考... 目录基本用法示例定义配置类配置 application.yml注入使用嵌套对象与集合元素深度校验开发

HTML5中的Microdata与历史记录管理详解

《HTML5中的Microdata与历史记录管理详解》Microdata作为HTML5新增的一个特性,它允许开发者在HTML文档中添加更多的语义信息,以便于搜索引擎和浏览器更好地理解页面内容,本文将探... 目录html5中的Mijscrodata与历史记录管理背景简介html5中的Microdata使用M

html5的响应式布局的方法示例详解

《html5的响应式布局的方法示例详解》:本文主要介绍了HTML5中使用媒体查询和Flexbox进行响应式布局的方法,简要介绍了CSSGrid布局的基础知识和如何实现自动换行的网格布局,详细内容请阅读本文,希望能对你有所帮助... 一 使用媒体查询响应式布局        使用的参数@media这是常用的

HTML5表格语法格式详解

《HTML5表格语法格式详解》在HTML语法中,表格主要通过table、tr和td3个标签构成,本文通过实例代码讲解HTML5表格语法格式,感兴趣的朋友一起看看吧... 目录一、表格1.表格语法格式2.表格属性 3.例子二、不规则表格1.跨行2.跨列3.例子一、表格在html语法中,表格主要通过< tab

springboot上传zip包并解压至服务器nginx目录方式

《springboot上传zip包并解压至服务器nginx目录方式》:本文主要介绍springboot上传zip包并解压至服务器nginx目录方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录springboot上传zip包并解压至服务器nginx目录1.首先需要引入zip相关jar包2.然

Java使用ANTLR4对Lua脚本语法校验详解

《Java使用ANTLR4对Lua脚本语法校验详解》ANTLR是一个强大的解析器生成器,用于读取、处理、执行或翻译结构化文本或二进制文件,下面就跟随小编一起看看Java如何使用ANTLR4对Lua脚本... 目录什么是ANTLR?第一个例子ANTLR4 的工作流程Lua脚本语法校验准备一个Lua Gramm

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

JS+HTML实现在线图片水印添加工具

《JS+HTML实现在线图片水印添加工具》在社交媒体和内容创作日益频繁的今天,如何保护原创内容、展示品牌身份成了一个不得不面对的问题,本文将实现一个完全基于HTML+CSS构建的现代化图片水印在线工具... 目录概述功能亮点使用方法技术解析延伸思考运行效果项目源码下载总结概述在社交媒体和内容创作日益频繁的

前端CSS Grid 布局示例详解

《前端CSSGrid布局示例详解》CSSGrid是一种二维布局系统,可以同时控制行和列,相比Flex(一维布局),更适合用在整体页面布局或复杂模块结构中,:本文主要介绍前端CSSGri... 目录css Grid 布局详解(通俗易懂版)一、概述二、基础概念三、创建 Grid 容器四、定义网格行和列五、设置行

SpringValidation数据校验之约束注解与分组校验方式

《SpringValidation数据校验之约束注解与分组校验方式》本文将深入探讨SpringValidation的核心功能,帮助开发者掌握约束注解的使用技巧和分组校验的高级应用,从而构建更加健壮和可... 目录引言一、Spring Validation基础架构1.1 jsR-380标准与Spring整合1