diff --git a/pages/qrcode/index.js b/pages/qrcode/index.js index 197b58b..07490fb 100644 --- a/pages/qrcode/index.js +++ b/pages/qrcode/index.js @@ -136,6 +136,93 @@ Page({ icon: 'none', duration: 2000 }); + }, + + // 长按二维码保存到相册 + onQRCodeLongPress: function () { + const { qrCodeUrl } = this.data; + + if (!qrCodeUrl) { + wx.showToast({ + title: '请先生成二维码', + icon: 'none', + duration: 2000 + }); + return; + } + + // 显示加载提示 + wx.showLoading({ + title: '保存中...', + mask: true + }); + + // 下载图片到本地临时文件 + wx.downloadFile({ + url: qrCodeUrl, + success: (res) => { + if (res.statusCode === 200) { + // 保存图片到相册 + wx.saveImageToPhotosAlbum({ + filePath: res.tempFilePath, + success: () => { + wx.hideLoading(); + wx.showToast({ + title: '保存成功', + icon: 'success', + duration: 2000 + }); + }, + fail: (err) => { + wx.hideLoading(); + console.error('保存图片到相册失败:', err); + + // 检查是否是权限问题 + if (err.errMsg.includes('auth deny')) { + wx.showModal({ + title: '保存失败', + content: '需要相册权限才能保存二维码,请在设置中开启', + confirmText: '去设置', + cancelText: '取消', + success: (modalRes) => { + if (modalRes.confirm) { + wx.openSetting({ + success: (settingRes) => { + console.log('设置结果:', settingRes); + } + }); + } + } + }); + } else { + wx.showToast({ + title: '保存失败,请重试', + icon: 'none', + duration: 2000 + }); + } + } + }); + } else { + wx.hideLoading(); + console.error('下载图片失败,状态码:', res.statusCode); + wx.showToast({ + title: '下载图片失败,请重试', + icon: 'none', + duration: 2000 + }); + } + }, + fail: (err) => { + wx.hideLoading(); + console.error('下载图片失败:', err); + wx.showToast({ + title: '保存失败,请重试', + icon: 'none', + duration: 2000 + }); + } + }); } }); \ No newline at end of file diff --git a/pages/qrcode/index.wxml b/pages/qrcode/index.wxml index 883016b..bc6a0ad 100644 --- a/pages/qrcode/index.wxml +++ b/pages/qrcode/index.wxml @@ -31,6 +31,7 @@ mode="aspectFit" bindload="onImageLoad" binderror="onImageError" + bindlongpress="onQRCodeLongPress" > 加载中... diff --git a/pages/qrcode/index.wxss b/pages/qrcode/index.wxss index cf63f36..4ae7929 100644 --- a/pages/qrcode/index.wxss +++ b/pages/qrcode/index.wxss @@ -14,6 +14,7 @@ margin-bottom: 20px; width: 100%; box-sizing: border-box; + margin-left: -5px; } .user-info-header { @@ -110,7 +111,7 @@ .qrcode-image { width: 300px; height: 300px; - margin: 0 auto; + margin: 0 auto 0 -5px; background-color: white; padding: 15px; border-radius: 10px; @@ -149,7 +150,7 @@ .qrcode-placeholder { width: 300px; height: 300px; - margin: 0 auto; + margin: 0 auto 0 -5px; background-color: #f0f0f0; border-radius: 10px; display: flex;