收藏
回答

保存网络海报到本地微信相册的功能开发工具和体验版本都是没有问题的 可是正式环境却保存不了

// 授权并保存图片(兼容网络URL和本地临时路径)
function klSqAndDownload(filePathOrUrl = ''{
  if (filePathOrUrl === "") {
    wx.showToast({ title'文件路径/URL为空'icon'error' });
    return false;
  }
  // 判断是否是网络URL(以http/https开头)
  const isNetworkUrl = filePathOrUrl.startsWith('http://') || filePathOrUrl.startsWith('https://');
  // 统一的保存方法(仅接收本地临时文件路径)
  const saveToAlbum = (tempFilePath) => {
    wx.saveImageToPhotosAlbum({
      filePath: tempFilePath,
      successfunction (res{
        console.log('保存成功', res);
        wx.showToast({
          title'保存成功',
          icon'success',
          duration2000
        })
      },
      failfunction (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,
      successfunction (res{
        wx.hideLoading();
        if (res.statusCode === 200) {
          // 下载成功,得到本地临时文件路径
          console.log('下载成功,临时路径:', res.tempFilePath);
          // 调用授权逻辑
          handleAuth(res.tempFilePath);
        } else {
          wx.showToast({ title'图片下载失败'icon'error' });
        }
      },
      failfunction (err{
        wx.hideLoading();
        console.log('下载失败', err);
        wx.showToast({ title'图片下载失败'icon'error' });
      }
    });
  };
  // 权限处理逻辑
  const handleAuth = (tempFilePath) => {
    wx.getSetting({
      successfunction (res{
        if (res.authSetting['scope.writePhotosAlbum']) {
          // 已授权,直接保存
          saveToAlbum(tempFilePath);
        } else {
          // 未授权,请求授权
          wx.authorize({
            scope'scope.writePhotosAlbum',
            successfunction () {
              wx.showToast({ title'授权成功'icon'success' });
              saveToAlbum(tempFilePath);
            },
            failfunction () {
              // 引导去设置页
              wx.showModal({
                title'权限申请',
                content'需要保存图片到相册的权限才能继续,是否前往设置页开启?',
                confirmText'去设置',
                cancelText'取消',
                successfunction (modalRes{
                  if (modalRes.confirm) {
                    wx.openSetting({
                      successfunction () {
                        // 延迟重试保存
                        setTimeout(() => {
                          saveToAlbum(tempFilePath);
                        }, 800);
                        wx.showToast({ title'请重试保存'icon'none' });
                      }
                    });
                  }
                }
              });
            }
          });
        }
      }
    });
  };


  // 主逻辑:网络URL先下载,本地路径直接处理权限
  if (isNetworkUrl) {
    downloadNetworkImage(filePathOrUrl);
  } else {
    handleAuth(filePathOrUrl);
  }
}
回答关注问题邀请回答
收藏

2 个回答

  • 小黎
    小黎
    01-08

    开发版打开调试去线上看有没有报错,是否配置了合法域名以及隐私协议

    01-08
    有用
    回复
  • 迪克
    迪克
    01-08

    下载的域名配了吗?

    隐私协议配了吗?

    01-08
    有用
    回复
登录 后发表内容