From 9fad04b664a1781b85bcf61e8bfe4a34d6b009d6 Mon Sep 17 00:00:00 2001 From: Lj <1095098147@qq.com> Date: Mon, 21 Jul 2025 19:31:19 +0800 Subject: [PATCH] =?UTF-8?q?=E2=80=98=E4=BF=AE=E5=A4=8Dbug=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/subcomponent/promo-qrcode.vue | 180 ++++++++---------------------------- 1 file changed, 38 insertions(+), 142 deletions(-) diff --git a/pages/subcomponent/promo-qrcode.vue b/pages/subcomponent/promo-qrcode.vue index b288b0d..bc8e15c 100644 --- a/pages/subcomponent/promo-qrcode.vue +++ b/pages/subcomponent/promo-qrcode.vue @@ -51,6 +51,7 @@ import pullRefreshMixin from '@/pages/mixins/pullRefreshMixin.js' import config from '@/config.js' import navbar from '@/compoent/base/navbar.vue' +import authorize from '@/utils/authorize.js' export default { components : { navbar @@ -149,7 +150,7 @@ export default { }, // 保存到手机相册 - saveToAlbum() { + async saveToAlbum() { if (!this.qrcodeUrl) { uni.showToast({ title: '二维码还未加载完成', @@ -158,159 +159,54 @@ export default { return } - // 检查是否为iOS设备 - const systemInfo = uni.getSystemInfoSync() - const isIOS = systemInfo.platform === 'ios' - - // 显示加载提示 - uni.showLoading({ - title: '保存中...' - }) - - // 先下载图片到本地 - uni.downloadFile({ - url: this.qrcodeUrl, - success: res => { - uni.hideLoading() - if (res.statusCode === 200) { - this.saveLocalImage(res.tempFilePath, isIOS) - } else { - uni.showToast({ title: '下载失败', icon: 'none' }) - } - }, - fail: err => { - uni.hideLoading() - console.log('下载失败:', err) - uni.showToast({ title: '下载失败', icon: 'none' }) - } - }) + try { + // 请求相册权限 + await authorize('scope.writePhotosAlbum') + + // 获取图片信息并保存 + await this.imgApi(this.qrcodeUrl) + + } catch (error) { + console.log('保存失败:', error) + uni.showToast({ + title: '保存失败', + icon: 'none' + }) + } }, - // 保存本地图片到相册 - saveLocalImage(tempFilePath, isIOS) { - // #ifdef APP-PLUS - // APP端特殊处理 - uni.saveImageToPhotosAlbum({ - filePath: tempFilePath, - success: () => { - uni.showToast({ - title: '保存成功', - icon: 'success' - }) - }, - fail: (err) => { - console.log('保存失败', err) - // iOS权限处理 - if (isIOS || (err.errMsg && err.errMsg.includes('auth deny'))) { - uni.showModal({ - title: '权限提示', - content: '需要您授权保存相册权限,请在设置中开启', - confirmText: '去设置', - success: (res) => { - if (res.confirm) { - // #ifdef APP-PLUS - plus.runtime.openURL('app-settings://') - // #endif - // #ifdef MP-WEIXIN - uni.openSetting() - // #endif - } - } - }) - } else { - uni.showToast({ - title: '保存失败', - icon: 'none' - }) - } - } - }) - // #endif - - // #ifdef MP-WEIXIN - // 微信小程序端处理 - uni.getSetting({ - success: (res) => { - if (res.authSetting['scope.writePhotosAlbum'] === false) { - // 权限被拒绝,引导用户手动开启 - uni.showModal({ - title: '权限提示', - content: '需要您授权保存相册权限,请在设置中开启', - confirmText: '去设置', - success: (modalRes) => { - if (modalRes.confirm) { - uni.openSetting() - } - } - }) - } else { - // 尝试保存,如果没有权限会自动弹出授权框 + // 获取图片信息并保存到相册 + imgApi(image) { + return new Promise((resolve, reject) => { + // 获取图片的信息 + uni.getImageInfo({ + src: image, + success: (imageInfo) => { + // 保存图片到手机相册 uni.saveImageToPhotosAlbum({ - filePath: tempFilePath, + filePath: imageInfo.path, success: () => { - uni.showToast({ + uni.showModal({ title: '保存成功', - icon: 'success' + content: '图片已成功保存到相册', + showCancel: false }) + resolve() }, fail: (err) => { console.log('保存失败', err) - // iOS特殊处理 - if (isIOS) { - uni.showModal({ - title: 'iOS权限提示', - content: 'iOS设备需要手动授权相册权限,请在设置中开启', - confirmText: '去设置', - success: (modalRes) => { - if (modalRes.confirm) { - uni.openSetting() - } - } - }) - } else { - uni.showToast({ - title: '保存失败', - icon: 'none' - }) - } + reject(new Error('保存失败')) + }, + complete: (res) => { + console.log('保存结果:', res) } }) + }, + fail: (err) => { + console.log('获取图片信息失败:', err) + reject(new Error('获取图片信息失败')) } - }, - fail: () => { - // 直接尝试保存 - uni.saveImageToPhotosAlbum({ - filePath: tempFilePath, - success: () => { - uni.showToast({ - title: '保存成功', - icon: 'success' - }) - }, - fail: (err) => { - console.log('保存失败', err) - // iOS特殊处理 - if (isIOS) { - uni.showModal({ - title: 'iOS权限提示', - content: 'iOS设备需要手动授权相册权限,请在设置中开启', - confirmText: '去设置', - success: (modalRes) => { - if (modalRes.confirm) { - uni.openSetting() - } - } - }) - } else { - uni.showToast({ - title: '保存失败', - icon: 'none' - }) - } - } - }) - } + }) }) - // #endif }, // 图片加载成功