hsy
7 小时以前 2d3530a7c11557a40807ad7c0b9e302ce6ce3a9f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<template>
  <el-dialog
    title="文档归档"
    v-model="visible"
    width="650px"
    destroy-on-close
    append-to-body
  >
    <div class="archive-container" v-loading="loading">
      <el-empty v-if="!loading && (!archiveGroups || archiveGroups.length === 0)" description="暂无归档附件" />
      
      <el-collapse v-else v-model="activeNames">
        <el-collapse-item 
          v-for="(group, index) in archiveGroups" 
          :key="index" 
          :title="`${group.moduleName} (${group.files ? group.files.length : 0})`" 
          :name="index"
        >
          <div class="attachment-list">
            <div 
              v-for="(file, fileIdx) in group.files" 
              :key="fileIdx" 
              class="attachment-item"
            >
              <el-icon class="file-icon"><Document /></el-icon>
              <a 
                :href="getRealUrl(file.url)" 
                target="_blank" 
                class="file-link"
                :title="file.name"
              >
                {{ file.name }}
              </a>
              <el-button link type="primary" class="download-btn" @click="handleDownload(file.url, file.name)">下载</el-button>
            </div>
          </div>
        </el-collapse-item>
      </el-collapse>
    </div>
    
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="visible = false">关闭</el-button>
      </div>
    </template>
  </el-dialog>
</template>
 
<script setup>
import { ref, watch } from 'vue'
import { getProjectAttachments } from '@/api/projectManagement/project'
import { Document } from '@element-plus/icons-vue'
 
const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },
  projectId: {
    type: [Number, String],
    default: null
  }
})
 
const emit = defineEmits(['update:modelValue'])
 
const visible = ref(false)
const loading = ref(false)
const archiveGroups = ref([])
const activeNames = ref([])
 
watch(() => props.modelValue, (val) => {
  visible.value = val
  if (val && props.projectId) {
    fetchAttachments()
  } else {
    archiveGroups.value = []
    activeNames.value = []
  }
})
 
watch(() => visible.value, (val) => {
  emit('update:modelValue', val)
})
 
const fetchAttachments = async () => {
  loading.value = true
  try {
    const res = await getProjectAttachments(props.projectId)
    if (res.code === 200) {
      archiveGroups.value = res.data || []
      // 默认展开所有面板
      activeNames.value = archiveGroups.value.map((_, index) => index)
    }
  } catch (error) {
    console.error('获取附件失败', error)
  } finally {
    loading.value = false
  }
}
 
// 获取真实地址(处理可能的相对路径前缀)
const getRealUrl = (url) => {
  if (!url) return '#'
  if (url.startsWith('http://') || url.startsWith('https://')) {
    return url
  }
  return import.meta.env.VITE_APP_BASE_API + url
}
 
// 提取文件名(兼容旧逻辑)
const getFileName = (url) => {
  if (!url) return '未知文件'
  const parts = url.split('/')
  const name = parts[parts.length - 1]
  // 处理可能的时间戳前缀
  const nameParts = name.split('_')
  return nameParts.length > 1 ? nameParts.slice(1).join('_') : name
}
 
const handleDownload = (url, name) => {
  const realUrl = getRealUrl(url)
  const a = document.createElement('a')
  a.style.display = 'none'
  a.href = realUrl
  a.download = name || getFileName(url)
  document.body.appendChild(a)
  a.click()
  document.body.removeChild(a)
}
</script>
 
<style scoped>
.archive-container {
  min-height: 200px;
}
.attachment-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 5px 0;
}
.attachment-item {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  background-color: #f8f9fa;
  border-radius: 4px;
  transition: background-color 0.3s;
}
.attachment-item:hover {
  background-color: #f0f2f5;
}
.file-icon {
  font-size: 16px;
  color: #909399;
  margin-right: 8px;
}
.file-link {
  flex: 1;
  color: #606266;
  text-decoration: none;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-right: 15px;
}
.file-link:hover {
  color: #409EFF;
  text-decoration: underline;
}
.download-btn {
  margin-left: auto;
}
</style>