import Vue from 'vue'
import axios from 'axios'
import qs from 'qs'
import {
  VueAxios
} from './axios'
// import { notification } from 'ant-design-vue'

// 创建 axios 实例
const service = axios.create({
  // baseURL: process.env.BASE_URL, // api base_url
  baseURL: '',
  timeout: 6000000, // 请求超时时间,
  headers: {
    'Content-Type': 'application/json'
  }
})

const err = (error) => {
  if (error.response) {
    // 可在此处统一处理服务器端报错
    // const { data } = error.response
    // notification.error({
    //   message: 'Error',
    //   description: data.message || 'Try Again Later.'
    // })
  }
  return Promise.reject(error)
}

// request interceptor
service.interceptors.request.use(config => {
  // 可在此处添加请求自定义 token 请根据实际情况自行修改
  // const token = Vue.ls.get('Access-Token')
  config.headers.token = Vue.ls.get('token')
  return config
}, err)

// response interceptor
service.interceptors.response.use(response => response.data, err)

const installer = {
  vm: {},
  install(Vue) {
    Vue.use(VueAxios, service)
  }
}

/**
 * 整合url中拼接的参数
 * @method
 * @param {Object} params 传入需要拼接的参数对象
 * @return {String} 返回的参数字符串 例：keyword=黄河&number=1&string=huanghe
 */
function splicingParam(params) {
  let str = ""
  for (const key in params) {
    if (params.hasOwnProperty(key)) {
      str += "&" + key + "=" + params[key]
    }
  }
  return str.substr(1, str.length)
}

function get(url, params) {
  return new Promise((resolve, reject) => {
    service({
      method: 'GET',
      url,
      params: params
    }).then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}

function post(url, data) {
  return new Promise((resolve, reject) => {
    service({
      method: 'POST',
      url,
      data: data,
      headers: {
        'Content-Type': 'application/json;'
      }
    }).then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}

function postform(url, data) {
  return new Promise((resolve, reject) => {
    service({
      method: 'POST',
      url,
      data: qs.stringify(data),
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded;'
      }
    }).then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}

function put(url, data) {
  return new Promise((resolve, reject) => {
    service({
      method: 'PUT',
      url,
      data: data,
      headers: {
        'Content-Type': 'application/json;'
      }
    }).then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}

function putform(url, data) {
  return new Promise((resolve, reject) => {
    service({
      method: 'PUT',
      url,
      data: qs.stringify(data),
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded;'
      }
    }).then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}

function del(url, data) {
  return new Promise((resolve, reject) => {
    service({
      method: 'DELETE',
      url,
      data: data,
      headers: {
        'Content-Type': 'application/json;'
      }
    }).then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}

function delform(url, data) {
  return new Promise((resolve, reject) => {
    service({
      method: 'DELETE',
      url,
      data: qs.stringify(data),
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded;'
      }
    }).then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}

function postFile(url, data) {
  return new Promise((resolve, reject) => {
    service({
      method: 'POST',
      url,
      data: data,
      contentType: false,
      processData: false,
      headers: {
        'Content-Type': 'multipart/form-data',
      }
    }).then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}

export {
  installer as VueAxios,
  service as axios,
  splicingParam,
  get,
  post,
  del,
  put,
  putform,
  postform,
  delform,
  postFile
}
