UploadImgs.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <template>
  2. <div class="upload-box">
  3. <el-upload
  4. :action="updateUrl"
  5. list-type="picture-card"
  6. :class="['upload', drag ? 'no-border' : '']"
  7. v-model:file-list="fileList"
  8. :multiple="true"
  9. :limit="limit"
  10. :headers="uploadHeaders"
  11. :before-upload="beforeUpload"
  12. :on-exceed="handleExceed"
  13. :on-success="uploadSuccess"
  14. :on-error="uploadError"
  15. :drag="drag"
  16. :accept="fileType.join(',')"
  17. >
  18. <div class="upload-empty">
  19. <slot name="empty">
  20. <Icon icon="ep:plus" />
  21. <!-- <span>请上传图片</span> -->
  22. </slot>
  23. </div>
  24. <template #file="{ file }">
  25. <img :src="file.url" class="upload-image" />
  26. <div class="upload-handle" @click.stop>
  27. <div class="handle-icon" @click="handlePictureCardPreview(file)">
  28. <Icon icon="ep:zoom-in" />
  29. <span>查看</span>
  30. </div>
  31. <div class="handle-icon" @click="handleRemove(file)">
  32. <Icon icon="ep:delete" />
  33. <span>删除</span>
  34. </div>
  35. </div>
  36. </template>
  37. </el-upload>
  38. <div class="el-upload__tip">
  39. <slot name="tip"></slot>
  40. </div>
  41. <el-image-viewer
  42. v-if="imgViewVisible"
  43. @close="imgViewVisible = false"
  44. :url-list="[viewImageUrl]"
  45. />
  46. </div>
  47. </template>
  48. <script setup lang="ts" name="UploadImgs">
  49. import { PropType } from 'vue'
  50. import { ElNotification } from 'element-plus'
  51. import type { UploadProps, UploadFile, UploadUserFile } from 'element-plus'
  52. import { propTypes } from '@/utils/propTypes'
  53. import { getAccessToken, getTenantId } from '@/utils/auth'
  54. const message = useMessage() // 消息弹窗
  55. type FileTypes =
  56. | 'image/apng'
  57. | 'image/bmp'
  58. | 'image/gif'
  59. | 'image/jpeg'
  60. | 'image/pjpeg'
  61. | 'image/png'
  62. | 'image/svg+xml'
  63. | 'image/tiff'
  64. | 'image/webp'
  65. | 'image/x-icon'
  66. const props = defineProps({
  67. modelValue: {
  68. type: Array as PropType<UploadUserFile[]>,
  69. required: true
  70. },
  71. updateUrl: propTypes.string.def(import.meta.env.VITE_UPLOAD_URL),
  72. drag: propTypes.bool.def(true), // 是否支持拖拽上传 ==> 非必传(默认为 true)
  73. disabled: propTypes.bool.def(false), // 是否禁用上传组件 ==> 非必传(默认为 false)
  74. limit: propTypes.number.def(5), // 最大图片上传数 ==> 非必传(默认为 5张)
  75. fileSize: propTypes.number.def(5), // 图片大小限制 ==> 非必传(默认为 5M)
  76. fileType: propTypes.array.def(['image/jpeg', 'image/png', 'image/gif']), // 图片类型限制 ==> 非必传(默认为 ["image/jpeg", "image/png", "image/gif"])
  77. height: propTypes.string.def('150px'), // 组件高度 ==> 非必传(默认为 150px)
  78. width: propTypes.string.def('150px'), // 组件宽度 ==> 非必传(默认为 150px)
  79. borderRadius: propTypes.string.def('8px') // 组件边框圆角 ==> 非必传(默认为 8px)
  80. })
  81. const uploadHeaders = ref({
  82. Authorization: 'Bearer ' + getAccessToken(),
  83. 'tenant-id': getTenantId()
  84. })
  85. const fileList = ref<UploadUserFile[]>(props.modelValue)
  86. /**
  87. * @description 文件上传之前判断
  88. * @param rawFile 上传的文件
  89. * */
  90. const beforeUpload: UploadProps['beforeUpload'] = (rawFile) => {
  91. const imgSize = rawFile.size / 1024 / 1024 < props.fileSize
  92. const imgType = props.fileType
  93. if (!imgType.includes(rawFile.type as FileTypes))
  94. ElNotification({
  95. title: '温馨提示',
  96. message: '上传图片不符合所需的格式!',
  97. type: 'warning'
  98. })
  99. if (!imgSize)
  100. ElNotification({
  101. title: '温馨提示',
  102. message: `上传图片大小不能超过 ${props.fileSize}M!`,
  103. type: 'warning'
  104. })
  105. return imgType.includes(rawFile.type as FileTypes) && imgSize
  106. }
  107. // 图片上传成功
  108. interface UploadEmits {
  109. (e: 'update:modelValue', value: UploadUserFile[]): void
  110. }
  111. const emit = defineEmits<UploadEmits>()
  112. const uploadSuccess = (response, uploadFile: UploadFile) => {
  113. if (!response) return
  114. uploadFile.url = response.data
  115. emit('update:modelValue', fileList.value)
  116. message.success('上传成功')
  117. }
  118. // 删除图片
  119. const handleRemove = (uploadFile: UploadFile) => {
  120. fileList.value = fileList.value.filter(
  121. (item) => item.url !== uploadFile.url || item.name !== uploadFile.name
  122. )
  123. emit('update:modelValue', fileList.value)
  124. }
  125. // 图片上传错误提示
  126. const uploadError = () => {
  127. ElNotification({
  128. title: '温馨提示',
  129. message: '图片上传失败,请您重新上传!',
  130. type: 'error'
  131. })
  132. }
  133. // 文件数超出提示
  134. const handleExceed = () => {
  135. ElNotification({
  136. title: '温馨提示',
  137. message: `当前最多只能上传 ${props.limit} 张图片,请移除后上传!`,
  138. type: 'warning'
  139. })
  140. }
  141. // 图片预览
  142. const viewImageUrl = ref('')
  143. const imgViewVisible = ref(false)
  144. const handlePictureCardPreview: UploadProps['onPreview'] = (uploadFile) => {
  145. viewImageUrl.value = uploadFile.url!
  146. imgViewVisible.value = true
  147. }
  148. </script>
  149. <style scoped lang="scss">
  150. .is-error {
  151. .upload {
  152. :deep(.el-upload--picture-card),
  153. :deep(.el-upload-dragger) {
  154. border: 1px dashed var(--el-color-danger) !important;
  155. &:hover {
  156. border-color: var(--el-color-primary) !important;
  157. }
  158. }
  159. }
  160. }
  161. :deep(.disabled) {
  162. .el-upload--picture-card,
  163. .el-upload-dragger {
  164. cursor: not-allowed;
  165. background: var(--el-disabled-bg-color) !important;
  166. border: 1px dashed var(--el-border-color-darker);
  167. &:hover {
  168. border-color: var(--el-border-color-darker) !important;
  169. }
  170. }
  171. }
  172. .upload-box {
  173. .no-border {
  174. :deep(.el-upload--picture-card) {
  175. border: none !important;
  176. }
  177. }
  178. :deep(.upload) {
  179. .el-upload-dragger {
  180. display: flex;
  181. align-items: center;
  182. justify-content: center;
  183. width: 100%;
  184. height: 100%;
  185. padding: 0;
  186. overflow: hidden;
  187. border: 1px dashed var(--el-border-color-darker);
  188. border-radius: v-bind(borderRadius);
  189. &:hover {
  190. border: 1px dashed var(--el-color-primary);
  191. }
  192. }
  193. .el-upload-dragger.is-dragover {
  194. background-color: var(--el-color-primary-light-9);
  195. border: 2px dashed var(--el-color-primary) !important;
  196. }
  197. .el-upload-list__item,
  198. .el-upload--picture-card {
  199. width: v-bind(width);
  200. height: v-bind(height);
  201. background-color: transparent;
  202. border-radius: v-bind(borderRadius);
  203. }
  204. .upload-image {
  205. width: 100%;
  206. height: 100%;
  207. object-fit: contain;
  208. }
  209. .upload-handle {
  210. position: absolute;
  211. top: 0;
  212. right: 0;
  213. box-sizing: border-box;
  214. display: flex;
  215. align-items: center;
  216. justify-content: center;
  217. width: 100%;
  218. height: 100%;
  219. cursor: pointer;
  220. background: rgb(0 0 0 / 60%);
  221. opacity: 0;
  222. transition: var(--el-transition-duration-fast);
  223. .handle-icon {
  224. display: flex;
  225. flex-direction: column;
  226. align-items: center;
  227. justify-content: center;
  228. padding: 0 6%;
  229. color: aliceblue;
  230. .el-icon {
  231. margin-bottom: 15%;
  232. font-size: 140%;
  233. }
  234. span {
  235. font-size: 100%;
  236. }
  237. }
  238. }
  239. .el-upload-list__item {
  240. &:hover {
  241. .upload-handle {
  242. opacity: 1;
  243. }
  244. }
  245. }
  246. .upload-empty {
  247. display: flex;
  248. flex-direction: column;
  249. align-items: center;
  250. font-size: 12px;
  251. line-height: 30px;
  252. color: var(--el-color-info);
  253. .el-icon {
  254. font-size: 28px;
  255. color: var(--el-text-color-secondary);
  256. }
  257. }
  258. }
  259. .el-upload__tip {
  260. line-height: 15px;
  261. text-align: center;
  262. }
  263. }
  264. </style>