feat: improve filtered exports and server pagination
This commit is contained in:
1 parent
4e37f3bb5c
commit
1ddfecbecc
26 files changed
+645
-120
No files matched your search
+24
-2
@@ -9,6 +9,24 @@ function responseFileName(header: string | undefined, fallback: string) {
|
||||
return plain || fallback
|
||||
}
|
||||
|
||||
function uniqueFallbackName(path: string, fallback: string) {
|
||||
if (/template|模板/i.test(path) || !fallback.includes('.')) return fallback
|
||||
const dot = fallback.lastIndexOf('.')
|
||||
const now = new Date()
|
||||
const parts = new Intl.DateTimeFormat('zh-CN', {
|
||||
timeZone: 'Asia/Shanghai', year: 'numeric', month: '2-digit', day: '2-digit',
|
||||
hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false,
|
||||
}).formatToParts(now).reduce<Record<string, string>>((result, item) => {
|
||||
result[item.type] = item.value
|
||||
return result
|
||||
}, {})
|
||||
const milliseconds = String(now.getMilliseconds()).padStart(3, '0')
|
||||
const random = crypto.getRandomValues(new Uint8Array(3))
|
||||
const code = Array.from(random, value => value.toString(16).padStart(2, '0')).join('').toUpperCase()
|
||||
const suffix = `${parts.year}${parts.month}${parts.day}-${parts.hour}${parts.minute}${parts.second}-${milliseconds}-${code}`
|
||||
return `${fallback.slice(0, dot)}-${suffix}${fallback.slice(dot)}`
|
||||
}
|
||||
|
||||
export async function downloadApiFile(
|
||||
path: string,
|
||||
fallbackName: string,
|
||||
@@ -21,7 +39,9 @@ export async function downloadApiFile(
|
||||
const url = URL.createObjectURL(response.data)
|
||||
const anchor = document.createElement('a')
|
||||
anchor.href = url
|
||||
anchor.download = responseFileName(response.headers['content-disposition'], fallbackName)
|
||||
anchor.download = responseFileName(
|
||||
response.headers['content-disposition'], uniqueFallbackName(path, fallbackName),
|
||||
)
|
||||
document.body.appendChild(anchor)
|
||||
anchor.click()
|
||||
anchor.remove()
|
||||
@@ -37,7 +57,9 @@ export async function downloadApiPostFile(
|
||||
const url = URL.createObjectURL(response.data)
|
||||
const anchor = document.createElement('a')
|
||||
anchor.href = url
|
||||
anchor.download = responseFileName(response.headers['content-disposition'], fallbackName)
|
||||
anchor.download = responseFileName(
|
||||
response.headers['content-disposition'], uniqueFallbackName(path, fallbackName),
|
||||
)
|
||||
document.body.appendChild(anchor)
|
||||
anchor.click()
|
||||
anchor.remove()
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import http from './http'
|
||||
|
||||
export async function fetchAllPagedItems<T = any>(
|
||||
url: string,
|
||||
params: Record<string, unknown> = {},
|
||||
pageSize = 100,
|
||||
): Promise<T[]> {
|
||||
const items: T[] = []
|
||||
let page = 1
|
||||
|
||||
for (;;) {
|
||||
const response = await http.get(url, { params: { ...params, page, pageSize } })
|
||||
const batch = (response.data?.items ?? []) as T[]
|
||||
items.push(...batch)
|
||||
const total = Number(response.data?.total ?? items.length)
|
||||
if (batch.length === 0 || items.length >= total) return items
|
||||
page++
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user