174 lines
5.4 KiB
JavaScript
174 lines
5.4 KiB
JavaScript
import request from './request';
|
||
|
||
/**
|
||
* 识别身份证正面或反面
|
||
* @param {string} imageUrl - 身份证图片URL
|
||
* @param {number} type - 1: 正面, 2: 反面
|
||
* @returns {Promise<object>} - 识别结果
|
||
*/
|
||
export const recognizeIdCard = async (imageUrl, type = 1) => {
|
||
try {
|
||
const result = await request.post('/api/recognize/idCard', {
|
||
img_url: imageUrl,
|
||
type: type
|
||
});
|
||
|
||
if (result.code !== 200) {
|
||
throw new Error(result.msg || '身份证识别失败');
|
||
}
|
||
// 验证是否真的识别到了有效信息
|
||
const hasValidData = validateIdCardResult(result.data, type);
|
||
|
||
if (!hasValidData) {
|
||
throw new Error(`未识别到有效的身份证${type === 1 ? '正面' : '反面'}信息,请确保上传的是清晰的身份证照片`);
|
||
}
|
||
|
||
return result.data;
|
||
} catch (error) {
|
||
console.error('身份证识别失败:', error);
|
||
throw new Error(`未识别到有效的身份证${type === 1 ? '正面' : '反面'}信息,请确保上传的是清晰的身份证照片`);
|
||
}
|
||
};
|
||
|
||
/**
|
||
* 验证身份证识别结果是否有效
|
||
* @param {Object} data - 识别结果
|
||
* @param {number} type - 1: 正面, 2: 反面
|
||
* @returns {boolean} 是否有效
|
||
*/
|
||
const validateIdCardResult = (data, type) => {
|
||
if (type === 1) {
|
||
// 正面需要验证姓名和身份证号
|
||
return (data.name && data.name.trim()) || (data.id_number && data.id_number.trim());
|
||
} else {
|
||
// 反面需要验证有效期
|
||
return (data.start_date && data.start_date.trim()) || (data.end_date && data.end_date.trim());
|
||
}
|
||
};
|
||
|
||
/**
|
||
* 识别营业执照
|
||
* @param {string} imageUrl - 营业执照图片URL
|
||
* @returns {Promise<object>} 识别结果
|
||
*/
|
||
export const recognizeBusinessLicense = async (imageUrl) => {
|
||
try {
|
||
console.log('发送营业执照识别请求,图片URL:', imageUrl);
|
||
|
||
const result = await request.post('/api/recognize/uscc', {
|
||
img_url: imageUrl
|
||
});
|
||
|
||
console.log('营业执照识别接口完整响应:', JSON.stringify(result, null, 2));
|
||
|
||
if (result.code !== 200) {
|
||
throw new Error(result.msg || '营业执照识别失败');
|
||
}
|
||
|
||
console.log('接口返回的 uscc_num 字段:', result.data.uscc_num);
|
||
console.log('接口返回的 uscc_name 字段:', result.data.uscc_name);
|
||
console.log('接口返回的 uscc_type 字段:', result.data.uscc_type);
|
||
|
||
// 验证是否真的识别到了有效信息
|
||
const hasValidData = validateBusinessLicenseResult(result.data);
|
||
|
||
if (!hasValidData) {
|
||
throw new Error('未识别到有效的营业执照信息,请确保上传的是清晰的营业执照照片');
|
||
}
|
||
|
||
// 直接返回接口的所有数据,不做映射
|
||
return {
|
||
uscc_num: result.data.uscc_num || '',
|
||
uscc_name: result.data.uscc_name || '',
|
||
legal_name: result.data.legal_name || '',
|
||
address: result.data.address || '',
|
||
op_scope: result.data.op_scope || '',
|
||
establishing_date: result.data.establishing_date || '',
|
||
uscc_type: result.data.uscc_type || '' // 直接返回原始企业类型
|
||
};
|
||
} catch (error) {
|
||
console.error('营业执照识别失败:', error);
|
||
throw new Error('未识别到有效的营业执照信息,请确保上传的是清晰的营业执照照片');
|
||
}
|
||
};
|
||
|
||
/**
|
||
* 验证营业执照识别结果是否有效
|
||
* @param {Object} data - 识别结果
|
||
* @returns {boolean} 是否有效
|
||
*/
|
||
const validateBusinessLicenseResult = (data) => {
|
||
// 检查关键字段是否有值
|
||
const requiredFields = ['uscc_num', 'uscc_name'];
|
||
let hasValidData = false;
|
||
|
||
for (const field of requiredFields) {
|
||
if (data[field] && data[field].trim()) {
|
||
hasValidData = true;
|
||
break;
|
||
}
|
||
}
|
||
|
||
return hasValidData;
|
||
};
|
||
|
||
/**
|
||
* 格式化日期字符串
|
||
* @param {string} dateStr - 日期字符串(YYYYMMDD格式)
|
||
* @returns {string} - 格式化的日期 "YYYY年MM月DD日"
|
||
*/
|
||
export const formatDateFromNumber = (dateStr) => {
|
||
if (!dateStr) return '';
|
||
|
||
// 处理8位数字格式
|
||
if (/^\d{8}$/.test(dateStr)) {
|
||
const year = dateStr.substring(0, 4);
|
||
const month = dateStr.substring(4, 6);
|
||
const day = dateStr.substring(6, 8);
|
||
return `${year}年${parseInt(month)}月${parseInt(day)}日`;
|
||
}
|
||
|
||
// 处理其他格式
|
||
return dateStr;
|
||
};
|
||
|
||
/**
|
||
* 解析身份证有效期
|
||
* @param {string} startDate - 开始日期
|
||
* @param {string} endDate - 结束日期
|
||
* @returns {Object} { startDate, endDate, isPermanent }
|
||
*/
|
||
export const parseValidPeriod = (startDate, endDate) => {
|
||
const isPermanent = endDate === '长期' || endDate === '永久' || endDate === '长久有效';
|
||
|
||
return {
|
||
startDate: formatDateFromNumber(startDate),
|
||
endDate: isPermanent ? '长久有效' : formatDateFromNumber(endDate),
|
||
isPermanent: isPermanent
|
||
};
|
||
};
|
||
|
||
/**
|
||
* 验证身份证号码
|
||
* @param {string} idNumber - 身份证号码
|
||
* @returns {boolean} - 是否有效
|
||
*/
|
||
export const validateIdNumber = (idNumber) => {
|
||
if (!idNumber) return false;
|
||
|
||
// 简单验证:15位或18位
|
||
const reg = /(^\d{15}$)|(^\d{17}(\d|X|x)$)/;
|
||
return reg.test(idNumber);
|
||
};
|
||
|
||
/**
|
||
* 验证营业执照号码(统一社会信用代码)
|
||
* @param {string} usccNum - 统一社会信用代码
|
||
* @returns {boolean} - 是否有效
|
||
*/
|
||
export const validateUsccNum = (usccNum) => {
|
||
if (!usccNum) return false;
|
||
|
||
// 统一社会信用代码为18位
|
||
return usccNum.length === 18;
|
||
}; |