TabVoice.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <template>
  2. <div>
  3. <div class="select-item2" v-if="reply.url">
  4. <p class="item-name">{{ reply.name }}</p>
  5. <el-row class="ope-row" justify="center">
  6. <WxVoicePlayer :url="reply.url" />
  7. </el-row>
  8. <el-row class="ope-row" justify="center">
  9. <el-button type="danger" circle @click="onDelete"><Icon icon="ep:delete" /></el-button>
  10. </el-row>
  11. </div>
  12. <el-row v-else style="text-align: center">
  13. <!-- 选择素材 -->
  14. <el-col :span="12" class="col-select">
  15. <el-button type="success" @click="showDialog = true">
  16. 素材库选择<Icon icon="ep:circle-check" />
  17. </el-button>
  18. <el-dialog
  19. title="选择语音"
  20. v-model="showDialog"
  21. width="90%"
  22. append-to-body
  23. destroy-on-close
  24. >
  25. <WxMaterialSelect
  26. type="voice"
  27. :account-id="reply.accountId"
  28. @select-material="selectMaterial"
  29. />
  30. </el-dialog>
  31. </el-col>
  32. <!-- 文件上传 -->
  33. <el-col :span="12" class="col-add">
  34. <el-upload
  35. :action="UPLOAD_URL"
  36. :headers="HEADERS"
  37. multiple
  38. :limit="1"
  39. :file-list="fileList"
  40. :data="uploadData"
  41. :before-upload="beforeVoiceUpload"
  42. :on-success="onUploadSuccess"
  43. >
  44. <el-button type="primary">点击上传</el-button>
  45. <template #tip>
  46. <div class="el-upload__tip">
  47. 格式支持 mp3/wma/wav/amr,文件大小不超过 2M,播放长度不超过 60s
  48. </div>
  49. </template>
  50. </el-upload>
  51. </el-col>
  52. </el-row>
  53. </div>
  54. </template>
  55. <script lang="ts" setup>
  56. import WxMaterialSelect from '@/views/mp/components/wx-material-select'
  57. import WxVoicePlayer from '@/views/mp/components/wx-voice-play'
  58. import { UploadType, useBeforeUpload } from '@/views/mp/hooks/useUpload'
  59. import type { UploadRawFile } from 'element-plus'
  60. import { getAccessToken } from '@/utils/auth'
  61. import { Reply } from './types'
  62. const message = useMessage()
  63. const UPLOAD_URL = import.meta.env.VITE_BASE_URL + '/admin-api/mp/material/upload-temporary'
  64. const HEADERS = { Authorization: 'Bearer ' + getAccessToken() } // 设置上传的请求头部
  65. const props = defineProps<{
  66. modelValue: Reply
  67. }>()
  68. const emit = defineEmits<{
  69. (e: 'update:modelValue', v: Reply)
  70. }>()
  71. const reply = computed<Reply>({
  72. get: () => props.modelValue,
  73. set: (val) => emit('update:modelValue', val)
  74. })
  75. const showDialog = ref(false)
  76. const fileList = ref([])
  77. const uploadData = reactive({
  78. accountId: reply.value.accountId,
  79. type: 'voice',
  80. title: '',
  81. introduction: ''
  82. })
  83. const beforeVoiceUpload = (rawFile: UploadRawFile) => useBeforeUpload(UploadType.Voice, 10)(rawFile)
  84. const onUploadSuccess = (res: any) => {
  85. if (res.code !== 0) {
  86. message.error('上传出错:' + res.msg)
  87. return false
  88. }
  89. // 清空上传时的各种数据
  90. fileList.value = []
  91. uploadData.title = ''
  92. uploadData.introduction = ''
  93. // 上传好的文件,本质是个素材,所以可以进行选中
  94. selectMaterial(res.data)
  95. }
  96. const onDelete = () => {
  97. reply.value.mediaId = null
  98. reply.value.url = null
  99. reply.value.name = null
  100. }
  101. const selectMaterial = (item: Reply) => {
  102. showDialog.value = false
  103. // reply.value.type = ReplyType.Voice
  104. reply.value.mediaId = item.mediaId
  105. reply.value.url = item.url
  106. reply.value.name = item.name
  107. }
  108. </script>
  109. <style lang="scss" scoped>
  110. .select-item2 {
  111. padding: 10px;
  112. margin: 0 auto 10px;
  113. border: 1px solid #eaeaea;
  114. .item-name {
  115. overflow: hidden;
  116. font-size: 12px;
  117. text-align: center;
  118. text-overflow: ellipsis;
  119. white-space: nowrap;
  120. .ope-row {
  121. width: 100%;
  122. padding-top: 10px;
  123. text-align: center;
  124. }
  125. }
  126. .col-select {
  127. width: 49.5%;
  128. height: 160px;
  129. padding: 50px 0;
  130. border: 1px solid rgb(234 234 234);
  131. }
  132. .col-add {
  133. float: right;
  134. width: 49.5%;
  135. height: 160px;
  136. padding: 50px 0;
  137. border: 1px solid rgb(234 234 234);
  138. .el-upload__tip {
  139. line-height: 18px;
  140. text-align: center;
  141. }
  142. }
  143. }
  144. </style>