SPuUploadImg.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <template>
  2. <div class="upload-box">
  3. <el-upload :id="uuid" :accept="fileType.join(',')" :action="updateUrl" :before-upload="beforeUpload"
  4. :class="['upload', drag ? 'no-border' : '']" :drag="drag" :headers="uploadHeaders" :multiple="false"
  5. :on-error="uploadError" :on-success="uploadSuccess" :show-file-list="false">
  6. <template v-if="modelValue">
  7. <img :src="modelValue" class="upload-image" />
  8. <div class="upload-handle" @click.stop>
  9. <div class="handle-icon" @click="editImg" v-if="!disabled">
  10. <Icon icon="ep:edit" />
  11. <span v-if="showBtnText">{{ t('action.edit') }}</span>
  12. </div>
  13. <div class="handle-icon" @click="imagePreview(modelValue)">
  14. <Icon icon="ep:zoom-in" />
  15. <span v-if="showBtnText">{{ t('action.detail') }}</span>
  16. </div>
  17. <div v-if="showDelete && !disabled" class="handle-icon" @click="deleteImg">
  18. <Icon icon="ep:delete" />
  19. <span v-if="showBtnText">{{ t('action.del') }}</span>
  20. </div>
  21. </div>
  22. </template>
  23. <template v-else>
  24. <div class="upload-empty">
  25. <slot name="empty">
  26. <Icon icon="ep:plus" />
  27. <!-- <span>请上传图片</span> -->
  28. </slot>
  29. </div>
  30. </template>
  31. </el-upload>
  32. <div class="el-upload__tip">
  33. <slot name="tip"></slot>
  34. </div>
  35. </div>
  36. </template>
  37. <script lang="ts" setup>
  38. import type { UploadProps } from 'element-plus'
  39. import { generateUUID } from '@/utils'
  40. import { propTypes } from '@/utils/propTypes'
  41. import { getAccessToken, getTenantId } from '@/utils/auth'
  42. import { createImageViewer } from '@/components/ImageViewer'
  43. defineOptions({ name: 'UploadImg' })
  44. type FileTypes =
  45. | 'image/apng'
  46. | 'image/bmp'
  47. | 'image/gif'
  48. | 'image/jpeg'
  49. | 'image/pjpeg'
  50. | 'image/png'
  51. | 'image/svg+xml'
  52. | 'image/tiff'
  53. | 'image/webp'
  54. | 'image/x-icon'
  55. // 接受父组件参数
  56. const props = defineProps({
  57. modelValue: propTypes.string.def(''),
  58. updateUrl: propTypes.string.def(import.meta.env.VITE_UPLOAD_URL),
  59. drag: propTypes.bool.def(true), // 是否支持拖拽上传 ==> 非必传(默认为 true)
  60. disabled: propTypes.bool.def(false), // 是否禁用上传组件 ==> 非必传(默认为 false)
  61. fileSize: propTypes.number.def(5), // 图片大小限制 ==> 非必传(默认为 5M)
  62. fileType: propTypes.array.def(['image/jpeg', 'image/png', 'image/gif']), // 图片类型限制 ==> 非必传(默认为 ["image/jpeg", "image/png", "image/gif"])
  63. height: propTypes.string.def('150px'), // 组件高度 ==> 非必传(默认为 150px)
  64. width: propTypes.string.def('150px'), // 组件宽度 ==> 非必传(默认为 150px)
  65. borderradius: propTypes.string.def('8px'), // 组件边框圆角 ==> 非必传(默认为 8px)
  66. // 是否显示删除按钮
  67. showDelete: propTypes.bool.def(true),
  68. // 是否显示按钮文字
  69. showBtnText: propTypes.bool.def(true)
  70. })
  71. const { t } = useI18n() // 国际化
  72. const message = useMessage() // 消息弹窗
  73. // 生成组件唯一id
  74. const uuid = ref('id-' + generateUUID())
  75. // 查看图片
  76. const imagePreview = (imgUrl : string) => {
  77. createImageViewer({
  78. zIndex: 9999999,
  79. urlList: [imgUrl]
  80. })
  81. }
  82. const emit = defineEmits(['update:modelValue'])
  83. const deleteImg = () => {
  84. emit('update:modelValue', '')
  85. }
  86. const uploadHeaders = ref({
  87. Authorization: 'Bearer ' + getAccessToken(),
  88. 'tenant-id': getTenantId()
  89. })
  90. const editImg = () => {
  91. const dom = document.querySelector(`#${uuid.value} .el-upload__input`)
  92. dom && dom.dispatchEvent(new MouseEvent('click'))
  93. }
  94. const beforeUpload : UploadProps['beforeUpload'] = (rawFile) => {
  95. const imgSize = rawFile.size / 1024 / 1024 < props.fileSize
  96. const imgType = props.fileType
  97. if (!imgType.includes(rawFile.type as FileTypes))
  98. message.notifyWarning('上传图片不符合所需的格式!')
  99. if (!imgSize) message.notifyWarning(`上传图片大小不能超过 ${props.fileSize}M!`)
  100. return imgType.includes(rawFile.type as FileTypes) && imgSize
  101. }
  102. // 图片上传成功提示
  103. const uploadSuccess : UploadProps['onSuccess'] = (res : any) : void => {
  104. message.success('上传成功')
  105. emit('update:modelValue', res.data)
  106. }
  107. // 图片上传错误提示
  108. const uploadError = () => {
  109. message.notifyError('图片上传失败,请您重新上传!')
  110. }
  111. </script>
  112. <style lang="scss" scoped>
  113. .is-error {
  114. .upload {
  115. :deep(.el-upload),
  116. :deep(.el-upload-dragger) {
  117. border: 1px dashed var(--el-color-danger) !important;
  118. &:hover {
  119. border-color: var(--el-color-primary) !important;
  120. }
  121. }
  122. }
  123. }
  124. :deep(.disabled) {
  125. .el-upload,
  126. .el-upload-dragger {
  127. cursor: not-allowed !important;
  128. background: var(--el-disabled-bg-color);
  129. border: 1px dashed var(--el-border-color-darker) !important;
  130. &:hover {
  131. border: 1px dashed var(--el-border-color-darker) !important;
  132. }
  133. }
  134. }
  135. .upload-box {
  136. .no-border {
  137. :deep(.el-upload) {
  138. border: none !important;
  139. }
  140. }
  141. :deep(.upload) {
  142. .el-upload {
  143. position: relative;
  144. display: flex;
  145. align-items: center;
  146. justify-content: center;
  147. // width: v-bind(width);
  148. height: 106px;
  149. overflow: hidden;
  150. border: 1px dashed var(--el-border-color-darker);
  151. // border-radius: v-bind(borderradius);
  152. transition: var(--el-transition-duration-fast);
  153. &:hover {
  154. border-color: var(--el-color-primary);
  155. .upload-handle {
  156. opacity: 1;
  157. }
  158. }
  159. .el-upload-dragger {
  160. display: flex;
  161. align-items: center;
  162. justify-content: center;
  163. width: 100%;
  164. height: 100%;
  165. padding: 0;
  166. overflow: hidden;
  167. background-color: transparent;
  168. border: none;
  169. border-radius: 0;
  170. &:hover {
  171. border: 1px dashed var(--el-color-primary);
  172. }
  173. }
  174. .el-upload-dragger.is-dragover {
  175. background-color: var(--el-color-primary-light-9);
  176. border: 2px dashed var(--el-color-primary) !important;
  177. }
  178. .upload-image {
  179. width: 100%;
  180. height: 100%;
  181. object-fit: contain;
  182. }
  183. .upload-empty {
  184. position: relative;
  185. display: flex;
  186. flex-direction: column;
  187. align-items: center;
  188. justify-content: center;
  189. font-size: 12px;
  190. line-height: 30px;
  191. color: var(--el-color-info);
  192. .el-icon {
  193. font-size: 28px;
  194. color: var(--el-text-color-secondary);
  195. }
  196. }
  197. .upload-handle {
  198. position: absolute;
  199. top: 0;
  200. right: 0;
  201. display: flex;
  202. width: 100%;
  203. height: 100%;
  204. cursor: pointer;
  205. background: rgb(0 0 0 / 60%);
  206. opacity: 0;
  207. box-sizing: border-box;
  208. transition: var(--el-transition-duration-fast);
  209. align-items: center;
  210. justify-content: center;
  211. .handle-icon {
  212. display: flex;
  213. flex-direction: column;
  214. align-items: center;
  215. justify-content: center;
  216. padding: 0 6%;
  217. color: aliceblue;
  218. .el-icon {
  219. margin-bottom: 40%;
  220. font-size: 130%;
  221. line-height: 130%;
  222. }
  223. span {
  224. font-size: 85%;
  225. line-height: 85%;
  226. }
  227. }
  228. }
  229. }
  230. }
  231. .el-upload__tip {
  232. line-height: 18px;
  233. text-align: center;
  234. }
  235. }
  236. </style>