vin-api-valid-tracker.js 1.33 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
}

/**
 * @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)
		const valid = result?.data?.data?.valid
		return { ok: true, valid: valid !== false }
	} catch {
		return { ok: false, valid: false }
	}
}

export default {
	buildVinErrorKey,
	shouldRunVinApiValidate,
	runVinApiValidate
}

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