萌新微信小程序成长-2(上传图片以及预览和大图预览)

local:function(e) {
    var that = this;
    var a = that.data.imageList.length
    wx.chooseImage({
      count: 9-a, // 默认9
      sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
      success: function(res) {
        // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
        var temp = res.tempFilePaths
        var img = that.data.imageList
        //这里直接将临时文件路径push到数组里面直接就可以for遍历显示了
        var xd=that.data.xd
        for (var i = 0; i < temp.length; i++) {
         if(that.data.new1==1){
           xd.push(temp[i])
           img.push(temp[i])
         }else{
           img.push(temp[i])
         }
        }
        if (img.length <= 9) {
          that.setData({
            imageList: img,
            xd:xd
          })
        }
        var qb = that.data.imageList.length;
        if (qb < 9) {
          that.setData({
            num: qb,
          })
        } else {
          that.setData({
            num: 9,
          })
        }
      },
      fail: function(res) {
        console.log(res.errMsg)
      }
    })
  },
 // 预览大图
 preview: function(e) {
    var that = this;
    var src = e.currentTarget.dataset.src; //获取data-src
    var imgList = that.data.imageList;
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: imgList // 需要预览的图片http链接列表
    })
  },
  //上传图片
  upload:function(){
  var that=this
  var image1=that.data.imageList
  for (var i = 0; i < image1.length; i++) {
   wx.uploadFile({
       url: app.globalData.url + 'parameter=' + par + '&service=cn.ant.add.goodsimage&serviceCode=02&token=xxx',
       filePath: image1[i],
       name: 'file',
       header: {
         'content-type': 'multipart/form-data'
       },
       formData: {

       },
       success(res) {
         console.log(res)
         wx.redirectTo({
           url: './fbcg/fbcg?goodsid=' + that.data.goodsId,
         })     
       },
       fail: function (res) {
         console.log(res)
        if(that.data.new1!=null){
          wx.redirectTo({
            url: './fbcg/fbcg?goodsid=' + that.data.goodsId,
          })     
        }
       },
       complete() {
        
       }
     })
     }
     }

你可能感兴趣的:(小程序)