vin-api-valid-tracker.js 3.9 KB
/** 车架号 API 异步校验状态 — 对齐 createOrder vinValid + validateVin */

import { buildGroupErrorKey } from './group-values.js'

export function buildVinErrorKey({ groupKey, rowIndex, childKey } = {}) {
	if (groupKey != null && rowIndex != null && childKey) {
		return buildGroupErrorKey(groupKey, rowIndex, childKey)
	}
	return childKey || ''
}

export function shouldRunVinApiValidate(vin) {
	const str = String(vin ?? '').trim()
	return str.length === 17
}

function resolveVinApiValid(result) {
	const valid = result?.data?.data?.valid
	return { ok: true, valid: valid !== false }
}

/**
 * @param {string} vin
 * @param {Function} validateVinFn - utils/vinVaildate.validateVin
 * @returns {Promise<{ ok: boolean, valid: boolean }>}
 */
export async function runVinApiValidate(vin, validateVinFn) {
	if (!shouldRunVinApiValidate(vin)) {
		return { ok: true, valid: true }
	}
	if (typeof validateVinFn !== 'function') {
		return { ok: false, valid: false }
	}
	try {
		const result = await validateVinFn(vin)
		return resolveVinApiValid(result)
	} catch (err) {
		if (err?.cancelled) {
			return { ok: false, valid: false, cancelled: true }
		}
		return { ok: false, valid: false }
	}
}

/**
 * 提交前批量校验:无防抖,默认 silent
 * @param {string} vin
 * @param {Function} validateVinImmediateFn - utils/vinVaildate.validateVinImmediate
 * @param {{ silent?: boolean }} [options]
 * @returns {Promise<{ ok: boolean, valid: boolean }>}
 */
export async function runVinApiValidateImmediate(
	vin,
	validateVinImmediateFn,
	options = {}
) {
	if (!shouldRunVinApiValidate(vin)) {
		return { ok: true, valid: true }
	}
	if (typeof validateVinImmediateFn !== 'function') {
		return { ok: false, valid: false }
	}
	const silent = options.silent !== false
	try {
		const result = await validateVinImmediateFn(vin, { silent })
		return resolveVinApiValid(result)
	} catch {
		return { ok: false, valid: false }
	}
}

/**
 * 从 groupRowsCache 收集需 API 校验的 VIN 单元格
 * @returns {Array<{ errorKey: string, vin: string }>}
 */
export function listVinScanCellsFromCache(groupRowsCache = {}, buildKey = buildVinErrorKey) {
	const entries = []
	Object.keys(groupRowsCache || {}).forEach(groupKey => {
		;(groupRowsCache[groupKey] || []).forEach((rowFields, rowIndex) => {
			;(rowFields || []).forEach(child => {
				if (child?.type !== 'input' || child.prefixComponent !== 'scan') {
					return
				}
				const vin = child.value
				if (!shouldRunVinApiValidate(vin)) {
					return
				}
				entries.push({
					errorKey: buildKey({ groupKey, rowIndex, childKey: child.key }),
					vin
				})
			})
		})
	})
	return entries
}

/**
 * 批量跑 VIN API 校验并返回 errorKey → 是否通过
 * @param {Array<{ errorKey: string, vin: string }>} entries
 * @param {Function} runOne - (vin) => Promise<{ ok, valid }>
 * @returns {Promise<Record<string, boolean>>}
 */
export async function buildVinApiValidMapFromEntries(entries = [], runOne) {
	const map = {}
	if (!entries.length || typeof runOne !== 'function') {
		return map
	}
	const results = await Promise.all(
		entries.map(async entry => {
			const { ok, valid, cancelled } = await runOne(entry.vin)
			return {
				errorKey: entry.errorKey,
				passed: !cancelled && ok && valid
			}
		})
	)
	results.forEach(({ errorKey, passed }) => {
		if (errorKey) {
			map[errorKey] = passed
		}
	})
	return map
}

export default {
	buildVinErrorKey,
	shouldRunVinApiValidate,
	runVinApiValidate,
	runVinApiValidateImmediate,
	listVinScanCellsFromCache,
	buildVinApiValidMapFromEntries
}

if (typeof module !== 'undefined' && module.exports) {
	module.exports = {
		buildVinErrorKey,
		shouldRunVinApiValidate,
		runVinApiValidate,
		runVinApiValidateImmediate,
		listVinScanCellsFromCache,
		buildVinApiValidMapFromEntries,
		default: {
			buildVinErrorKey,
			shouldRunVinApiValidate,
			runVinApiValidate,
			runVinApiValidateImmediate,
			listVinScanCellsFromCache,
			buildVinApiValidMapFromEntries
		}
	}
}