Image.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <template>
  2. <div class="media-image" v-show="images.length">
  3. <div class="swiper" ref="swiper$">
  4. <div class="swiper-wrapper">
  5. <div class="swiper-slide" v-for="(item, index) in images" :style="`background-image: url(${item.src})`" :key="index"></div>
  6. </div>
  7. <div class="swiper-button-prev"></div>
  8. <div class="swiper-button-next"></div>
  9. </div>
  10. <div v-if="isEdit" class="add" @click="file.click()" :class="{ disable: images.length >= 9 }">
  11. 继续添加&nbsp;<span>{{ images.length }}</span
  12. >&nbsp;/&nbsp;9
  13. </div>
  14. </div>
  15. <div v-if="isEdit" class="placeholder" @click="file.click()" v-show="images.length == 0">
  16. <div class="icon">
  17. <i class="iconfont icon-add"></i>
  18. <span>上传图片</span>
  19. </div>
  20. <div class="tips">支持JPG、PNG等图片格式,单张不超过5MB,最多支持上传9张。</div>
  21. <input ref="file" type="file" style="display: none" accept="image/jpg,image/jpeg,image/png" @change="onChange" />
  22. </div>
  23. <div class="del-btn" v-if="isEdit" v-show="notify.media?.[notify.type]?.length" @click="delPic">
  24. <i class="iconfont icon-del"></i>
  25. </div>
  26. </template>
  27. <script setup>
  28. import { ref, onMounted, inject } from 'vue'
  29. import { checkSizeLimitFree, base64ToDataURL, convertBlob2File, base64ToBlob } from '@/utils/file'
  30. import common from '@/utils/common'
  31. const notify = inject('notify')
  32. const isEdit = inject('isEdit')
  33. const emits = defineEmits(['tips'])
  34. const props = defineProps({
  35. viewer: {
  36. type: Boolean,
  37. default: false,
  38. },
  39. })
  40. const file = ref(null)
  41. const swiper$ = ref(null)
  42. const images = ref([])
  43. const onChange = e => {
  44. if (!e.target.files.length) {
  45. return
  46. }
  47. let file = e.target.files[0]
  48. if (checkSizeLimitFree(file.size, 5)) {
  49. let reader = new FileReader()
  50. reader.onload = function () {
  51. images.value.push({ src: base64ToDataURL(reader.result), file })
  52. notify.value.media['image'] = images.value
  53. }
  54. reader.readAsDataURL(file)
  55. } else {
  56. emits('tips', '请上传 5MB 以内的 jpg/png 文件')
  57. }
  58. e.target.value = ''
  59. }
  60. const delPic = () => {
  61. let index = swiper.activeIndex
  62. images.value.splice(index, 1)
  63. }
  64. let swiper
  65. onMounted(() => {
  66. swiper = new Swiper(swiper$.value, {
  67. observer: true,
  68. navigation: {
  69. prevEl: swiper$.value.querySelector('.swiper-button-prev'),
  70. nextEl: swiper$.value.querySelector('.swiper-button-next'),
  71. },
  72. on: {
  73. observerUpdate: function () {
  74. swiper.slideTo(images.value.length - 1, 0, false)
  75. },
  76. },
  77. })
  78. images.value = notify.value.media?.image || []
  79. // if (!notify.value.media) {
  80. // notify.value.media = {}
  81. // }
  82. // notify.value.media['image'] = images.value
  83. })
  84. </script>
  85. <style lang="scss" scoped>
  86. .del-btn {
  87. width: 24px;
  88. height: 24px;
  89. background: rgba(0, 0, 0, 0.6);
  90. border-radius: 50%;
  91. position: absolute;
  92. cursor: pointer;
  93. top: 10px;
  94. right: 10px;
  95. z-index: 10;
  96. display: flex;
  97. align-items: center;
  98. justify-content: center;
  99. .iconfont {
  100. font-size: 1em;
  101. }
  102. }
  103. .placeholder {
  104. cursor: pointer;
  105. width: 100%;
  106. height: 100%;
  107. display: flex;
  108. align-items: center;
  109. justify-content: center;
  110. .icon {
  111. display: flex;
  112. align-items: center;
  113. justify-content: center;
  114. flex-direction: column;
  115. color: rgba(255, 255, 255, 0.6);
  116. font-size: 14px;
  117. span {
  118. margin-top: 10px;
  119. }
  120. }
  121. .tips {
  122. font-size: 12px;
  123. padding: 10px;
  124. position: absolute;
  125. bottom: 0;
  126. width: 100%;
  127. color: rgba(255, 255, 255, 0.3);
  128. }
  129. }
  130. .media-image {
  131. width: 100%;
  132. height: 100%;
  133. .swiper {
  134. width: 100%;
  135. height: 100%;
  136. .swiper-slide {
  137. text-align: center;
  138. display: flex;
  139. justify-content: center;
  140. align-items: center;
  141. width: 100%;
  142. height: 100%;
  143. background-position: center center;
  144. background-size: contain;
  145. }
  146. .swiper-button-prev,
  147. .swiper-button-next {
  148. width: 32px;
  149. height: 32px;
  150. background: rgba(0, 0, 0, 0.2);
  151. border-radius: 50%;
  152. &::after {
  153. font-weight: bold;
  154. font-size: 14px;
  155. }
  156. }
  157. }
  158. .add {
  159. cursor: pointer;
  160. font-size: 12px;
  161. position: absolute;
  162. bottom: 0;
  163. left: 0;
  164. width: 100%;
  165. height: 32px;
  166. line-height: 32px;
  167. text-align: center;
  168. background: linear-gradient(180deg, rgba(0, 0, 0, 0.1), #000 200%);
  169. border-radius: 0 0 4px 4px;
  170. z-index: 10;
  171. &.disable {
  172. pointer-events: none;
  173. }
  174. span {
  175. color: #0076f6;
  176. }
  177. }
  178. }
  179. </style>