191 lines
4.5 KiB
JavaScript
191 lines
4.5 KiB
JavaScript
// 检查相册权限
|
|
const checkAlbumPermission = () => {
|
|
return new Promise((resolve, reject) => {
|
|
// #ifdef MP-WEIXIN
|
|
// 微信小程序权限检查
|
|
uni.getSetting({
|
|
success(res) {
|
|
if (res.authSetting['scope.writePhotosAlbum'] === undefined) {
|
|
// 从未询问过
|
|
requestAlbumPermission().then(resolve).catch(reject);
|
|
} else if (res.authSetting['scope.writePhotosAlbum'] === false) {
|
|
// 已拒绝,需要引导用户打开
|
|
resolve(false);
|
|
} else {
|
|
resolve(true);
|
|
}
|
|
},
|
|
fail: reject
|
|
});
|
|
// #endif
|
|
|
|
// #ifdef MP-KUAISHOU
|
|
// 快手小程序权限检查
|
|
uni.getSetting({
|
|
success(res) {
|
|
if (res.authSetting['scope.album'] === undefined) {
|
|
requestAlbumPermission().then(resolve).catch(reject);
|
|
} else if (res.authSetting['scope.album'] === false) {
|
|
resolve(false);
|
|
} else {
|
|
resolve(true);
|
|
}
|
|
},
|
|
fail: reject
|
|
});
|
|
// #endif
|
|
|
|
// 其他平台
|
|
// #ifndef MP-WEIXIN || MP-KUAISHOU
|
|
resolve(true);
|
|
// #endif
|
|
});
|
|
};
|
|
|
|
// 请求相册权限
|
|
const requestAlbumPermission = () => {
|
|
return new Promise((resolve, reject) => {
|
|
// #ifdef MP-WEIXIN
|
|
uni.authorize({
|
|
scope: 'scope.writePhotosAlbum',
|
|
success: () => resolve(true),
|
|
fail: () => resolve(false)
|
|
});
|
|
// #endif
|
|
|
|
// #ifdef MP-KUAISHOU
|
|
uni.authorize({
|
|
scope: 'scope.album',
|
|
success: () => resolve(true),
|
|
fail: () => resolve(false)
|
|
});
|
|
// #endif
|
|
|
|
// 其他平台
|
|
// #ifndef MP-WEIXIN || MP-KUAISHOU
|
|
resolve(true);
|
|
// #endif
|
|
});
|
|
};
|
|
|
|
// 打开系统设置
|
|
const openSettings = () => {
|
|
return new Promise((resolve) => {
|
|
uni.showModal({
|
|
title: '提示',
|
|
content: '需要您授权访问相册才能保存视频,是否前往设置?',
|
|
success(res) {
|
|
if (res.confirm) {
|
|
uni.openSetting({
|
|
success: () => resolve(true),
|
|
fail: () => resolve(false)
|
|
});
|
|
} else {
|
|
resolve(false);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
};
|
|
|
|
// 保存视频到相册
|
|
export const saveVideoToAlbum = (filePath) => {
|
|
return new Promise(async (resolve, reject) => {
|
|
try {
|
|
// 1. 检查权限
|
|
const hasPermission = await checkAlbumPermission();
|
|
|
|
if (!hasPermission) {
|
|
// 尝试打开设置
|
|
const settingsResult = await openSettings();
|
|
if (!settingsResult) {
|
|
return reject(new Error('相册权限被拒绝'));
|
|
}
|
|
|
|
// 再次检查权限
|
|
const newPermission = await checkAlbumPermission();
|
|
if (!newPermission) {
|
|
return reject(new Error('相册权限被拒绝'));
|
|
}
|
|
}
|
|
|
|
// 2. 保存文件
|
|
// #ifdef MP-WEIXIN
|
|
uni.saveVideoToPhotosAlbum({
|
|
filePath,
|
|
success: resolve,
|
|
fail: reject
|
|
});
|
|
// #endif
|
|
|
|
// #ifdef MP-KUAISHOU
|
|
uni.saveMediaToAlbum({
|
|
filePath,
|
|
mediaType: 'video',
|
|
success: resolve,
|
|
fail: reject
|
|
});
|
|
// #endif
|
|
|
|
// #ifndef MP-WEIXIN || MP-KUAISHOU
|
|
uni.showToast({
|
|
title: '当前平台暂不支持保存到相册',
|
|
icon: 'none'
|
|
});
|
|
reject(new Error('Unsupported platform'));
|
|
// #endif
|
|
} catch (error) {
|
|
reject(error);
|
|
}
|
|
});
|
|
};
|
|
|
|
// 下载并保存视频
|
|
export const downloadAndSaveVideo = async (url) => {
|
|
try {
|
|
uni.showLoading({ title: '下载中...', mask: true });
|
|
|
|
// 1. 下载文件
|
|
const downloadRes = await uni.$http.download(url);
|
|
|
|
// 2. 保存到相册
|
|
await saveVideoToAlbum(downloadRes.tempFilePath);
|
|
|
|
uni.showToast({ title: '已成功保存到相册' });
|
|
return true;
|
|
} catch (error) {
|
|
console.error('下载保存失败:', error);
|
|
uni.showToast({
|
|
title: `保存失败: ${error.message || '未知错误'}`,
|
|
icon: 'none'
|
|
});
|
|
return false;
|
|
} finally {
|
|
uni.hideLoading();
|
|
}
|
|
};
|
|
|
|
// 获取图片选择权限
|
|
export const getImagePermission = () => {
|
|
return new Promise((resolve, reject) => {
|
|
// #ifdef MP-WEIXIN
|
|
uni.chooseImage({
|
|
count: 1,
|
|
success: resolve,
|
|
fail: reject
|
|
});
|
|
// #endif
|
|
|
|
// #ifdef MP-KUAISHOU
|
|
uni.chooseImage({
|
|
count: 1,
|
|
success: resolve,
|
|
fail: reject
|
|
});
|
|
// #endif
|
|
|
|
// #ifndef MP-WEIXIN || MP-KUAISHOU
|
|
reject(new Error('Unsupported platform'));
|
|
// #endif
|
|
});
|
|
}; |