Files
shanxia2.0/web-admin/src/utils/pl-paste-recognize.ts
T
2026-08-02 17:18:33 +08:00

128 lines
3.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 三方车辆/人员客户文本粘贴解析
* 标准格式见 `PL_PASTE_SAMPLE_TEXT`。
*/
/**
* 粘贴识别输入框预填示例(格式与解析规则一致,敏感字段已脱敏,供用户对照改写)
* 姓名保留姓、手机保留前 3 后 4、身份证保留前 6 后 4、车牌保留省份简称后打码
*/
export const PL_PASTE_SAMPLE_TEXT = [
'皖****** 皖******',
'核载:34 吨',
'驾驶员:张**',
'手机号:138****0000',
'身份证:340000********0000',
'押运员:李**',
'电话:139****0000',
'身份证:340000********0000'
].join('\n')
export interface PlPastePersonnelDraft {
personnelName: string
phoneNo?: string
idCardNo?: string
personnelType: 'driver' | 'escort'
}
export interface PlPasteVehicleDraft {
licensePlate: string
trailerNo?: string
loadCapacity?: number
}
export interface PlPasteRecognizeResult {
vehicle?: PlPasteVehicleDraft
driver?: PlPastePersonnelDraft
escort?: PlPastePersonnelDraft
warnings: string[]
}
const PLATE_LINE =
/^[\u4e00-\u9fa5A-Z]{1,2}[A-Z0-9]+(?:\s+[\u4e00-\u9fa5A-Z]{1,2}[A-Z0-9]+)?$/i
export function parsePlCustomerVehicleText(raw: string): PlPasteRecognizeResult {
const warnings: string[] = []
const lines = raw
.split(/\r?\n/)
.map(l => l.trim())
.filter(Boolean)
let vehicle: PlPasteVehicleDraft | undefined
let driver: PlPastePersonnelDraft | undefined
let escort: PlPastePersonnelDraft | undefined
let section: 'none' | 'driver' | 'escort' = 'none'
for (const line of lines) {
const normalizedPlateLine = line.replace(/\s+/g, ' ')
if (!vehicle && PLATE_LINE.test(normalizedPlateLine)) {
const parts = normalizedPlateLine.split(/\s+/).filter(Boolean)
vehicle = {
licensePlate: parts[0] || '',
trailerNo: parts[1] || undefined
}
continue
}
const load = line.match(/核载[::]?\s*([\d.]+)\s*吨?/)
if (load) {
if (!vehicle) {
vehicle = { licensePlate: '' }
}
vehicle.loadCapacity = Number(load[1])
continue
}
if (/^驾驶员[::]/.test(line)) {
section = 'driver'
driver = {
personnelName: line.replace(/^驾驶员[::]\s*/, '').trim(),
personnelType: 'driver'
}
continue
}
if (/^押运员[::]/.test(line)) {
section = 'escort'
escort = {
personnelName: line.replace(/^押运员[::]\s*/, '').trim(),
personnelType: 'escort'
}
continue
}
const phone = line.match(/^(?:手机号|电话)[::]?\s*(\d{11})$/)
if (phone) {
if (section === 'driver' && driver) {
driver.phoneNo = phone[1]
continue
}
if (section === 'escort' && escort) {
escort.phoneNo = phone[1]
continue
}
}
const idCard = line.match(/^身份证[::]?\s*([0-9Xx]{15,18})$/)
if (idCard) {
const no = idCard[1].toUpperCase()
if (section === 'driver' && driver) {
driver.idCardNo = no
continue
}
if (section === 'escort' && escort) {
escort.idCardNo = no
continue
}
}
warnings.push(`未识别行:${line}`)
}
if (vehicle && !vehicle.licensePlate.trim()) {
warnings.push('未识别到主车车牌')
}
return { vehicle, driver, escort, warnings }
}