Image.vue 5.6 KB

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