Image.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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=".jpg,.png,.jpeg" @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, getMime, 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 (!['png', 'jpg', 'jpeg'].includes(getMime(file.name))) {
  50. emits('tips', t('components.FileSizeTips', { size: '5', file: 'jpg/png' }))
  51. continue;
  52. }
  53. if (checkSizeLimitFree(file.size, 5)) {
  54. let reader = new FileReader()
  55. reader.onload = function () {
  56. if (images.value.length >= 9) {
  57. if (!frist) {
  58. frist = true
  59. emits('tips', t('components.TipsImgLength'))
  60. }
  61. } else {
  62. images.value.push({ src: base64ToDataURL(reader.result), file })
  63. notify.value.media['image'] = images.value
  64. }
  65. }
  66. reader.readAsDataURL(file)
  67. } else {
  68. emits('tips', t('components.FileSizeTips', { size: '5', file: 'jpg/png' }))
  69. }
  70. console.log(images.value.length)
  71. }
  72. e.target.value = ''
  73. }
  74. const delPic = () => {
  75. let index = swiper.activeIndex
  76. images.value.splice(index, 1)
  77. }
  78. let swiper
  79. onMounted(() => {
  80. swiper = new Swiper(swiper$.value, {
  81. observer: true,
  82. navigation: {
  83. prevEl: swiper$.value.querySelector('.swiper-button-prev'),
  84. nextEl: swiper$.value.querySelector('.swiper-button-next'),
  85. },
  86. on: {
  87. observerUpdate: function () {
  88. swiper.slideTo(images.value.length - 1, 0, false)
  89. },
  90. },
  91. })
  92. images.value = notify.value.media?.image || []
  93. // if (!notify.value.media) {
  94. // notify.value.media = {}
  95. // }
  96. // notify.value.media['image'] = images.value
  97. console.log(swiper)
  98. })
  99. </script>
  100. <style lang="scss" scoped>
  101. .del-btn {
  102. width: 24px;
  103. height: 24px;
  104. background: rgba(0, 0, 0, 0.6);
  105. border-radius: 50%;
  106. position: absolute;
  107. cursor: pointer;
  108. top: 10px;
  109. right: 10px;
  110. z-index: 10;
  111. display: flex;
  112. align-items: center;
  113. justify-content: center;
  114. .iconfont {
  115. font-size: 1em;
  116. }
  117. }
  118. .placeholder {
  119. cursor: pointer;
  120. width: 100%;
  121. height: 100%;
  122. display: flex;
  123. align-items: center;
  124. justify-content: center;
  125. .icon {
  126. display: flex;
  127. align-items: center;
  128. justify-content: center;
  129. flex-direction: column;
  130. color: rgba(255, 255, 255, 0.6);
  131. font-size: 14px;
  132. span {
  133. margin-top: 10px;
  134. }
  135. }
  136. .tips {
  137. font-size: 12px;
  138. padding: 10px;
  139. position: absolute;
  140. bottom: 0;
  141. width: 100%;
  142. color: rgba(255, 255, 255, 0.3);
  143. }
  144. }
  145. .media-image {
  146. width: 100%;
  147. height: 100%;
  148. .swiper {
  149. width: 100%;
  150. height: 100%;
  151. .swiper-slide {
  152. text-align: center;
  153. display: flex;
  154. justify-content: center;
  155. align-items: center;
  156. width: 100%;
  157. height: 100%;
  158. background-position: center center;
  159. background-size: contain;
  160. }
  161. .swiper-button-prev,
  162. .swiper-button-next {
  163. width: 32px;
  164. height: 32px;
  165. background: rgba(0, 0, 0, 0.2);
  166. border-radius: 50%;
  167. &::after {
  168. font-weight: bold;
  169. font-size: 14px;
  170. }
  171. }
  172. }
  173. .add {
  174. cursor: pointer;
  175. font-size: 12px;
  176. position: absolute;
  177. bottom: 0;
  178. left: 0;
  179. width: 100%;
  180. height: 32px;
  181. line-height: 32px;
  182. text-align: center;
  183. background: linear-gradient(180deg, rgba(0, 0, 0, 0.1), #000 200%);
  184. border-radius: 0 0 4px 4px;
  185. z-index: 10;
  186. &.disable {
  187. pointer-events: none;
  188. }
  189. span {
  190. color: #0076f6;
  191. }
  192. }
  193. }
  194. </style>