fix: 修复保存网络图片

This commit is contained in:
2025-12-06 23:32:16 +08:00
parent 0a864fd400
commit bbb5170802
2 changed files with 41 additions and 26 deletions

View File

@@ -56,7 +56,7 @@
// border-radius: 12px; // border-radius: 12px;
// border: 1px solid rgba(0, 0, 0, 0.06); // border: 1px solid rgba(0, 0, 0, 0.06);
// background: lightgray 50% / cover no-repeat; // background: lightgray 50% / cover no-repeat;
box-shadow: 0 4px 36px 0 rgba(0, 0, 0, 0.16); // box-shadow: 0 4px 36px 0 rgba(0, 0, 0, 0.16);
} }
.qrcodeTip { .qrcodeTip {
@@ -74,12 +74,14 @@
} }
.listPage { .listPage {
background-color: #FAFAFA; background-color: #fafafa;
font-family: "PingFang SC"; font-family: "PingFang SC";
transition: padding-top 0.3s ease-in-out; // 添加过渡动画,让布局变化更平滑 transition: padding-top 0.3s ease-in-out; // 添加过渡动画,让布局变化更平滑
display: flex; display: flex;
flex-direction: column; flex-direction: column;
height: calc(100vh - var(--status-bar-height, 0px) - var(--nav-bar-height, 0px) - 112px); // 减去底部导航栏高度 112px height: calc(
100vh - var(--status-bar-height, 0px) - var(--nav-bar-height, 0px) - 112px
); // 减去底部导航栏高度 112px
overflow: hidden; overflow: hidden;
.fixedHeader { .fixedHeader {
@@ -190,4 +192,4 @@
.guideBarHighZIndex { .guideBarHighZIndex {
z-index: 900 !important; // 正常状态,保持较高层级 z-index: 900 !important; // 正常状态,保持较高层级
} }

View File

@@ -1,35 +1,48 @@
import Taro from "@tarojs/taro"; import Taro from "@tarojs/taro";
export function saveImage(url) { export function saveImage(url) {
Taro.getSetting().then(async (res) => { // 下载网络图片,返回本地 temp 文件
if (!res.authSetting["scope.writePhotosAlbum"]) { const download = () => {
return Taro.downloadFile({ url }).then(res => {
if (res.statusCode === 200) {
return res.tempFilePath
}
throw new Error("图片下载失败")
})
}
const save = async () => {
try {
const filePath = await download()
await Taro.saveImageToPhotosAlbum({ filePath })
Taro.showToast({ title: "保存成功" })
} catch (e) {
console.error(e)
Taro.showToast({ title: "图片保存失败", icon: "none" })
}
}
Taro.getSetting().then(res => {
const authorized = res.authSetting["scope.writePhotosAlbum"]
if (!authorized) {
Taro.authorize({ Taro.authorize({
scope: "scope.writePhotosAlbum", scope: "scope.writePhotosAlbum",
success: async () => { success: () => {
try { save()
Taro.saveImageToPhotosAlbum({ filePath: url });
Taro.showToast({ title: "保存成功" });
} catch (e) {
Taro.showToast({ title: "图片保存失败", icon: "none" });
}
}, },
fail: () => { fail: () => {
Taro.showModal({ Taro.showModal({
title: "提示", title: "提示",
content: "需要开启相册权限才能保存图片", content: "需要开启相册权限才能保存图片",
success: (r) => { success: (r) => {
if (r.confirm) Taro.openSetting(); if (r.confirm) Taro.openSetting()
}, }
}); })
}, }
}); })
} else { } else {
try { save()
Taro.saveImageToPhotosAlbum({ filePath: url });
Taro.showToast({ title: "保存成功" });
} catch (e) {
Taro.showToast({ title: "图片保存失败", icon: "none" });
}
} }
}); })
} }