function klSqAndDownload(filePathOrUrl = '') {
if (filePathOrUrl === "") {
wx.showToast({ title: '文件路径/URL为空', icon: 'error' });
return false;
}
const isNetworkUrl = filePathOrUrl.startsWith('http://') || filePathOrUrl.startsWith('https://');
const saveToAlbum = (tempFilePath) => {
wx.saveImageToPhotosAlbum({
filePath: tempFilePath,
success: function (res) {
console.log('保存成功', res);
wx.showToast({
title: '保存成功',
icon: 'success',
duration: 2000
})
},
fail: function (err) {
console.log('保存失败', err);
if (err.errMsg.includes('auth deny')) {
wx.showToast({ title: '权限未开启', icon: 'error' });
} else if (err.errMsg.includes('not absolute path')) {
wx.showToast({ title: '文件路径错误', icon: 'error' });
} else {
wx.showToast({ title: '保存失败', icon: 'error' });
}
}
});
};
const downloadNetworkImage = (url) => {
wx.showLoading({ title: '图片下载中...' });
wx.downloadFile({
url: url,
success: function (res) {
wx.hideLoading();
if (res.statusCode === 200) {
console.log('下载成功,临时路径:', res.tempFilePath);
handleAuth(res.tempFilePath);
} else {
wx.showToast({ title: '图片下载失败', icon: 'error' });
}
},
fail: function (err) {
wx.hideLoading();
console.log('下载失败', err);
wx.showToast({ title: '图片下载失败', icon: 'error' });
}
});
};
const handleAuth = (tempFilePath) => {
wx.getSetting({
success: function (res) {
if (res.authSetting['scope.writePhotosAlbum']) {
saveToAlbum(tempFilePath);
} else {
wx.authorize({
scope: 'scope.writePhotosAlbum',
success: function () {
wx.showToast({ title: '授权成功', icon: 'success' });
saveToAlbum(tempFilePath);
},
fail: function () {
wx.showModal({
title: '权限申请',
content: '需要保存图片到相册的权限才能继续,是否前往设置页开启?',
confirmText: '去设置',
cancelText: '取消',
success: function (modalRes) {
if (modalRes.confirm) {
wx.openSetting({
success: function () {
setTimeout(() => {
saveToAlbum(tempFilePath);
}, 800);
wx.showToast({ title: '请重试保存', icon: 'none' });
}
});
}
}
});
}
});
}
}
});
};
if (isNetworkUrl) {
downloadNetworkImage(filePathOrUrl);
} else {
handleAuth(filePathOrUrl);
}
}
开发版打开调试去线上看有没有报错,是否配置了合法域名以及隐私协议
下载的域名配了吗?
隐私协议配了吗?