feat(manual-entry): 支持筛选及全量导出 CSV
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
|
||||
const apiClient = vi.hoisted(() => ({
|
||||
get: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('@/shared/api/client', () => ({ apiClient }))
|
||||
|
||||
import { exportManualEntries } from './adminManualEntry'
|
||||
|
||||
describe('manual entry export API', () => {
|
||||
it('exports filtered rows and reads download metadata', async () => {
|
||||
const blob = new Blob(['csv'])
|
||||
const filename = '客服手动录单_20261129-103000.csv'
|
||||
apiClient.get.mockResolvedValueOnce({
|
||||
data: blob,
|
||||
headers: {
|
||||
'content-disposition': `attachment; filename="manual-entry-export.csv"; filename*=UTF-8''${encodeURIComponent(filename)}`,
|
||||
'x-export-row-count': '3',
|
||||
},
|
||||
})
|
||||
|
||||
await expect(
|
||||
exportManualEntries({
|
||||
entry_no: '202611280470',
|
||||
entry_group: 'sell',
|
||||
keyword: '',
|
||||
start_date: '2026-11-01',
|
||||
end_date: undefined,
|
||||
})
|
||||
).resolves.toEqual({ blob, filename, rowCount: 3 })
|
||||
expect(apiClient.get).toHaveBeenCalledWith('/admin/manual-entries/export', {
|
||||
params: {
|
||||
entry_no: '202611280470',
|
||||
entry_group: 'sell',
|
||||
start_date: '2026-11-01',
|
||||
},
|
||||
responseType: 'blob',
|
||||
timeout: 60000,
|
||||
skipErrorHandler: true,
|
||||
})
|
||||
})
|
||||
|
||||
it('exports all rows when no filters are provided', async () => {
|
||||
apiClient.get.mockResolvedValueOnce({
|
||||
data: new Blob(['csv']),
|
||||
headers: {},
|
||||
})
|
||||
|
||||
await exportManualEntries()
|
||||
|
||||
expect(apiClient.get).toHaveBeenCalledWith('/admin/manual-entries/export', {
|
||||
params: {},
|
||||
responseType: 'blob',
|
||||
timeout: 60000,
|
||||
skipErrorHandler: true,
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,3 +1,5 @@
|
||||
import axios from 'axios'
|
||||
|
||||
import { apiClient } from '@/shared/api/client'
|
||||
|
||||
import type { ApiResponse, PaginatedResult } from '@/shared/types/types'
|
||||
@@ -70,6 +72,12 @@ export interface ManualEntryListQuery {
|
||||
end_date?: string
|
||||
}
|
||||
|
||||
function cleanParams(query: object) {
|
||||
return Object.fromEntries(
|
||||
Object.entries(query).filter(([, value]) => value !== '' && value !== undefined)
|
||||
)
|
||||
}
|
||||
|
||||
export async function fetchManualEntries(
|
||||
page = 1,
|
||||
pageSize = 20,
|
||||
@@ -84,6 +92,48 @@ export async function fetchManualEntries(
|
||||
return data.data
|
||||
}
|
||||
|
||||
export async function exportManualEntries(query: ManualEntryListQuery = {}) {
|
||||
try {
|
||||
const response = await apiClient.get<Blob>('/admin/manual-entries/export', {
|
||||
params: cleanParams(query),
|
||||
responseType: 'blob',
|
||||
timeout: 60000,
|
||||
skipErrorHandler: true,
|
||||
})
|
||||
return {
|
||||
blob: response.data,
|
||||
filename: readExportFilename(response.headers['content-disposition']),
|
||||
rowCount: Number(response.headers['x-export-row-count'] || 0),
|
||||
}
|
||||
} catch (error) {
|
||||
if (axios.isAxiosError(error) && error.response?.data instanceof Blob) {
|
||||
const text = await error.response.data.text()
|
||||
try {
|
||||
const payload = JSON.parse(text) as { message?: unknown }
|
||||
if (typeof payload.message === 'string') {
|
||||
error.message = payload.message
|
||||
}
|
||||
} catch {
|
||||
// 非 JSON 错误响应保留原始 Axios 错误。
|
||||
}
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
function readExportFilename(contentDisposition: unknown) {
|
||||
if (typeof contentDisposition !== 'string') return 'manual-entry-export.csv'
|
||||
const encoded = contentDisposition.match(/filename\*=UTF-8''([^;]+)/i)?.[1]
|
||||
if (encoded) {
|
||||
try {
|
||||
return decodeURIComponent(encoded)
|
||||
} catch {
|
||||
return encoded
|
||||
}
|
||||
}
|
||||
return contentDisposition.match(/filename="?([^";]+)"?/i)?.[1] || 'manual-entry-export.csv'
|
||||
}
|
||||
|
||||
export async function createManualEntry(payload: CreateManualEntryRequest) {
|
||||
const { data } = await apiClient.post<ApiResponse<{ entry: ManualEntryItem }>>(
|
||||
'/admin/manual-entries',
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, reactive, ref } from 'vue'
|
||||
import { ElMessage, ElMessageBox } from 'element-plus'
|
||||
import { Download } from '@element-plus/icons-vue'
|
||||
|
||||
import {
|
||||
createManualEntry,
|
||||
createManualEntryOption,
|
||||
deleteManualEntry,
|
||||
deleteManualEntryOption,
|
||||
exportManualEntries,
|
||||
fetchManualEntries,
|
||||
fetchManualEntryOptions,
|
||||
updateManualEntry,
|
||||
@@ -88,6 +90,18 @@ function handleEntryOperatorChange(name: string) {
|
||||
}
|
||||
|
||||
const submitting = ref(false)
|
||||
const exporting = ref(false)
|
||||
|
||||
function currentListQuery() {
|
||||
const [startDate, endDate] = filters.dateRange || []
|
||||
return {
|
||||
keyword: filters.keyword || undefined,
|
||||
entry_no: filters.entry_no || undefined,
|
||||
entry_group: filters.entry_group || undefined,
|
||||
start_date: startDate,
|
||||
end_date: endDate,
|
||||
}
|
||||
}
|
||||
|
||||
const {
|
||||
loading,
|
||||
@@ -98,14 +112,7 @@ const {
|
||||
load: loadEntries,
|
||||
} = useAdminPaginatedTable<ManualEntryItem>({
|
||||
fetchFn: (page, pageSize) => {
|
||||
const [startDate, endDate] = filters.dateRange || []
|
||||
return fetchManualEntries(page, pageSize, {
|
||||
keyword: filters.keyword || undefined,
|
||||
entry_no: filters.entry_no || undefined,
|
||||
entry_group: filters.entry_group || undefined,
|
||||
start_date: startDate,
|
||||
end_date: endDate,
|
||||
})
|
||||
return fetchManualEntries(page, pageSize, currentListQuery())
|
||||
},
|
||||
})
|
||||
|
||||
@@ -138,6 +145,32 @@ function applySearch() {
|
||||
void loadEntries()
|
||||
}
|
||||
|
||||
async function handleExportCommand(command: string) {
|
||||
exporting.value = true
|
||||
try {
|
||||
const result = await exportManualEntries(command === 'all' ? {} : currentListQuery())
|
||||
downloadBlob(result.blob, result.filename)
|
||||
ElMessage.success(
|
||||
result.rowCount > 0 ? `已导出 ${result.rowCount} 条录单记录` : '当前条件下暂无录单记录'
|
||||
)
|
||||
} catch (error) {
|
||||
ElMessage.error(readError(error, '导出失败'))
|
||||
} finally {
|
||||
exporting.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function downloadBlob(blob: Blob, filename: string) {
|
||||
const url = URL.createObjectURL(blob)
|
||||
const link = document.createElement('a')
|
||||
link.href = url
|
||||
link.download = filename
|
||||
document.body.appendChild(link)
|
||||
link.click()
|
||||
document.body.removeChild(link)
|
||||
URL.revokeObjectURL(url)
|
||||
}
|
||||
|
||||
async function handleCreate() {
|
||||
const entryNo = entryForm.entry_no.trim()
|
||||
if (!entryNo) {
|
||||
@@ -412,6 +445,19 @@ async function handleRemoveOption(item: ManualEntryOption) {
|
||||
</p>
|
||||
</div>
|
||||
<div class="page-header-actions">
|
||||
<el-dropdown
|
||||
trigger="click"
|
||||
:disabled="exporting || loading"
|
||||
@command="handleExportCommand"
|
||||
>
|
||||
<el-button :icon="Download" :loading="exporting">导出 CSV</el-button>
|
||||
<template #dropdown>
|
||||
<el-dropdown-menu>
|
||||
<el-dropdown-item command="filtered">导出筛选结果</el-dropdown-item>
|
||||
<el-dropdown-item command="all">导出全部</el-dropdown-item>
|
||||
</el-dropdown-menu>
|
||||
</template>
|
||||
</el-dropdown>
|
||||
<el-button v-if="canManageOptions" :loading="optionsLoading" @click="openConfig">
|
||||
配置平台/操作人/出款人
|
||||
</el-button>
|
||||
|
||||
Reference in New Issue
Block a user