微信小程序Promise多图片上传 - Upload Multiple Images with Promise.all

{
  chooseImage: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1,
      success(res) {
        var images = that.data.images;
        var image_paths = that.data.paths;
        images.push(res.tempFiles[0]);
        image_paths.push(res.tempFilePaths[0]);
        that.setData({
          paths: image_paths,
          images: images,
          img_count: that.data.img_count + 1
        })
      }
    })
   },
   uploadImages: function () {
    wx.showNavigationBarLoading();
    var that = this;
    that.setData({
      ids: [],
      submitDisabled: true
    })
    var promise = Promise.all(that.data.paths.map((pic, index) => {
      console.log(pic)
      return new Promise(function (resolve, reject) {
        wx.uploadFile({
          url: app.globalData.webservice_url + '/biz/activity/uploadFile',
          filePath: pic,
          name: 'files',
          header: {
            'X-Token': wx.getStorageSync('token')
          },
          formData: app.encode({

          }),
          success: function (res) {
            var d = JSON.parse(res.data);
            if (d.code === 1) {
              var ids = that.data.ids;
              ids.push(d.data.id);
              that.setData({
                ids: ids
              })
              // console.log(d.data.id)
              resolve(res.data);
            } else {
              app.showModal('提示', d.desc)
            }
          },
          fail: function (err) {
            reject(new Error('failed to upload file'));
            console.log("fail")
          },
          complete: function () {
            that.setData({
              submitDisabled: false
            })
            wx.hideNavigationBarLoading();
          }
        });
      });
    })).then(that.add_activity);
  },
  add_activity: function () {
    var that = this;
    if (that.data.shopid === undefined) {
      app.showModal("提示", '请选择店铺');
      return;
    }
    if (that.data.activityName === undefined || that.data.activityName === '') {
      app.showModal("提示", '请输入项目名称');
      return;
    }
    if (that.data.activityType === undefined) {
      app.showModal("提示", '请选择活动分类');
      return;
    }
    if (this.data.count === undefined) {
      app.showModal("提示", '请输入拼团最高人数');
      return;
    }
    if (this.data.end_time === undefined) {
      app.showModal("提示", '请选择活动结束时间');
      return;
    }
    if (this.data.original === undefined) {
      app.showModal("提示", '请输入原单价');
      return;
    }
    if (this.data.discount === undefined) {
      app.showModal("提示", '请输入活动单价');
      return;
    }
    if (this.data.activity_desc === undefined || this.data.activity_desc === '') {
      app.showModal('提示', "请输入活动描述");
      return;
    }
    console.log(that.data.ids)
    wx.request({
      url: app.globalData.webservice_url + '/biz/activity/addActivity',
      data: JSON.stringify(app.encode({
        businessId: wx.getStorageSync('businessId'),
        shopId: that.data.shopid,
        projectName: that.data.activityName,
        projectType: that.data.activityType,
        totalNum: that.data.count,
        activityEndDate: that.data.end_time + " 23:59:59",
        originalPrice: parseFloat(that.data.original),
        activityPrice: parseFloat(that.data.discount),
        remark: that.data.activity_desc,
        fileIds: that.data.ids.join(',')
      })),
      success: function (res) {
        if (res.data.code === 1) {
          app.showModal('提示', res.data.desc, function(){
            app._from_("shopCenter");
            wx.switchTab({
              url: '../user/shopCenter',
            })
          })
        } else {
          app.showModal('提示', res.data.desc);
        }
      },
      fail: function (res) {
        app.showModal('提示', res.errMsg);
      },
      complete: function (res) {
        console.log(res);
      },
      header: {
        'X-Token': wx.getStorageSync('token')
      },
      method: "POST"
    })
  },
}

你可能感兴趣的:(微信小程序Promise多图片上传 - Upload Multiple Images with Promise.all)