小程序下载视频到本地
wxml代码
<view class="button_view">下载视频</view>
wxss代码
.button_view{
margin-top:40rpx;
width:100%;
height:90rpx;
line-height:90rpx;
text-align: center;
background: chocolate;
font-size: 30rpx;
color: #fff;
}
js代码
data: {
video_url:'',//视频路径
},
// 下载
download_fun: function (e) {
var that = this
if (!wx.saveImageToPhotosAlbum) {
wx.showModal({
title: '提示',
content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
})
return;
}
// 检查用户授权
wx.getSetting({
success(res) {
if (!res.authSetting['scope.writePhotosAlbum']) {
// 接口调用询问
wx.authorize({
scope: 'scope.writePhotosAlbum',
success() {
that.dow_video()
},
fail() {
// 用户拒绝授权
wx.showModal({
title: '保存视频',
content: '保存视频需要您授权',
showCancel: true,
confirmText: '确定',
success(res) {
if (res.confirm) {
// 打开设置页面
wx.openSetting({
success(res) {
if (res.authSetting['scope.writePhotosAlbum']) {
that.dow_video()
} else {
wx.showToast({
title: '授权失败!',
icon: "none"
});
}
},
fail(res) {
wx.showToast({
title: '授权失败!',
icon: "none"
});
}
});
} else if (res.cancel) {
wx.showToast({
title: '您已取消授权!',
icon: "none"
});
}
}
})
}
})
} else {
that.dow_video()
}
},
fail(res) {
console.log(res);
}
})
},
// 下载视频
dow_video: function () {
var that = this;
wx.showLoading({
title: '下载视频中',
})
// 用户已经同意小程序使用相册的授权
wx.downloadFile({
url: that.data.video_url,//要下载的视频路径
success: function (res) {
that.setData({
is_button: true
})
var temp = res.tempFilePath;//获取临时文件路径
wx.saveVideoToPhotosAlbum({
filePath: temp,
success: function () {
wx.hideLoading()
wx.showToast({
title: '视频保存成功',
duration: 3000,
icon: 'none'
})
},
fail: function () {
wx.hideLoading()
}
})
},
fail: function (res) {
wx.hideLoading()
wx.showToast({
icon: 'none',
title: '获取视频临时路径失败',
})
}
})
},
ps:样式随便写的,主要实现功能