file.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <template>
  2. <div class="app-container">
  3. <el-card shadow="never">
  4. <template #header>
  5. <div class="card-header">
  6. <el-space>
  7. <el-button text @click="$router.back()">← 返回</el-button>
  8. <span>单位档案</span>
  9. </el-space>
  10. <el-button type="primary" size="small" @click="uploadDialogVisible = true">上传文件</el-button>
  11. </div>
  12. </template>
  13. <el-dialog v-model="uploadDialogVisible" title="上传文件" width="480px" :close-on-click-modal="false" @closed="resetUploadForm">
  14. <el-form label-width="80px">
  15. <el-form-item label="标题">
  16. <el-input v-model="fileTitle" placeholder="请输入文件标题" maxlength="100" />
  17. </el-form-item>
  18. </el-form>
  19. <el-upload
  20. ref="uploadRef"
  21. :action="uploadUrl"
  22. :data="{ fileCate: 'UNIT-' + unitId, fileTitle: fileTitle }"
  23. :show-file-list="false"
  24. :before-upload="beforeUpload"
  25. :on-success="handleUploadSuccess"
  26. :on-error="handleUploadError">
  27. <el-button type="primary" size="small">选择文件</el-button>
  28. </el-upload>
  29. </el-dialog>
  30. <div v-if="fileList.length === 0">
  31. <el-empty description="暂无档案文件" />
  32. </div>
  33. <div v-else class="file-list">
  34. <div v-for="file in fileList" :key="file.fileId" class="file-item" @mouseover="hoverId = file.fileId" @mouseout="hoverId = ''">
  35. <el-icon size="16" color="#409EFF"><Document /></el-icon>
  36. <a :href="getDownloadUrl(file)" target="_blank" class="file-name">{{ file.fileName }}</a>
  37. <span class="file-size">{{ formatSize(file.fileSize) }}</span>
  38. <span class="file-time">{{ file.createTime }}</span>
  39. <el-button v-show="hoverId === file.fileId" type="danger" link size="small" @click.stop="handleDeleteFile(file)">删除</el-button>
  40. </div>
  41. </div>
  42. </el-card>
  43. </div>
  44. </template>
  45. <script setup name="UnitFile">
  46. import { getDocFileList, delDocFile } from '@/api/slgc/doc/index'
  47. const route = useRoute()
  48. const { proxy } = getCurrentInstance()
  49. const uploadRef = ref(null)
  50. const unitId = ref(route.query.id || '')
  51. const fileList = ref([])
  52. const hoverId = ref('')
  53. const uploadUrl = ref('/slgc/doc-file/upload')
  54. const uploadDialogVisible = ref(false)
  55. const fileTitle = ref('')
  56. function loadFiles() {
  57. if (!unitId.value) return
  58. getDocFileList({ fileCate: 'UNIT-' + unitId.value }).then(r => {
  59. fileList.value = r.rows || []
  60. })
  61. }
  62. function getDownloadUrl(file) {
  63. return `/common/download/resource?resource=${encodeURIComponent(file.filePath)}`
  64. }
  65. function formatSize(size) {
  66. if (!size) return ''
  67. const s = Number(size)
  68. if (s < 1024) return s + ' B'
  69. if (s < 1048576) return (s / 1024).toFixed(1) + ' KB'
  70. return (s / 1048576).toFixed(1) + ' MB'
  71. }
  72. function beforeUpload(file) {
  73. const maxSize = 50 * 1024 * 1024
  74. if (file.size > maxSize) {
  75. ElMessage.error('文件大小不能超过 50MB')
  76. return false
  77. }
  78. return true
  79. }
  80. function handleUploadSuccess(res) {
  81. if (res.code === 200) {
  82. ElMessage.success('上传成功')
  83. uploadDialogVisible.value = false
  84. fileTitle.value = ''
  85. loadFiles()
  86. } else {
  87. ElMessage.error(res.msg || '上传失败')
  88. }
  89. }
  90. function resetUploadForm() {
  91. fileTitle.value = ''
  92. }
  93. function handleUploadError() {
  94. ElMessage.error('上传失败')
  95. }
  96. function handleDeleteFile(file) {
  97. proxy.$modal.confirm('确认删除"' + file.fileName + '"?').then(() => {
  98. delDocFile(file.fileId).then(() => {
  99. ElMessage.success('删除成功')
  100. loadFiles()
  101. })
  102. })
  103. }
  104. onMounted(() => { loadFiles() })
  105. </script>
  106. <style scoped>
  107. .card-header { font-weight: 600; font-size: 16px; display: flex; justify-content: space-between; align-items: center; }
  108. .file-list { padding: 8px 0; }
  109. .file-item { display: flex; align-items: center; padding: 8px 12px; border-bottom: 1px solid #f0f0f0; }
  110. .file-item:hover { background: #fafafa; }
  111. .file-name { margin-left: 8px; flex: 1; color: #409EFF; text-decoration: none; font-size: 14px; }
  112. .file-name:hover { text-decoration: underline; }
  113. .file-size { color: #999; font-size: 12px; margin-right: 12px; }
  114. .file-time { color: #999; font-size: 12px; margin-right: 12px; }
  115. </style>