You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
173 lines
3.7 KiB
173 lines
3.7 KiB
<template>
|
|
<view class="audio-upload">
|
|
<view class="title-box">
|
|
<view class="title-pic">{{ title }}</view>
|
|
<button class="no-margin-button" size="mini" @click="chooseAudio">选择音频</button>
|
|
</view>
|
|
|
|
<view class="audio-upload-preview" @click="previewAudio" v-if="audioSrc">
|
|
<audio-play class="audio-player" :audio="audioSrc"></audio-play>
|
|
</view>
|
|
</view>
|
|
</template>
|
|
|
|
<script>
|
|
import { getToken } from '@/utils/auth.js';
|
|
import config from '@/config.js';
|
|
const baseUrl = config.baseUrl;
|
|
|
|
import audioPlay from '@/components/h-custom-audio/index.vue';
|
|
|
|
export default {
|
|
components: { audioPlay },
|
|
props: {
|
|
title: {
|
|
type: String,
|
|
default: ''
|
|
},
|
|
url: {
|
|
type: String,
|
|
default: ''
|
|
}
|
|
},
|
|
data() {
|
|
return {
|
|
audioSrc: '',
|
|
requestUrl: baseUrl + '/common/upload',
|
|
token: getToken()
|
|
};
|
|
},
|
|
watch: {
|
|
url: {
|
|
deep: true,
|
|
immediate: true,
|
|
handler(val, oval) {
|
|
if (val) {
|
|
this.audioSrc = baseUrl + val;
|
|
}
|
|
}
|
|
}
|
|
},
|
|
methods: {
|
|
chooseAudio() {
|
|
// #ifdef H5
|
|
uni.chooseFile({
|
|
count: 1, //选取的文件个数限制
|
|
extension: ['.mp3'], //可定义允许哪些后缀的文件可被选择
|
|
success: (res) => {
|
|
debugger;
|
|
|
|
let tempFilePaths = res.tempFilePaths;
|
|
uni.showLoading({
|
|
title: '上传中...'
|
|
});
|
|
uni.uploadFile({
|
|
name: 'audio', //文件上传的name值
|
|
url: this.requestUrl, //接口地址
|
|
header: {
|
|
Authorization: this_.token
|
|
}, //头信息
|
|
formData: {}, //上传额外携带的参数
|
|
filePath: tempFilePaths[0], //临时路径
|
|
fileType: 'audio', //文件类型
|
|
success: (uploadFileRes) => {
|
|
console.log(uploadFileRes);
|
|
|
|
uni.hideLoading();
|
|
const ret = JSON.parse(uploadFileRes.data);
|
|
console.log(ret);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
// #endif
|
|
|
|
// #ifdef MP-WEIXIN
|
|
let this_ = this;
|
|
|
|
wx.chooseMessageFile({
|
|
count: 1, //选取的文件个数限制
|
|
type: 'file',
|
|
// extension: ['.mp3'], //可定义允许哪些后缀的文件可被选择
|
|
success: (res) => {
|
|
let tempFilePaths = res.tempFiles;
|
|
console.log(tempFilePaths[0].name.split('.')[1])
|
|
let audioType = ['MP3','WAV','WMA','mp3']
|
|
if(!audioType.includes(tempFilePaths[0].name.split('.')[1])){
|
|
uni.showToast({
|
|
icon:'error',
|
|
title:'请上传mp3格式文件'
|
|
|
|
})
|
|
return
|
|
}
|
|
uni.showLoading({
|
|
title: '上传中...'
|
|
});
|
|
uni.uploadFile({
|
|
name: 'file', //文件上传的name值
|
|
url: this_.requestUrl, //接口地址
|
|
header: {
|
|
Authorization: this_.token
|
|
}, //头信息
|
|
formData: {}, //上传额外携带的参数
|
|
filePath: tempFilePaths[0].path, //临时路径
|
|
fileType: 'file', //文件类型
|
|
success: (uploadFileRes) => {
|
|
uni.hideLoading();
|
|
const ret = JSON.parse(uploadFileRes.data);
|
|
this_.audioSrc = ret.url;
|
|
console.log(ret);
|
|
|
|
let uploadFile = ret.fileName;
|
|
|
|
this_.$emit('fileSuccess', uploadFile); //返回上传成功的数据
|
|
},
|
|
fail: (err) => {
|
|
uni.hideLoading();
|
|
}
|
|
});
|
|
}
|
|
});
|
|
// #endif
|
|
},
|
|
previewAudio() {
|
|
uni.playVoice({
|
|
filePath: this.audioSrc
|
|
});
|
|
}
|
|
}
|
|
};
|
|
</script>
|
|
|
|
<style lang="scss" scoped>
|
|
.audio-upload {
|
|
display: flex;
|
|
align-items: center;
|
|
flex-direction: column;
|
|
}
|
|
.audio-upload-preview {
|
|
display: flex;
|
|
align-items: center;
|
|
margin-right: 20px;
|
|
width: 95%;
|
|
}
|
|
.audio-player{
|
|
width: 100%;
|
|
}
|
|
.preview-text {
|
|
margin-left: 10px;
|
|
}
|
|
.audio-upload-button button {
|
|
width: 120px;
|
|
margin: 0;
|
|
}
|
|
.title-box {
|
|
display: flex;
|
|
width: 94%;
|
|
align-items: center;
|
|
justify-content: space-between;
|
|
margin: 20rpx 3%;
|
|
padding-top: 10rpx;
|
|
}
|
|
</style>
|
|
|