uni.downloadFile({
url: detail.value.imageUrl,
success: (res) => {
if (res.statusCode === 200) {
if (detail.value.mediaType === 2) {
// 视频保存:需确保临时文件带 .mp4 后缀,否则部分系统保存成功但相册无视频
const tempPath = res.tempFilePath;
const saveVideo = (filePath) => {
uni.saveVideoToPhotosAlbum({
filePath: filePath,
success: () => {
uni.hideLoading();
uni.showToast({
title: '已保存到相册',
icon: 'success'
});
},
fail: (err) => {
uni.hideLoading();
handleSaveError(err);
}
});
};
if (!tempPath.endsWith('.mp4')) {
const fs = uni.getFileSystemManager();
const targetPath = `${wx.env.USER_DATA_PATH}/wallpaper_${Date.now()}.mp4`;
fs.copyFile({
srcPath: tempPath,
destPath: targetPath,
success: () => {
saveVideo(targetPath);
},
fail: () => {
// 复制失败则用原路径尝试保存
saveVideo(tempPath);
}
});
} else {
saveVideo(tempPath);
}
} else {
// 图片保存
uni.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success: () => {
uni.hideLoading();
uni.showToast({
title: '已保存到相册',
icon: 'success'
});
},
fail: (err) => {
uni.hideLoading();
handleSaveError(err);
}
});
}
}
},
fail: () => {
uni.hideLoading();
uni.showToast({
title: '下载失败',
icon: 'none'
});
}
});
