vin-api-valid-tracker.js
3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/** 车架号 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
}
}
}