import axios from 'axios'
import Vue from 'vue'
import { getToken } from '@/utils/auth'
import { Message } from 'element-ui'
import store from '@/store'
import {
  baseURL
} from '@/config/settings'
// 创建starof模块
export const fs = require('fs');
// 创建axios实例
const service = axios.create({
  baseURL,
  timeout: 25000, // 请求超时时间
  headers: {
    'Content-Type': 'application/json;charset=UTF-8'
  }
})
service.interceptors.request.use(
  config => {
    if (store.getters.token) {
      config.headers['X-Token'] = getToken()
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// respone拦截器
service.interceptors.response.use(
  response => {
    const res = response.data
    if (res.resultCode !== '200') {
      if (res.resultCode === '50002' || res.resultCode === '50001' || res.resultCode === '60001') {
        return res
      } else {
        Message({
          message: res.resultMsg || '请求数据失败',
          type: 'error',
          duration: 2 * 1000
        })
      }
      return res
    } else {
      return res.resultBody
    }
  },
  error => {
    return Promise.reject(error)
  }
)
export function fileUpload(url, params = {}) {
  return new Promise((resolve, reject) => {
    service({
      url: url,
      method: 'post',
      data: params,
      headers: { 'Content-Type': 'multipart/form-data' }
    }).then(response => {
      resolve(response)
    }).catch(error => {
      reject(error)
    })
  })
}

export const excelUpload = (url,data) => axios({
  method: 'post',
  url: url,  
  data: data,
  headers:{ 'Content-Type': 'multipart/form-data' }
});
export default service
Vue.prototype.$http = service
