<template>
|
<div>
|
<el-dialog :title="dialogTitle" v-model="visible" width="760px">
|
<div class="attachment-header">
|
<div class="el-upload__tip">{{ tip }}</div>
|
<el-upload :action="uploadAction" :headers="uploadHeader" multiple :show-file-list="false"
|
:on-success="handleUploadSuccess" :on-error="handleUploadError">
|
<el-button size="small" type="primary">上传附件</el-button>
|
</el-upload>
|
</div>
|
<el-table :data="attachmentList" border size="small" style="margin-top:12px">
|
<el-table-column label="文件名称" prop="fileName" min-width="280" show-overflow-tooltip />
|
<el-table-column label="上传时间" prop="createTime" width="170" />
|
<el-table-column label="操作" width="180">
|
<template #default="scope">
|
<el-button link type="primary" @click="previewFile(scope.row)">预览</el-button>
|
<el-button link type="primary" @click="downloadFile(scope.row)">下载</el-button>
|
<el-button link type="danger" @click="removeFile(scope.row)">删除</el-button>
|
</template>
|
</el-table-column>
|
</el-table>
|
<template #footer>
|
<el-button @click="visible = false">关 闭</el-button>
|
</template>
|
</el-dialog>
|
<el-dialog title="预览附件" v-model="previewVisible" fullscreen append-to-body>
|
<file-preview v-if="previewVisible" :file-url="previewUrl" :current-file="previewRow" />
|
</el-dialog>
|
</div>
|
</template>
|
|
<script>
|
import request from '@/utils/request'
|
import filePreview from '@/components/Preview/filePreview.vue'
|
|
/**
|
* 通用附件管理组件(上传 / 预览 / 下载 / 删除)
|
*
|
* 用法:页面操作列放三个入口,分别调用本组件的方法:
|
* 上传附件 -> this.$refs.xxx.open(row) 打开附件弹窗(弹窗内可上传)
|
* 下载附件 -> this.$refs.xxx.downloadAttachments(row) 只有一个附件直接下载,多个打开弹窗
|
* 预览附件 -> this.$refs.xxx.previewAttachments(row) 只有一个附件直接预览,多个打开弹窗
|
*
|
* 后端接口需按统一约定提供(参照 /supplierManagement 的附件接口):
|
* 上传:POST {uploadUrl}?{idParam}=主表ID 表单参数 file
|
* 列表:GET {selectUrl}?{idParam}=主表ID 返回 [{id, fileName, fileUrl, createTime, ...}]
|
* 删除:DELETE {deleteUrl}?id=附件ID
|
*/
|
export default {
|
name: 'LimsAttachment',
|
components: {
|
filePreview
|
},
|
props: {
|
// 上传接口地址,如 /supplierManagement/uploadAttachment
|
uploadUrl: {
|
type: String,
|
required: true
|
},
|
// 查询附件列表接口地址
|
selectUrl: {
|
type: String,
|
required: true
|
},
|
// 删除附件接口地址
|
deleteUrl: {
|
type: String,
|
required: true
|
},
|
// 关联主表ID在接口里的参数名,如 supplierManagementId
|
idParam: {
|
type: String,
|
default: 'id'
|
},
|
// 关联主表ID在行数据里的字段名,不传时默认与 idParam 相同
|
idField: {
|
type: String,
|
default: ''
|
},
|
// 弹窗标题上要显示名称的行字段,如 supplierName
|
nameField: {
|
type: String,
|
default: ''
|
},
|
// 弹窗标题前缀
|
title: {
|
type: String,
|
default: '附件'
|
},
|
// 弹窗左上角提示文字
|
tip: {
|
type: String,
|
default: '支持任意格式文件,单个文件不超过200MB'
|
}
|
},
|
data() {
|
return {
|
visible: false,
|
attachmentList: [],
|
currentRow: {},
|
previewVisible: false,
|
previewRow: {},
|
previewUrl: ''
|
}
|
},
|
computed: {
|
currentId() {
|
const field = this.idField || this.idParam
|
return this.currentRow ? this.currentRow[field] : undefined
|
},
|
dialogTitle() {
|
const name = this.nameField && this.currentRow ? this.currentRow[this.nameField] : ''
|
return this.title + (name ? ' - ' + name : '')
|
},
|
uploadAction() {
|
return this.javaApi + this.uploadUrl + '?' + this.idParam + '=' + this.currentId
|
}
|
},
|
methods: {
|
// 打开附件管理弹窗
|
open(row) {
|
this.currentRow = row
|
this.visible = true
|
this.loadList()
|
},
|
// 预览附件入口:只有一个附件直接预览,多个打开弹窗
|
previewAttachments(row) {
|
this.queryList(row).then(list => {
|
if (!list.length) {
|
this.$message.warning('暂无附件')
|
} else if (list.length === 1) {
|
this.previewFile(list[0])
|
} else {
|
this.open(row)
|
}
|
})
|
},
|
// 下载附件入口:只有一个附件直接下载,多个打开弹窗
|
downloadAttachments(row) {
|
this.queryList(row).then(list => {
|
if (!list.length) {
|
this.$message.warning('暂无附件')
|
} else if (list.length === 1) {
|
this.downloadFile(list[0])
|
} else {
|
this.open(row)
|
}
|
})
|
},
|
queryList(row) {
|
const field = this.idField || this.idParam
|
return request({
|
url: this.selectUrl,
|
method: 'get',
|
params: { [this.idParam]: row[field] }
|
}).then(res => (res && res.data) || [])
|
},
|
loadList() {
|
this.queryList(this.currentRow).then(list => {
|
this.attachmentList = list
|
})
|
},
|
handleUploadSuccess(res) {
|
if (res.code === 200) {
|
this.$message.success('上传成功')
|
this.loadList()
|
} else {
|
this.$message.error(res.msg || '上传失败')
|
}
|
},
|
handleUploadError() {
|
this.$message.error('上传失败,文件可能超过200MB')
|
},
|
previewFile(row) {
|
this.previewRow = row
|
this.previewUrl = this.fullUrl(row.fileUrl)
|
this.previewVisible = true
|
},
|
downloadFile(row) {
|
this.$download.saveAs(this.fullUrl(row.fileUrl), row.fileName)
|
},
|
removeFile(row) {
|
this.$confirm('是否删除该附件?', '提示', { type: 'warning' }).then(() => {
|
request({
|
url: this.deleteUrl,
|
method: 'delete',
|
params: { id: row.id }
|
}).then(() => {
|
this.$message.success('删除成功')
|
this.loadList()
|
})
|
}).catch(() => {})
|
},
|
// 静态地址拼接,与后端上传统一路由保持一致:图片在 /img,其它都在 /word
|
fullUrl(fileUrl) {
|
const base = /\.(jpg|jpeg|png|gif)$/i.test(fileUrl) ? '/img/' : '/word/'
|
return this.javaApi + base + fileUrl
|
}
|
}
|
}
|
</script>
|
|
<style scoped>
|
.attachment-header {
|
display: flex;
|
justify-content: space-between;
|
align-items: center;
|
}
|
</style>
|