index.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <template>
  2. <div class="attachment-upload">
  3. <el-upload
  4. :action="uploadUrl"
  5. :headers="headers"
  6. :data="uploadData"
  7. :show-file-list="false"
  8. :on-success="handleUploadSuccess"
  9. :on-error="handleUploadError"
  10. :before-upload="beforeUpload"
  11. :accept="accept"
  12. :multiple="multiple"
  13. :disabled="disabled"
  14. :loading="uploading"
  15. >
  16. <el-button :type="buttonType" :size="buttonSize" :icon="buttonIcon" :disabled="disabled">
  17. {{ buttonText }}
  18. </el-button>
  19. </el-upload>
  20. <div v-if="fileList.length > 0" class="file-list">
  21. <div v-for="(file, index) in fileList" :key="index" class="file-item">
  22. <div class="file-preview">
  23. <img v-if="isImageFile(file.url)" :src="file.url" class="file-image" />
  24. <div v-else class="file-icon">
  25. <i class="el-icon-document"></i>
  26. </div>
  27. </div>
  28. <div class="file-info">
  29. <div class="file-name">{{ file.name }}</div>
  30. <div class="file-size">{{ formatFileSize(file.size) }}</div>
  31. </div>
  32. <div class="file-actions">
  33. <el-button type="text" size="mini" @click="previewFile(file)">预览</el-button>
  34. <el-button type="text" size="mini" @click="downloadFile(file)">下载</el-button>
  35. <el-button type="text" size="mini" @click="removeFile(index)" style="color: #f56c6c;">删除</el-button>
  36. </div>
  37. </div>
  38. </div>
  39. </div>
  40. </template>
  41. <script>
  42. export default {
  43. name: 'AttachmentUpload',
  44. props: {
  45. // 上传地址
  46. uploadUrl: {
  47. type: String,
  48. default: '/common/upload'
  49. },
  50. // 请求头
  51. headers: {
  52. type: Object,
  53. default: () => ({})
  54. },
  55. // 上传时附带的额外参数
  56. uploadData: {
  57. type: Object,
  58. default: () => ({})
  59. },
  60. // 接受上传的文件类型
  61. accept: {
  62. type: String,
  63. default: '*'
  64. },
  65. // 是否支持多选文件
  66. multiple: {
  67. type: Boolean,
  68. default: true
  69. },
  70. // 是否禁用
  71. disabled: {
  72. type: Boolean,
  73. default: false
  74. },
  75. // 按钮类型
  76. buttonType: {
  77. type: String,
  78. default: 'primary'
  79. },
  80. // 按钮大小
  81. buttonSize: {
  82. type: String,
  83. default: 'mini'
  84. },
  85. // 按钮图标
  86. buttonIcon: {
  87. type: String,
  88. default: 'el-icon-upload'
  89. },
  90. // 按钮文字
  91. buttonText: {
  92. type: String,
  93. default: '上传文件'
  94. },
  95. // 文件列表
  96. value: {
  97. type: Array,
  98. default: () => []
  99. }
  100. },
  101. data() {
  102. return {
  103. uploading: false,
  104. fileList: []
  105. }
  106. },
  107. watch: {
  108. value: {
  109. handler(newVal) {
  110. this.fileList = [...newVal];
  111. },
  112. immediate: true
  113. }
  114. },
  115. methods: {
  116. // 上传前校验
  117. beforeUpload(file) {
  118. // 文件大小限制 (10MB)
  119. const isLt10M = file.size / 1024 / 1024 < 10;
  120. if (!isLt10M) {
  121. this.$message.error('上传文件大小不能超过 10MB!');
  122. return false;
  123. }
  124. this.uploading = true;
  125. return true;
  126. },
  127. // 上传成功
  128. handleUploadSuccess(response, file) {
  129. this.uploading = false;
  130. if (response.code === 200) {
  131. const fileInfo = {
  132. name: file.name,
  133. url: response.data.url,
  134. size: file.size,
  135. type: file.type
  136. };
  137. this.fileList.push(fileInfo);
  138. this.$emit('input', this.fileList);
  139. this.$emit('success', fileInfo);
  140. this.$message.success('上传成功');
  141. } else {
  142. this.$message.error(response.msg || '上传失败');
  143. }
  144. },
  145. // 上传失败
  146. handleUploadError(err, file) {
  147. this.uploading = false;
  148. this.$message.error('上传失败');
  149. this.$emit('error', err, file);
  150. },
  151. // 预览文件
  152. previewFile(file) {
  153. if (this.isImageFile(file.url)) {
  154. // 图片预览
  155. this.$alert(`<img src="${file.url}" style="max-width: 100%;" />`, '文件预览', {
  156. dangerouslyUseHTMLString: true,
  157. showConfirmButton: false,
  158. customClass: 'preview-dialog'
  159. });
  160. } else {
  161. // 其他文件类型,提供下载链接
  162. window.open(file.url, '_blank');
  163. }
  164. },
  165. // 下载文件
  166. downloadFile(file) {
  167. const link = document.createElement('a');
  168. link.href = file.url;
  169. link.download = file.name;
  170. document.body.appendChild(link);
  171. link.click();
  172. document.body.removeChild(link);
  173. },
  174. // 删除文件
  175. removeFile(index) {
  176. this.fileList.splice(index, 1);
  177. this.$emit('input', this.fileList);
  178. this.$emit('remove', index);
  179. },
  180. // 判断是否为图片文件
  181. isImageFile(url) {
  182. if (!url) return false;
  183. const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp'];
  184. return imageExtensions.some(ext => url.toLowerCase().includes(ext));
  185. },
  186. // 格式化文件大小
  187. formatFileSize(bytes) {
  188. if (bytes === 0) return '0 B';
  189. const k = 1024;
  190. const sizes = ['B', 'KB', 'MB', 'GB'];
  191. const i = Math.floor(Math.log(bytes) / Math.log(k));
  192. return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
  193. }
  194. }
  195. }
  196. </script>
  197. <style scoped>
  198. .attachment-upload {
  199. width: 100%;
  200. }
  201. .file-list {
  202. margin-top: 15px;
  203. }
  204. .file-item {
  205. display: flex;
  206. align-items: center;
  207. margin-bottom: 10px;
  208. padding: 10px;
  209. border: 1px solid #e4e7ed;
  210. border-radius: 4px;
  211. background-color: #fafafa;
  212. }
  213. .file-preview {
  214. margin-right: 15px;
  215. }
  216. .file-image {
  217. width: 60px;
  218. height: 60px;
  219. object-fit: cover;
  220. border-radius: 4px;
  221. }
  222. .file-icon {
  223. width: 60px;
  224. height: 60px;
  225. display: flex;
  226. align-items: center;
  227. justify-content: center;
  228. background-color: #f5f7fa;
  229. border-radius: 4px;
  230. color: #909399;
  231. }
  232. .file-icon i {
  233. font-size: 24px;
  234. }
  235. .file-info {
  236. flex: 1;
  237. margin-right: 15px;
  238. }
  239. .file-name {
  240. font-size: 14px;
  241. color: #303133;
  242. margin-bottom: 5px;
  243. word-break: break-all;
  244. }
  245. .file-size {
  246. font-size: 12px;
  247. color: #909399;
  248. }
  249. .file-actions {
  250. display: flex;
  251. gap: 5px;
  252. }
  253. /* 预览对话框样式 */
  254. :deep(.preview-dialog) {
  255. width: 80%;
  256. max-width: 800px;
  257. }
  258. :deep(.preview-dialog .el-message-box__content) {
  259. text-align: center;
  260. }
  261. </style>