一、选择图片

clickAddImage() {
    let self = this;
    wx.chooseImage({
      count: 5,  // 有默认参数
      sourceType: ['album', 'camera'], // 有默认参数
      success: function(res) {
        let arrayPath = res.tempFilePaths
        for (let index in arrayPath) {
          self.uploadImage(arrayPath[index])
        }
      },
    })
  },
  async uploadImage(filPath) {
    const data = await netWorkAdapter.uploadImage(filPath)
    let restult = data.data
    if (restult.length > 0) {
      let imageFile = restult[0]
      let messageStr = JSON.stringify(imageFile)
      this.sendImageMessage(messageStr)
    }else {
      wx.showToast({
        title: '图片内容为空',
        icon: 'none',
        duration: 2000
      })
    }
  },

二、组装网络请求

/**
 * 上传图片
 */
async function uploadImage(filPath) {
  let url = serverDefine.serverDefine.baseUrl + serverDefine.serverDefine.uploadImage
  let name = commonTools.stringToMD5(Math.random().toString)
  let parmater = { userId: utils.userInfo.userId, uuid: utils.userInfo.uuid, token: utils.userInfo.token, appResource: utils.userInfo.appResource}
  const data = await httpServer.uploadImage(url, filPath, name, parmater)
  return data
}

三、上传图片

/**
 * 图片上传
 */
function uploadImage(url, filePath, name, parameter) {
  wx.showLoading({
    title: '上传中...',
  })
  return new Promise((success, faile) => {
    wx.uploadFile({
      url: url,
      filePath: filePath,
      name: name,
      formData: parameter,
      header: parameter,
      success(res) {
        wx.hideLoading()
        let data = JSON.parse(res.data)
        if (data.code == 200) {
          success({code: 1, data: data.object })
        }else {
          wx.showToast({
            title: '上传失败',
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail(res) {
        wx.hideLoading()
        wx.showToast({
          title: '上传失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
  })
}

总结:微信小程序上传图片,功能并不复杂,关键要注意结构的合理,把网络请求和图片获取,分开进行;