识别身份证

This commit is contained in:
ljx
2025-09-09 09:59:03 +08:00
parent 942375b06b
commit f1339ad082
4 changed files with 171 additions and 77 deletions

View File

@ -41,7 +41,7 @@
</template>
<script setup lang="ts">
import { listByIds, delOss } from '@/api/system/oss';
import { listByIds, delOss, recognizeidCard, recognizeBankCard } from '@/api/system/oss';
import { OssVO } from '@/api/system/oss/types';
import { propTypes } from '@/utils/propTypes';
import { globalHeaders } from '@/utils/request';
@ -69,11 +69,15 @@ const props = defineProps({
default: false
},
// 压缩目标大小单位KB。默认300KB以上文件才压缩并压缩至300KB以内
compressTargetSize: propTypes.number.def(300)
compressTargetSize: propTypes.number.def(300),
idCardType: {
type: String,
default: false
}
});
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const emit = defineEmits(['update:modelValue']);
const emit = defineEmits(['update:modelValue', 'success']);
const number = ref(0);
const uploadList = ref<any[]>([]);
const dialogImageUrl = ref('');
@ -121,7 +125,7 @@ watch(
);
/** 上传前loading加载 */
const handleBeforeUpload = (file: any) => {
const handleBeforeUpload = async (file: any) => {
let isImg = false;
if (props.fileType.length) {
let fileExtension = '';
@ -169,10 +173,32 @@ const handleExceed = () => {
};
// 上传成功回调
const handleUploadSuccess = (res: any, file: UploadFile) => {
const handleUploadSuccess = async (res: any, file: UploadFile) => {
if (res.code === 200) {
uploadList.value.push({ name: res.data.fileName, url: res.data.url, ossId: res.data.ossId });
uploadedSuccessfully();
if (props.idCardType) {
const formData = new FormData();
formData.append('file', file.raw); // 假设后端接收的字段名为file
if (props.idCardType === 'front' || props.idCardType === 'back') {
const res = await recognizeidCard(formData, props.idCardType);
if (res.code === 200) {
proxy?.$modal.msgSuccess('身份证识别成功');
emit('success', res.data);
} else {
proxy?.$modal.msgError('身份证识别失败');
}
}
if (props.idCardType === 'bank') {
const res = await recognizeBankCard(formData);
if (res.code === 200) {
proxy?.$modal.msgSuccess('银行卡识别成功');
emit('success', res.data);
} else {
proxy?.$modal.msgError('银行卡识别失败');
}
}
}
} else {
number.value--;
proxy?.$modal.closeLoading();
@ -201,8 +227,7 @@ const uploadedSuccessfully = () => {
fileList.value = fileList.value.filter((f) => f.url !== undefined).concat(uploadList.value);
uploadList.value = [];
number.value = 0;
console.log(fileList.value);
console.log(listToString(fileList.value));
emit('update:modelValue', listToString(fileList.value));
proxy?.$modal.closeLoading();
}