Audio.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <div class="media" v-if="media.length">
  3. <i class="iconfont icon-music"></i>
  4. <span class="music-name">{{ media[0].name }}</span>
  5. </div>
  6. <div class="placeholder" @click="file.click()" v-if="!media.length">
  7. <div class="icon">
  8. <i class="iconfont icon-add"></i>
  9. <span>{{ $t('components.uploadAudio') }}</span>
  10. </div>
  11. <div class="tips">{{ $t('components.limitFileSize', { file: 'mp3/wav', size: '5' }) }}</div>
  12. <input ref="file" type="file" style="display: none" accept=".mp3, .wav" @change="onChange" />
  13. </div>
  14. <div class="del-btn" v-if="isEdit && notify.media?.[notify.type]?.length" @click="delMedia">
  15. <i class="iconfont icon-delete"></i>
  16. </div>
  17. </template>
  18. <script setup>
  19. import { ref, onMounted, inject } from 'vue'
  20. import { checkSizeLimitFree, base64ToDataURL, getMime } from '@/utils/file'
  21. import i18n from '@/i18n'
  22. const { t } = i18n.global
  23. const notify = inject('notify')
  24. const isEdit = inject('isEdit')
  25. const emits = defineEmits(['tips'])
  26. const file = ref(null)
  27. const media = ref([])
  28. const onChange = e => {
  29. if (!e.target.files.length) {
  30. return
  31. }
  32. let file = e.target.files[0]
  33. if (!['mp3', 'wav'].includes(getMime(file.name))) {
  34. emits('tips', t('components.FileSizeTips', { size: '5', file: 'mp3/wav' }))
  35. return
  36. }
  37. if (checkSizeLimitFree(file.size, 5)) {
  38. let reader = new FileReader()
  39. reader.onload = function () {
  40. media.value.push({ src: base64ToDataURL(reader.result), file, name: file.name })
  41. notify.value.media['audio'] = media.value
  42. }
  43. reader.readAsDataURL(file)
  44. } else {
  45. emits('tips', t('components.FileSizeTips', { size: '5', file: 'mp3/wav' }))
  46. }
  47. e.target.value = ''
  48. }
  49. const delMedia = () => {
  50. media.value.splice(0, 1)
  51. }
  52. onMounted(() => {
  53. media.value = notify.value.media?.audio || []
  54. })
  55. </script>
  56. <style lang="scss" scoped>
  57. .del-btn {
  58. width: 24px;
  59. height: 24px;
  60. background: rgba(0, 0, 0, 0.6);
  61. border-radius: 50%;
  62. position: absolute;
  63. cursor: pointer;
  64. top: 10px;
  65. right: 10px;
  66. z-index: 10;
  67. display: flex;
  68. align-items: center;
  69. justify-content: center;
  70. .iconfont {
  71. font-size: 1em;
  72. }
  73. }
  74. .placeholder {
  75. cursor: pointer;
  76. width: 100%;
  77. height: 100%;
  78. display: flex;
  79. align-items: center;
  80. justify-content: center;
  81. .icon {
  82. display: flex;
  83. align-items: center;
  84. justify-content: center;
  85. flex-direction: column;
  86. color: rgba(255, 255, 255, 0.6);
  87. font-size: 14px;
  88. span {
  89. margin-top: 10px;
  90. }
  91. }
  92. .tips {
  93. font-size: 12px;
  94. padding: 10px;
  95. position: absolute;
  96. bottom: 0;
  97. width: 100%;
  98. color: rgba(255, 255, 255, 0.3);
  99. }
  100. }
  101. .media {
  102. width: 100%;
  103. height: 100%;
  104. display: flex;
  105. align-items: center;
  106. justify-content: center;
  107. .music-name {
  108. margin-left: 5px;
  109. }
  110. .iconfont {
  111. font-size: 1em;
  112. }
  113. .add {
  114. cursor: pointer;
  115. font-size: 12px;
  116. position: absolute;
  117. bottom: 0;
  118. left: 0;
  119. width: 100%;
  120. height: 32px;
  121. line-height: 32px;
  122. text-align: center;
  123. background: linear-gradient(180deg, rgba(0, 0, 0, 0.1), #000 200%);
  124. border-radius: 0 0 4px 4px;
  125. z-index: 10;
  126. &.disable {
  127. pointer-events: none;
  128. }
  129. span {
  130. color: #0076f6;
  131. }
  132. }
  133. }
  134. </style>