uni-app 抖音小程序上传视频到七牛云功能

541 阅读1分钟

// 视频上传 <view class="upload_video" @click="uploadVideo()"></view>

onLoad(options) {
    // 获取七牛云token
    this.getQiToken()
			
},
methods: {
    getQiToken(){
          this.$http.post('获取token的地址接口', {
		showLoading: false
	  }).then(res => {
		console.log(res, '我的token')
		if (res.code == 200) {
                    this.uptoken = res.data.upload_token
		} else {
			uni.showToast({
                            title: res.msg,
                            icon: 'none'
			})
		}
	})
    },
    uploadVideo() {
        var that = this
	uni.chooseVideo({
            count: 1,
            sourceType: ['camera', 'album'],
            success: function(res) {
            //限制的50m视频大小
		let sizeMb = 50*1024*1024
		let size = res.size
		if(sizeMb-size<0){
                    uni.showToast({
			title:'视频过大,请重新上传',
			icon:'none',
			duration:1500
                    })
		}else{
                    let filePath = res.tempFilePath;
                    that.videoUrl = res.tempFilePath
                    uni.uploadFile({
			url: 'https://up-z1.qiniup.com', //七牛云地址
			filePath: filePath,
                        name: 'file',
			formData: {
                            'key':"video_template/"+data+'/'+that.$util.uuid()+".mp4", //上传到七牛云的视频的名字(必须保证唯一)
                            'token': that.uptoken
			},
			success: (uploadFileRes) => {
                           let dataUrl = JSON.parse(uploadFileRes.data)
                            that.QiUrl = dataUrl.key //七牛云返回的视频连接
                        
			}
                    });
                }
            },
            fail(err) {
                 that.scopeAlbums() //没有开启本地上传权限时,再次开启
            }
	});
				
     },
     // 授权
    scopeAlbums() {
	tt.getSetting({
            success(res) {
		if (res.authSetting['scope.album'] == false || res.authSetting['scope.camera'] == false) {
                    uni.showModal({
			title: '温馨提示',
			content: '为了更好的体验小程序,请授权相册哦~',
			showCancel: false,
			success() {
                            tt.openSetting({
				success(res2) {
                                    if (res2.authSetting['scope.album'] || res.authSetting[
													'scope.camera']) {
					return true;
                                    } else {
					return false;
                                    }
				},
				fail(err) {
                                    return false;
				}
                            });
                         }
                    });
		} else {
                    return true;
		}
            },
            fail(err) {
		return false;
            }
	});
    },
}