import axios from 'axios'
import {
  getToken
} from '@/utils/auth'

// var uploadUrl =
//   process.env.NODE_ENV === "test" ||
//   process.env.NODE_ENV === "development" ?
//   "/api/sysFiles/upload" :
//   process.env.NODE_ENV + "/sysFiles/upload"

// var uploadV1Url =
//   process.env.NODE_ENV === "test" ||
//   process.env.NODE_ENV === "development" ?
//   "/api/sysFiles/v1/upload" :
//   process.env.NODE_ENV + "/sysFiles/v1/upload"

var uploadUrl = process.env.VUE_APP_BASE_API + "/sysFiles/upload"
var uploadV1Url = process.env.VUE_APP_BASE_API + "/sysFiles/v1/upload"
var importZipUrl = process.env.VUE_APP_BASE_API + "/bizImport/importZip"
/**
 * 封装上传文件的post方法
 * @param data
 * @returns {Promise}
 */
export function upload(data) {
  return new Promise((resolve, reject) => {
    axios.post(uploadUrl, data, {
      headers: {
        'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime(),
        'authorization': getToken(),
      }
    }).then(response => {
      resolve(response.data)
    }).catch(error => {
      reject(error)
    })
  })
}


/**
 * 封装上传文件方法 支持自定义Formdata 的key
 * @param data
 * @returns {Promise}
 */
export function uploadV1(data) {
  return new Promise((resolve, reject) => {
    axios.post(uploadV1Url, data, {
      headers: {
        'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime(),
        'authorization': getToken(),
      }
    }).then(response => {
      resolve(response.data)
    }).catch(error => {
      reject(error)
    })
  })
}

/**
 * 批量上传文件 
 * @param data   formData类型 * 参数1 type:导入数据类别(biz_cultural_relic、biz_exhibition二选一) 参数2 zipFile:zip压缩文件
 * @param callback  用于获取上传进度
 * @returns {Promise}
 */
export function importZip(data, callback) {
  return new Promise((resolve, reject) => {
    axios.post(importZipUrl, data, {
      headers: {
        'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime(),
        'authorization': getToken(),
      },
      onUploadProgress: progressEvent => {
        // 原生获取上传进度的事件
        if (progressEvent.lengthComputable) {
          callback && callback(progressEvent);
        }
      },
    }).then(response => {
      resolve(response.data)
    }).catch(error => {
      reject(error)
    })
  })
}
