const FormData = require('./formdata')

function uploadImage(filePath) {
  let form = new FormData()
  form.appendFile("file", filePath)
  const { contentType, buffer } = form.getData()

  return new Promise((resolve, reject) => {
    wx.request({
      url: "https://www.qdeasydo.com/api/folder/up",
      method: 'POST',
      header: {
        'content-type': contentType
      },
      data: buffer,
      success: res => {
        if (res.data.code == 200) resolve("https://www.qdeasydo.com/api/folder/" + res.data.expands.file)
        else reject(res.data.message)
      },
      fail: () => reject('网络连接异常')
    })
  })
}

module.exports = { uploadImage }