index.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <template>
  2. <div class="flex flex-wrap">
  3. <template v-if="limit == 1">
  4. <div class="rounded cursor-pointer overflow-hidden relative border border-solid border-color image-wrap mr-[10px]" :style="style">
  5. <div class="w-full h-full relative" v-if="images.data.length">
  6. <div class="w-full h-full flex items-center justify-center">
  7. <el-image :src="img(images.data[0])" fit="contain"></el-image>
  8. </div>
  9. <div class="absolute z-[1] flex items-center justify-center w-full h-full inset-0 bg-black bg-opacity-60 operation">
  10. <icon name="element-ZoomIn" color="#fff" size="18px" class="mr-[10px]" @click="previewImage()" />
  11. <icon name="element-Delete" color="#fff" size="18px" v-if="status" @click="removeImage" />
  12. </div>
  13. </div>
  14. <el-upload v-bind="upload" class="upload-file w-full h-full" :show-file-list="false" >
  15. <div class="w-full h-full flex items-center justify-center flex-col">
  16. <icon name="element-Plus" size="20px" color="var(--el-text-color-secondary)" />
  17. <div class="leading-none text-xs mt-[10px] text-secondary">{{ imageText }}
  18. </div>
  19. </div>
  20. </el-upload>
  21. </div>
  22. </template>
  23. <template v-else>
  24. <div class="rounded cursor-pointer overflow-hidden relative border border-solid border-color image-wrap mr-[10px] mb-[10px]" :style="style" v-for="(item, index) in images.data" :key="index">
  25. <div class="w-full h-full relative">
  26. <div class="w-full h-full flex items-center justify-center">
  27. <el-image :src="img(item)" fit="contain"></el-image>
  28. </div>
  29. <div class="absolute z-[1] flex items-center justify-center w-full h-full inset-0 bg-black bg-opacity-60 operation">
  30. <icon name="element-ZoomIn" color="#fff" size="18px" class="mr-[10px]" @click="previewImage(index)" />
  31. <icon name="element-Delete" color="#fff" size="18px" v-if="status" @click="removeImage(index)" />
  32. </div>
  33. </div>
  34. </div>
  35. <div class="rounded cursor-pointer overflow-hidden relative border border-solid border-color" :style="style" v-if="images.data.length < limit && status">
  36. <el-upload v-bind="upload" class="upload-file w-full h-full" :show-file-list="false" :multiple="true" :limit="limit">
  37. <div class="w-full h-full flex items-center justify-center flex-col">
  38. <icon name="element-Plus" size="20px" color="var(--el-text-color-secondary)" />
  39. <div class="leading-none text-xs mt-[10px] text-secondary">{{ imageText }}
  40. </div>
  41. </div>
  42. </el-upload>
  43. </div>
  44. </template>
  45. </div>
  46. <el-image-viewer :url-list="previewImageList" v-if="imageViewer.show" @close="imageViewer.show = false"
  47. :initial-index="imageViewer.index" :zoom-rate="1" :hide-on-click-modal="true" />
  48. </template>
  49. <script lang="ts" setup>
  50. import { reactive,computed,watch,toRaw } from 'vue'
  51. import { getToken,img } from '@/utils/common'
  52. import { UploadFile, ElMessage, UploadFiles } from 'element-plus'
  53. const prop = defineProps({
  54. modelValue: {
  55. type: String,
  56. default: ''
  57. },
  58. data: {
  59. type: Array,
  60. default: []
  61. },
  62. width: {
  63. type: String,
  64. default: '100px'
  65. },
  66. height: {
  67. type: String,
  68. default: '100px'
  69. },
  70. // 上传图片的文字
  71. imageText: {
  72. type: String
  73. },
  74. // 限制图片的数量
  75. limit: {
  76. type: Number,
  77. default: 1
  78. },
  79. // 控制删除按钮的展示
  80. status:{
  81. type:Boolean,
  82. default:true
  83. }
  84. })
  85. const emit = defineEmits(['update:modelValue','success'])
  86. const value = computed({
  87. get() {
  88. return prop.modelValue
  89. },
  90. set(value) {
  91. emit('update:modelValue', value)
  92. }
  93. })
  94. const images: Record<string, any> = reactive({
  95. data: []
  96. })
  97. let previewImageList: string[] = reactive([])
  98. const setValue = () => {
  99. value.value = toRaw(images.data).toString()
  100. previewImageList = toRaw(images.data).map((url: string) => { return img(url) })
  101. }
  102. watch(() => value.value, () => {
  103. images.data = [
  104. ...value.value.split(',').filter((item: string) => { return item })
  105. ]
  106. setValue()
  107. }, { immediate: true })
  108. const style = computed(() => {
  109. return {
  110. width: prop.width,
  111. height: prop.height
  112. }
  113. })
  114. const headers: Record<string, any> = {}
  115. headers.token = getToken()
  116. headers['site-id'] = useCookie('siteId').value || useRuntimeConfig().public.VITE_SITE_ID
  117. const runtimeConfig = useRuntimeConfig()
  118. let url = runtimeConfig.public.VITE_APP_BASE_URL || `${location.origin}/api/`
  119. const upload: Record<string, any> = {
  120. action: `${url}/file/image`,
  121. headers,
  122. accept: '.png,.jpg,.jpeg',
  123. onSuccess: (response: any, uploadFile: UploadFile, uploadFiles: UploadFiles) => {
  124. if(images.data.length < prop.limit){
  125. images.data.push(response.data.url)
  126. }
  127. setValue()
  128. },
  129. onExceed: (response: any, uploadFile: UploadFile, uploadFiles: UploadFiles) => {
  130. if(response.length > prop.limit){
  131. ElMessage.error(`最多只能上传${prop.limit}张图片`)
  132. }
  133. }
  134. }
  135. /**
  136. * 删除图片
  137. * @param index
  138. */
  139. const removeImage = (index: number = 0) => {
  140. images.data.splice(index, 1)
  141. setValue()
  142. }
  143. /**
  144. * 查看图片
  145. */
  146. const imageViewer = reactive({
  147. show: false,
  148. index: 0
  149. })
  150. const previewImage = (index: number = 0) => {
  151. imageViewer.show = true
  152. imageViewer.index = index
  153. }
  154. </script>
  155. <style lang="scss">
  156. .upload-file .el-upload {
  157. width: 100%;
  158. height: 100%;
  159. }
  160. </style>