1.先创建一个js文件如(other.js)
// 导出
export default {
// 选择上传视频
uploadVideo(thisVideo, customArray) {
const that = thisVideo;
// 选择视频
uni.chooseVideo({
sourceType: ['album', 'camera'], // 类型[相册,照相机]
compressed: true, // 是否压缩上传的视频
camera: 'back',
success: async function(res) {
uni.showLoading({
title: '上传中请稍后',
});
that.videoSrc = res.tempFilePath;
uni.uploadFile({
url: `${process.env.VUE_APP_BASE_API}/v2/upload`,
filePath: res.tempFilePath,
name: 'file',
success: (res) => {
const _res = JSON.parse(res.data);
that.videoArray.push(_res.data.id);
for (let i = 0; i < customArray.length; i++) {
if (customArray[i].file_type === 2) {
customArray[i].file_ids = that.videoArray;
}
}
},
complete: function(res) {
uni.hideLoading();
},
});
},
});
},
// 上传视频end
};
2.页面中使用
<script>
import other from '@/utils/other';
export default ({
data() {
return {
resData: {},
};
},
methods: {
videoUpload() {
const that = this;
//other就是你创建的文件夹,uploadVideo是写的方法名
other.uploadVideo(that, this.resData);
},
});
</script>