新增:分享、下载图片、视频功能登

This commit is contained in:
2025-06-09 20:09:52 +08:00
parent 522281300f
commit 4eb8ef0f54
11 changed files with 1346 additions and 56 deletions

View File

@@ -327,4 +327,154 @@ export const original = (url) => {
return url.replace('/thumbnail/', '/');
}
return url;
};
/**
* 下载图片到相册
* @param {string} imageUrl 图片地址
* @param {function} successCallback 成功回调
* @param {function} failCallback 失败回调
*/
export const downloadImage = (imageUrl, successCallback, failCallback) => {
if (!imageUrl) {
uni.showToast({
title: '图片地址无效',
icon: 'none'
});
return;
}
uni.showLoading({
title: '下载中...',
mask: true
});
// 下载图片
uni.downloadFile({
url: imageUrl,
success: (res) => {
if (res.statusCode === 200) {
// 保存到相册
uni.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success: () => {
uni.hideLoading();
uni.showToast({
title: '保存成功',
icon: 'success'
});
successCallback && successCallback();
},
fail: (err) => {
uni.hideLoading();
console.log('保存图片失败:', err);
if (err.errMsg.includes('auth deny')) {
uni.showModal({
title: '提示',
content: '请允许访问相册后重试\n(右上角菜单 - 设置 - 相册)',
showCancel: false
});
} else {
uni.showToast({
title: '保存失败',
icon: 'none'
});
}
failCallback && failCallback(err);
}
});
} else {
uni.hideLoading();
uni.showToast({
title: '下载失败',
icon: 'none'
});
failCallback && failCallback();
}
},
fail: (err) => {
uni.hideLoading();
console.log('下载图片失败:', err);
uni.showToast({
title: '下载失败',
icon: 'none'
});
failCallback && failCallback(err);
}
});
};
/**
* 下载视频到相册
* @param {string} videoUrl 视频地址
* @param {function} successCallback 成功回调
* @param {function} failCallback 失败回调
*/
export const downloadVideo = (videoUrl, successCallback, failCallback) => {
if (!videoUrl) {
uni.showToast({
title: '视频地址无效',
icon: 'none'
});
return;
}
uni.showLoading({
title: '下载中...',
mask: true
});
// 下载视频
uni.downloadFile({
url: videoUrl,
success: (res) => {
if (res.statusCode === 200) {
// 保存到相册
uni.saveVideoToPhotosAlbum({
filePath: res.tempFilePath,
success: () => {
uni.hideLoading();
uni.showToast({
title: '保存成功',
icon: 'success'
});
successCallback && successCallback();
},
fail: (err) => {
uni.hideLoading();
console.log('保存视频失败:', err);
if (err.errMsg.includes('auth deny')) {
uni.showModal({
title: '提示',
content: '请允许访问相册后重试\n(右上角菜单 - 设置 - 相册)',
showCancel: false
});
} else {
uni.showToast({
title: '保存失败',
icon: 'none'
});
}
failCallback && failCallback(err);
}
});
} else {
uni.hideLoading();
uni.showToast({
title: '下载失败',
icon: 'none'
});
failCallback && failCallback();
}
},
fail: (err) => {
uni.hideLoading();
console.log('下载视频失败:', err);
uni.showToast({
title: '下载失败',
icon: 'none'
});
failCallback && failCallback(err);
}
});
};