From 0dbb9915224617f154d4cb3b9218479004c6da76 Mon Sep 17 00:00:00 2001 From: douboer Date: Wed, 11 Feb 2026 12:10:49 +0800 Subject: [PATCH] update at 2026-02-11 12:10:49 --- AI_CONTEXT.md | 5 +++++ miniprogram/pages/index/index.js | 22 +++++++++++++++++++--- miniprogram/utils/mp/canvas-export.js | 8 +++++++- 3 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 AI_CONTEXT.md diff --git a/AI_CONTEXT.md b/AI_CONTEXT.md new file mode 100644 index 0000000..7a903e7 --- /dev/null +++ b/AI_CONTEXT.md @@ -0,0 +1,5 @@ +# 项目 AI 协作说明 +## 项目目录结构 + + + diff --git a/miniprogram/pages/index/index.js b/miniprogram/pages/index/index.js index 94891cc..307b1dd 100644 --- a/miniprogram/pages/index/index.js +++ b/miniprogram/pages/index/index.js @@ -104,6 +104,22 @@ function showExportError(title, error, fallback) { }) } +function toPngSaveError(saveResult) { + if (!saveResult || saveResult.success) { + return null + } + + if (saveResult.reason === 'private_api_banned') { + return new Error('当前小程序账号不支持“保存到相册”(private api banned)。请改用“导出 SVG”分享,或更换支持该能力的账号/版本。') + } + + if (saveResult.reason === 'auth_denied') { + return saveResult.error || new Error('保存失败,请检查相册权限') + } + + return saveResult.error || new Error('保存 PNG 失败') +} + function writePngBufferToTempFile(pngBuffer, fontName) { const safeName = String(fontName || 'font').replace(/[<>:"/\\|?*\x00-\x1F]/g, '_').slice(0, 60) || 'font' const filePath = `${wx.env.USER_DATA_PATH}/${safeName}_${Date.now()}.png` @@ -906,7 +922,7 @@ Page({ const saveResult = await savePngToAlbum(pngPath) if (!saveResult.success) { - throw saveResult.error || new Error('保存 PNG 失败') + throw toPngSaveError(saveResult) } } wx.showToast({ title: 'PNG 已保存到相册', icon: 'success' }) @@ -970,7 +986,7 @@ Page({ if (saveResult.success) { wx.showToast({ title: 'PNG 已保存到相册', icon: 'success' }) } else { - showExportError('导出 PNG 失败', saveResult.error, '保存失败,请检查相册权限') + showExportError('导出 PNG 失败', toPngSaveError(saveResult), '保存 PNG 失败') } } catch (error) { showExportError('导出 PNG 失败', error, '请稍后重试') @@ -1029,7 +1045,7 @@ Page({ if (saveResult.success) { wx.showToast({ title: 'PNG 已保存到相册', icon: 'success' }) } else { - showExportError('导出 PNG 失败', saveResult.error, '保存失败,请检查相册权限') + showExportError('导出 PNG 失败', toPngSaveError(saveResult), '保存 PNG 失败') } } catch (error) { showExportError('导出 PNG 失败', error, '请稍后重试') diff --git a/miniprogram/utils/mp/canvas-export.js b/miniprogram/utils/mp/canvas-export.js index ee86402..a45662f 100644 --- a/miniprogram/utils/mp/canvas-export.js +++ b/miniprogram/utils/mp/canvas-export.js @@ -104,7 +104,9 @@ async function savePngToAlbum(filePath) { return { success: true } } catch (error) { const errMsg = String(error && error.errMsg ? error.errMsg : error) - const needAuth = errMsg.includes('auth deny') || errMsg.includes('authorize') + const lowerErrMsg = errMsg.toLowerCase() + const needAuth = lowerErrMsg.includes('auth deny') || lowerErrMsg.includes('authorize') + const privateApiBanned = lowerErrMsg.includes('private api banned') if (needAuth) { const modalRes = await showModal({ @@ -120,6 +122,10 @@ async function savePngToAlbum(filePath) { return { success: false, needAuth, + privateApiBanned, + reason: privateApiBanned + ? 'private_api_banned' + : (needAuth ? 'auth_denied' : 'save_failed'), error, } }