vue3?證件識(shí)別上傳組件封裝功能
證件圖片識(shí)別上傳根據(jù)業(yè)務(wù)需要,經(jīng)常涉及到證件上傳,例如身份證上傳、銀行卡、營(yíng)業(yè)執(zhí)照等信息,根據(jù)設(shè)計(jì)師的設(shè)計(jì),單獨(dú)封裝了一個(gè)上傳組件。識(shí)別接口后端用的是阿里云的。
上傳組件用的是 element-plus el-upload 上代碼:
<template>
<div class="component-upload-image">
<el-upload
:action="uploadUrl"
list-type="picture-card"
:on-success="handleUploadSuccess"
:before-upload="handleBeforeUpload"
:limit="limit"
:on-error="handleUploadError"
:on-exceed="handleExceed"
ref="imageUpload"
:before-remove="handleDelete"
:data="params"
name="file"
:show-file-list="true"
:headers="headers"
v-model:file-list="fileList"
:on-preview="handlePictureCardPreview"
:class="{ hide: fileList.length >= limit }">
<el-icon class="avatar-uploader-icon">
<plus />
</el-icon>
</el-upload>
<!-- 上傳提示 -->
<div class="el-upload__tip" v-if="showTip">
請(qǐng)上傳
<template v-if="fileSize">
大小不超過(guò) <b style="color: #f56c6c">{{ fileSize }}MB</b>
</template>
<template v-if="fileType">
格式為 <b style="color: #f56c6c">{{ fileType.join("、") }}</b>
</template>
的文件
</div>
<template v-if="showTipButton">
<el-button link type="primary" @click="showDiagram = !showDiagram" icon="InfoFilled">查看示意圖</el-button>
</template>
<el-image class="tip-img" :src="diagram" fit="contain" :preview-src-list="[diagram]" v-if="showDiagram" :preview-teleported="true"></el-image>
<el-dialog v-model="dialogVisible" title="預(yù)覽" width="800px" append-to-body>
<img :src="dialogImageUrl" style="display: block; max-width: 100%; margin: 0 auto" />
</el-dialog>
</div>
</template>
<script setup name="CertificateUpload">
import { getToken } from "@/utils/auth";
const props = defineProps({
// 是否顯示示意圖
showTipButton: {
type: Boolean,
default: true
},
// 示意圖
diagram: {
type: String,
default: '',
},
//識(shí)別接口
action:{
type: String,
default: '',
},
modelValue: [String, Object, Array],
limit: {
type: Number,
default: 1,
},
// 大小限制(MB)
fileSize: {
type: Number,
default: 5,
},
// 文件類(lèi)型, 例如['png', 'jpg', 'jpeg']
fileType: {
type: Array,
default: () => ['png','jpg','jpeg','bmp'],
},
// 是否顯示提示
isShowTip: {
type: Boolean,
default: true
},
});
const { proxy } = getCurrentInstance();
const emit = defineEmits(['update:modelValue','certificateInfo']);
const showDiagram = ref(false)
const dialogImageUrl = ref("");
const dialogVisible = ref(false);
const baseUrl = import.meta.env.VITE_APP_BASE_API;
// const uploadUrl = ref(baseUrl + props.action); // 上傳的圖片服務(wù)器地址
const headers = ref({ Authorization: "Bearer " + getToken() , repeatSubmit : false});
const uploadUrl = computed( //上傳的圖片服務(wù)器地址
() => baseUrl + props.action
);
const showTip = computed(
() => props.isShowTip && (props.fileType || props.fileSize)
);
const params = ref({})
const fileList = computed({
get: () => {
if(props.modelValue){
// 首先將值轉(zhuǎn)為數(shù)組
if(!Array.isArray(props.modelValue)){
proxy.$modal.msgError('modelValue 必須是數(shù)組 { url ossId name }[]')
return []
}
return props.modelValue
}else{
return []
}
},
set: (newVal) => {}
});
// 上傳前l(fā)oading加載
function handleBeforeUpload(file) {
try {
let isImg = false;
if (props.fileType.length) {
let fileExtension = "";
if (file.name.lastIndexOf(".") > -1) {
fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
}
isImg = props.fileType.some(type => {
if (file.type.indexOf(type) > -1) return true;
if (fileExtension && fileExtension.indexOf(type) > -1) return true;
return false;
});
} else {
isImg = file.type.indexOf("image") > -1;
}
if (!isImg) {
proxy.$modal.msgError(
`文件格式不正確, 請(qǐng)上傳${props.fileType.join("/")}圖片格式文件!`
);
return false;
}
if (props.fileSize) {
const isLt = file.size / 1024 / 1024 < props.fileSize;
if (!isLt) {
proxy.$modal.msgError(`上傳頭像圖片大小不能超過(guò) ${props.fileSize} MB!`);
return false;
}
}
params.value.name = file.name.replace(/\.[a-zA-Z]+$/g, '') + '_' + new Date().getTime() // 帶上時(shí)間戳,避免文件地址重復(fù)被覆蓋
proxy.$modal.loading("正在上傳圖片,請(qǐng)稍候...");
} catch (error) {
proxy.$modal.closeLoading();
console.log(error)
}
}
// 文件個(gè)數(shù)超出
function handleExceed() {
proxy.$modal.msgError(`上傳文件數(shù)量不能超過(guò) ${props.limit} 個(gè)!`);
}
// 上傳成功回調(diào)
function handleUploadSuccess(res, file, fileList) {
try {
if (res.code === 200) {
// 多張
// const all = [res.data.file.url, ...fileList.filter(z=>!z.response).map(u=>u.url)].join(',')
emit("update:modelValue", [{
url: res.data.oss.url,
name: res.data.oss.fileName,
ossId: res.data.oss.ossId
}])
emit("certificateInfo",res.data.recognition.data)
proxy.$modal.closeLoading();
} else {
proxy.$modal.closeLoading();
proxy.$modal.msgError(res.msg);
proxy.$refs.imageUpload.handleRemove(file);
}
} catch (error) {
proxy.$modal.closeLoading();
console.log(error)
}
}
// 刪除圖片
function handleDelete(file) {
const findex = fileList.value.map(f => f.url).indexOf(file.url);
if (findex > -1) {
fileList.value.splice(findex, 1);
emit("update:modelValue", fileList.value.map(z => z.url).join(','))
return false;
}
}
// 上傳失敗
function handleUploadError() {
proxy.$modal.msgError("上傳圖片失敗");
proxy.$modal.closeLoading();
}
// 預(yù)覽
function handlePictureCardPreview(file) {
dialogImageUrl.value = file.url;
dialogVisible.value = true;
}
</script>
<style scoped lang="scss">
:deep(.hide .el-upload--picture-card) {
display: none;
}
</style>到此這篇關(guān)于vue3 證件識(shí)別上傳組件封裝的文章就介紹到這了,更多相關(guān)vue3 證件識(shí)別上傳內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue3?Watch踩坑實(shí)戰(zhàn)之watch監(jiān)聽(tīng)無(wú)效
Vue.js中的watch選項(xiàng)用于監(jiān)聽(tīng)Vue實(shí)例上某個(gè)特定的數(shù)據(jù)變化,下面這篇文章主要給大家介紹了關(guān)于Vue3?Watch踩坑實(shí)戰(zhàn)之watch監(jiān)聽(tīng)無(wú)效的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
在vscode 中設(shè)置 vue模板內(nèi)容的方法
這篇文章主要介紹了在vscode 中設(shè)置 vue模板內(nèi)容的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Vue+Bootstrap收藏(點(diǎn)贊)功能邏輯與具體實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Vue+Bootstrap收藏(點(diǎn)贊)功能邏輯與具體實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10
Vue的transition-group與Virtual Dom Diff算法的使用
這篇文章主要介紹了Vue的transition-group與Virtual Dom Diff算法的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Vue 進(jìn)入/離開(kāi)動(dòng)畫(huà)效果
這篇文章主要介紹了Vue 進(jìn)入/離開(kāi)動(dòng)畫(huà)效果,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-12-12
Vue使用正則校驗(yàn)文本框?yàn)檎麛?shù)
這篇文章主要介紹了Vue使用正則校驗(yàn)文本框?yàn)檎麛?shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10

