WXL
2025-12-28 40bd04c1299a0edf63771b90b5f9e78bfb943474
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
<template>
  <el-dialog
    :title="title"
    :visible.sync="visible"
    width="90%"
    top="5vh"
    :close-on-click-modal="true"
    class="attachment-preview-dialog"
    @close="handleClose"
  >
    <div class="attachment-preview">
      <el-tabs v-model="activeTab" type="card">
        <el-tab-pane
          v-for="(file, index) in fileList"
          :key="index"
          :label="getTabLabel(file)"
          :name="index.toString()"
        >
          <div class="preview-content">
            <!-- PDF预览 -->
            <PdfPreview
              v-if="getFileType(file.fileName) === 'pdf'"
              :pdf-url="file.fileUrl"
              :file-name="file.fileName"
            />
 
            <!-- 图片预览 -->
            <ImagePreview
              v-else-if="getFileType(file.fileName) === 'image'"
              :image-url="file.fileUrl"
              :file-name="file.fileName"
            />
 
            <!-- 不支持预览的文件类型 -->
            <div v-else class="unsupported-preview">
              <el-alert
                title="该文件格式不支持在线预览,请下载后查看"
                type="warning"
                show-icon
                :closable="false"
              />
              <div class="download-action">
                <el-button type="primary" @click="handleDownload(file)">
                  <i class="el-icon-download"></i> 下载文件
                </el-button>
              </div>
            </div>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
 
    <span slot="footer" class="dialog-footer">
      <el-button @click="$emit('close')">关闭</el-button>
    </span>
  </el-dialog>
</template>
 
<script>
import PdfPreview from "./PdfPreview";
import ImagePreview from "./ImagePreview";
 
export default {
  name: "AttachmentPreview",
  components: { PdfPreview, ImagePreview },
  props: {
    visible: Boolean,
    fileList: {
      type: Array,
      default: () => []
    },
    title: String
  },
  data() {
    return {
      activeTab: "0"
    };
  },
  methods: {
    getFileType(fileName) {
      const extension = fileName.split('.').pop().toLowerCase();
      const imageTypes = ["jpg", "jpeg", "png", "gif", "bmp", "webp"];
      const pdfTypes = ["pdf"];
 
      if (imageTypes.includes(extension)) return "image";
      if (pdfTypes.includes(extension)) return "pdf";
      return "other";
    },
 
    getTabLabel(file) {
      const type = this.getFileType(file.fileName);
      const icon = type === 'pdf' ? 'el-icon-document' :
                  type === 'image' ? 'el-icon-picture' : 'el-icon-files';
      return `${file.fileName}`;
    },
 
    handleDownload(file) {
      const link = document.createElement("a");
      link.href = file.fileUrl;
      link.download = file.fileName;
      link.style.display = "none";
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
      this.$message.success("开始下载文件");
    },
 
    handleClose() {
      this.activeTab = "0";
      this.$emit('close');
    }
  }
};
</script>
 
<style scoped>
.attachment-preview-dialog >>> .el-dialog {
  min-height: 80vh;
  display: flex;
  flex-direction: column;
}
 
.attachment-preview-dialog >>> .el-dialog__body {
  flex: 1;
  padding: 0;
  display: flex;
  flex-direction: column;
}
 
.attachment-preview {
  flex: 1;
  display: flex;
  flex-direction: column;
}
 
.preview-content {
  flex: 1;
  height: 800px;
  padding: 0;
}
 
.unsupported-preview {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 40px;
}
 
.download-action {
  margin-top: 20px;
}
</style>