2026-05-12 03:05:51 +00:00
|
|
|
import { apiRequest } from './api.js'
|
|
|
|
|
|
2026-06-06 17:19:07 +08:00
|
|
|
const inflightOcrRequests = new Map()
|
|
|
|
|
|
|
|
|
|
function buildOcrRequestKey(files = []) {
|
|
|
|
|
return files
|
|
|
|
|
.map((file) => [
|
|
|
|
|
String(file?.name || ''),
|
|
|
|
|
String(file?.size || 0),
|
|
|
|
|
String(file?.lastModified || 0),
|
|
|
|
|
String(file?.receiptId || '')
|
|
|
|
|
].join(':'))
|
|
|
|
|
.join('|')
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 08:58:59 +08:00
|
|
|
export function recognizeOcrFiles(files, options = {}) {
|
2026-06-06 17:19:07 +08:00
|
|
|
const requestKey = buildOcrRequestKey(files)
|
|
|
|
|
if (requestKey && inflightOcrRequests.has(requestKey)) {
|
|
|
|
|
return inflightOcrRequests.get(requestKey)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-12 03:05:51 +00:00
|
|
|
const formData = new FormData()
|
|
|
|
|
for (const file of files) {
|
|
|
|
|
formData.append('files', file)
|
2026-05-29 14:51:18 +08:00
|
|
|
formData.append('receipt_ids', String(file?.receiptId || ''))
|
2026-05-12 03:05:51 +00:00
|
|
|
}
|
|
|
|
|
|
2026-06-06 17:19:07 +08:00
|
|
|
const request = apiRequest('/ocr/recognize', {
|
2026-05-12 03:05:51 +00:00
|
|
|
method: 'POST',
|
|
|
|
|
body: formData,
|
2026-05-22 08:58:59 +08:00
|
|
|
contentType: null,
|
|
|
|
|
...options
|
2026-05-12 03:05:51 +00:00
|
|
|
})
|
2026-06-06 17:19:07 +08:00
|
|
|
if (!requestKey) {
|
|
|
|
|
return request
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inflightOcrRequests.set(requestKey, request)
|
|
|
|
|
request.then(
|
|
|
|
|
() => inflightOcrRequests.delete(requestKey),
|
|
|
|
|
() => inflightOcrRequests.delete(requestKey)
|
|
|
|
|
)
|
|
|
|
|
return request
|
2026-05-12 03:05:51 +00:00
|
|
|
}
|