关于javascript:微信小程序根据手机的操作系统保存多图到相册相册中以顺序显示

10次阅读

共计 2217 个字符,预计需要花费 6 分钟才能阅读完成。

解决:1. 下载的多图保留到相册后,不是间断的。2.ios 下载的图片和安卓下载的图片不是正确的程序
计划:1. 不能应用循环下载图片,须要应用到递归。2. 通过判断不同的操作系统来决定采纳正序或倒序下载

技术总结:
1. 获取以后手机操作系统(app.js)

let t = this;
wx.getSystemInfo({success: function (res) {
    t.globalData.systemInfo = res;
    wx.setStorageSync('system',res.system)
    // 这里能够获取到其余的一些零碎信息
  }
});

2. 在下载图片的页面配置点击事件(index.js)

  data:{
    system: "", // 操作系统
    urls:[
        "https://cn.vuejs.org/images/logo.png",
        "https://cn.vuejs.org/images/logo.png",
        "https://cn.vuejs.org/images/logo.png",
        "https://cn.vuejs.org/images/logo.png"
    ]  // 图片集
  },
  onShow: function () {let system = wx.getStorageSync('system') || '';
    this.setData({system: system})
  },
  downloadUrls() { // 下载图片集
    let that = this;
    wx.getSetting({ // 获取保留到相册的权限
      success: function (settingdata) {if (!settingdata.authSetting['scope.writePhotosAlbum']) { // 未批准受权
          wx.authorize({ // 弹出批准受权框
            scope: 'scope.writePhotosAlbum',
            success() { // 用户批准受权后的回调
              that.checkSystemTyep();},
            fail() { // 用户回绝受权后的回调
              showToast("请批准关上相册的权限")
            }
          })
        } else { // 用户曾经受权过了
          that.checkSystemTyep(); // 检测手机操作系统并下载图片}
      }
    })
  },
  /**
   * 通过操作系统判断 是 ios 须要正序下载图片 安卓倒序下载图片
   */
  checkSystemTyep() {
    let that = this;
    let urls = this.data.urls
    let system = this.data.system;
    var i = urls.length - 1;
    console.log('system:', system.indexOf('iOS'), system, i)
    if (system.indexOf('iOS') != -1) {that.dow_temp1(0,urls);
    } else {that.dow_temp(i,urls);
    }
  },
  dow_temp1(i,urls) { // 递归下载相册 ios 正序下载图片
    let that = this;
    var all_n = urls.length;
    if (i < all_n) {
      let downloadTask = wx.downloadFile({url: urls[i],
        success: function (res) {
          let temp = res.tempFilePath
          console.info("长期门路", temp)
          wx.saveImageToPhotosAlbum({ // 保留到本地
            filePath: temp,
            success(res) {console.info('第', (i), '张保留胜利');
              that.dow_temp1(i + 1,urls)
            },
            fail: function (err) {console.info('第', (i), '张保留失败');
              that.dow_temp1(i,urls);
            }
          })
        },
        fail: function (res) {showToast('获取图片长期门路失败')
        },
      })
    } else if (i == all_n) {showToast('图片保留胜利')
    }
  },
  dow_temp(i,urls) { // 递归下载相册 安卓顺叙下载图片
    let that = this;
    var all_n = urls.length;
    if (i >= 0) {
      let downloadTask = wx.downloadFile({url: urls[i],
        success: function (res) {
          let temp = res.tempFilePath
          console.info("长期门路", temp)
          wx.saveImageToPhotosAlbum({ // 保留到本地
            filePath: temp,
            success(res) {console.info('第', (i), '张保留胜利');
              that.dow_temp(i - 1,urls)
            },
            fail: function (err) {console.info('第', (i), '张保留失败');
              that.dow_temp(i,urls);
            }
          })
        },
        fail: function (res) {showToast('获取图片长期门路失败')
        },
      })
    } else if (i < 0) {showToast('图片保留胜利')
    }
  },
  showToast(title) {
      wx.showToast({
        title,
        icon: 'none',
        duration: 2000,
        mask: true,
      })
},
正文完
 0