refactor: Orders.vue uses shared composables, fix error handling
This commit is contained in:
@@ -121,9 +121,12 @@ import { ref, onMounted } from 'vue'
|
||||
import { ElMessage, ElMessageBox } from 'element-plus'
|
||||
import { Right } from '@element-plus/icons-vue'
|
||||
import { useProcessingStore } from '../../stores/processing'
|
||||
import { statusType, statusText, fmtTime } from '../../composables/useFileUtils'
|
||||
import { useFilePreview } from '../../composables/useFilePreview'
|
||||
import api from '../../api'
|
||||
|
||||
const processingStore = useProcessingStore()
|
||||
const { showPreview, previewType, previewSrc, previewRows, openPreview, cleanupPreview } = useFilePreview()
|
||||
|
||||
const items = ref<any[]>([])
|
||||
const total = ref(0)
|
||||
@@ -134,76 +137,29 @@ const selected = ref<any[]>([])
|
||||
const sortBy = ref('created_at')
|
||||
const sortOrder = ref('desc')
|
||||
|
||||
// Preview
|
||||
const showPreview = ref(false)
|
||||
const previewType = ref('')
|
||||
const previewSrc = ref('')
|
||||
const previewRows = ref<string[][]>([])
|
||||
|
||||
// Detail
|
||||
const showDetailDlg = ref(false)
|
||||
const detailLogs = ref<string[]>([])
|
||||
|
||||
function statusType(s: string) {
|
||||
const m: Record<string, string> = { done: 'success', merged: 'success', excel_done: 'warning', ocr_done: 'info', pending: 'info' }
|
||||
return m[s] || 'info'
|
||||
}
|
||||
function statusText(s: string) {
|
||||
const m: Record<string, string> = { done: '已完成', merged: '已合并', excel_done: '已处理', ocr_done: '已OCR', pending: '待处理' }
|
||||
return m[s] || s
|
||||
}
|
||||
|
||||
function fmtTime(t: string): string {
|
||||
if (!t) return '--'
|
||||
return t.replace('T', ' ').slice(0, 19)
|
||||
}
|
||||
|
||||
async function loadData() {
|
||||
loading.value = true
|
||||
try {
|
||||
const res = await api.get('/files/relations', { params: { view: 'orders', page: page.value, page_size: pageSize, sort_by: sortBy.value, sort_order: sortOrder.value } })
|
||||
items.value = res.data.items
|
||||
total.value = res.data.total
|
||||
} catch {}
|
||||
} catch {
|
||||
ElMessage.error('加载文件列表失败')
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function onSelect(rows: any[]) { selected.value = rows }
|
||||
|
||||
async function previewFile(row: any) {
|
||||
const token = localStorage.getItem('token')
|
||||
const fname = row.result_purchase || row.output_excel || row.input_image
|
||||
const dir = row.result_purchase ? 'result' : row.output_excel ? 'output' : 'input'
|
||||
const url = `/api/files/preview/${dir}/${encodeURIComponent(fname)}`
|
||||
|
||||
try {
|
||||
const resp = await fetch(url, { headers: { Authorization: `Bearer ${token}` } })
|
||||
const ct = resp.headers.get('content-type') || ''
|
||||
|
||||
if (ct.includes('image')) {
|
||||
previewType.value = 'image'
|
||||
const blob = await resp.blob()
|
||||
previewSrc.value = URL.createObjectURL(blob)
|
||||
} else {
|
||||
const data = await resp.json()
|
||||
if (data.type === 'excel') {
|
||||
previewType.value = 'excel'
|
||||
previewRows.value = data.rows
|
||||
}
|
||||
}
|
||||
showPreview.value = true
|
||||
} catch {
|
||||
ElMessage.error('预览失败')
|
||||
}
|
||||
}
|
||||
|
||||
function cleanupPreview() {
|
||||
if (previewSrc.value && previewSrc.value.startsWith('blob:')) {
|
||||
URL.revokeObjectURL(previewSrc.value)
|
||||
}
|
||||
previewSrc.value = ''
|
||||
previewType.value = ''
|
||||
previewRows.value = []
|
||||
await openPreview(dir, fname)
|
||||
}
|
||||
|
||||
function showDetail(row: any) {
|
||||
@@ -239,7 +195,9 @@ async function deleteFile(row: any) {
|
||||
if (row.id) await api.delete('/files/relations', { data: { ids: [row.id] } })
|
||||
ElMessage.success('已删除')
|
||||
loadData()
|
||||
} catch {}
|
||||
} catch (err: any) {
|
||||
if (err !== 'cancel') ElMessage.error('删除失败')
|
||||
}
|
||||
}
|
||||
|
||||
async function batchMerge() {
|
||||
@@ -275,7 +233,9 @@ async function batchDelete() {
|
||||
}
|
||||
ElMessage.success('批量删除完成')
|
||||
loadData()
|
||||
} catch {}
|
||||
} catch (err: any) {
|
||||
if (err !== 'cancel') ElMessage.error('批量删除失败')
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(loadData)
|
||||
|
||||
Reference in New Issue
Block a user