Files
bj_power/bj_power_wms/frontend/src/components/AttachmentPanel.vue
T

138 lines
4.6 KiB
Vue
Raw Normal View History

2026-09-15 16:37:39 +08:00
<script setup>
// 通用附件区(内嵌在业务页里使用,不建独立"附件中心"菜单)
// 用法:<AttachmentPanel biz-type="inbound" :biz-id="row.inboundNo" />
defineOptions({ name: 'AttachmentPanel' })
import { ref, watch } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import request from '../utils/request'
import { fmtTime } from '../utils/format'
const props = defineProps({
bizType: { type: String, required: true },
bizId: { type: String, default: '' },
title: { type: String, default: '附件' },
disabled: { type: Boolean, default: false },
})
const list = ref([])
const loading = ref(false)
const uploading = ref(false)
async function load() {
if (!props.bizId) {
list.value = []
return
}
loading.value = true
try {
const d = await request.get('/attachments', { params: { bizType: props.bizType, bizId: props.bizId } })
list.value = d?.list || []
} catch (e) {
list.value = []
} finally {
loading.value = false
}
}
// 自定义上传:走统一 axios(自动带 Bearer token + 统一错误提示)
async function doUpload(opt) {
const fd = new FormData()
fd.append('file', opt.file)
fd.append('bizType', props.bizType)
fd.append('bizId', props.bizId)
uploading.value = true
try {
await request.post('/attachments/upload', fd)
ElMessage.success('上传成功')
await load()
} finally {
uploading.value = false
}
}
function preview(row) {
window.open(row.url, '_blank')
}
// 下载:后端 /uploads/{id}?dl=1 返回 Content-Disposition: attachment,用临时 <a> 触发真实下载(绕开弹窗拦截)
function download(row) {
const a = document.createElement('a')
a.href = row.url + '?dl=1'
a.download = row.fileName || ''
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
}
2026-09-15 16:37:39 +08:00
async function remove(row) {
try {
await ElMessageBox.confirm(`确认删除附件「${row.fileName}」?`, '删除确认', { type: 'warning' })
} catch {
return
}
await request.post('/attachments/delete', { id: row.id })
ElMessage.success('已删除')
await load()
}
function sizeText(n) {
if (!n) return '-'
if (n < 1024) return n + ' B'
if (n < 1024 * 1024) return (n / 1024).toFixed(1) + ' KB'
return (n / 1024 / 1024).toFixed(2) + ' MB'
}
watch(() => [props.bizType, props.bizId], load, { immediate: true })
defineExpose({ load })
</script>
<template>
<div class="attachment-panel">
<div class="ap-head">
<span class="ap-title">{{ title }}</span>
<el-upload
:auto-upload="true"
:show-file-list="false"
:http-request="doUpload"
accept=".pdf,.png,.jpg,.jpeg,.gif,.xlsx,.xls,.doc,.docx,.dwg,.dxf,.zip,.txt,.csv"
:disabled="!bizId || disabled"
>
<el-button type="primary" plain size="small" :loading="uploading" :disabled="!bizId || disabled">
上传附件
</el-button>
</el-upload>
<span class="ap-tip">支持 PDF / 图纸 / 图片 / Office,单个不超过 20MB;点文件名或「下载」下载,点「查看」新窗口预览</span>
2026-09-15 16:37:39 +08:00
</div>
<el-table :data="list" size="small" border v-loading="loading" empty-text="暂无附件">
<el-table-column label="文件名" min-width="200" show-overflow-tooltip>
<template #default="{ row }">
<el-link type="primary" :underline="false" @click="download(row)">{{ row.fileName }}</el-link>
</template>
</el-table-column>
2026-09-15 16:37:39 +08:00
<el-table-column label="大小" width="100" align="center">
<template #default="{ row }">{{ sizeText(row.fileSize) }}</template>
</el-table-column>
<el-table-column prop="uploadedBy" label="上传人" width="110" align="center">
<template #default="{ row }">{{ row.uploadedBy || '-' }}</template>
</el-table-column>
<el-table-column label="上传时间" width="165" align="center">
<template #default="{ row }">{{ fmtTime(row.createdAt) }}</template>
</el-table-column>
<el-table-column label="操作" width="170" align="center">
2026-09-15 16:37:39 +08:00
<template #default="{ row }">
<el-button link type="primary" size="small" @click="preview(row)">查看</el-button>
<el-button link type="primary" size="small" @click="download(row)">下载</el-button>
2026-09-15 16:37:39 +08:00
<el-button link type="danger" size="small" :disabled="disabled" @click="remove(row)">删除</el-button>
</template>
</el-table-column>
</el-table>
</div>
</template>
<style scoped>
.attachment-panel { margin-top: 6px; }
.ap-head { display: flex; align-items: center; gap: 12px; margin-bottom: 8px; }
.ap-title { font-weight: 600; color: #303133; }
.ap-tip { color: #909399; font-size: 12px; }
</style>