**首先获取用户权限 **
getUserSetting(isVideo, downLOadPath) {
wx.showLoading({
title: '加载中...',
mask: true,
});
let that = this
wx.getSetting({
success(res) {
if (res.authSetting['scope.writePhotosAlbum'] === undefined) {
wx.authorize({
scope: 'scope.writePhotosAlbum',
success() {
console.log('打开了授权')
that.downloadSaveFile(isVideo, downLOadPath)
},
fail() {
wx.showToast({
title: '授权失败',
icon: 'none'
});
}
})
} else if (!res.authSetting['scope.writePhotosAlbum']) {
wx.openSetting({
success(res) {
// console.log(res)
if (res.authSetting['scope.writePhotosAlbum']) {
console.log('授权了')
that.downloadSaveFile(isVideo, downLOadPath)
} else {
wx.showToast({
title: '您没有授权,无法保存到相册',
icon: 'none'
})
}
},
fail() {
wx.showToast({
title: '授权失败',
icon: 'none'
});
},
})
} else {
that.downloadSaveFile(isVideo, downLOadPath)
}
},
fail() {
wx.showToast({
title: '加载失败',
icon: 'none'
});
},
complete() {
wx.hideLoading()
}
})
},
保存到本地封装的方法
downloadSaveFile(isVido, downLoadPath) {
let fileName = new Date().valueOf();
let savePath = '';
if (isVido) {
savePath = wx.env.USER_DATA_PATH + '/' + fileName + '.mp4'
} else {
savePath = wx.env.USER_DATA_PATH + '/' + fileName + '.jpg'
}
// console.log(downLoadPath)
// console.log(savePath)
wx.downloadFile({
url: downLoadPath,
filePath: savePath,
success(res) {
// console.log(res);
let path = res.filePath;
if (isVido) {
wx.saveVideoToPhotosAlbum({
filePath: path,
success() {
console.log("视频保存成功:")
}
})
} else {
wx.saveImageToPhotosAlbum({
filePath: path,
success() {
console.log("视频保存成功:")
}
})
}
}
})
//监听下载进度
downloadTask.onProgressUpdate((res) => {
// console.log(res);
if (res.progress === 100) {
this.setData({
progress: ''
});
wx.hideLoading({
success: (res) => {},
})
} else {
this.setData({
progress: res.progress + '%'
});
wx.showLoading({
title: '下载中'+ res.progress + '%',
})
}
});
},
点击保存
preservation(e) {
let index = e.currentTarget.dataset.index
if (this.data.imgObj[index].type == 'image') {
this.getUserSetting(false, this.data.imgObj[index].path);
} else if (this.data.imgObj[index].type === 'video') {
this.getUserSetting(true, this.data.imgObj[index].path);
}
},
//这里边的true 和 false 是判断他是不是视频
|