// import axios from 'axios'
import { https, cookie } from 'apec-fe-utils'
import { Message } from 'element-ui'
import { defaultConfig, errMsg } from './config'
// import * as handlers from './handlers' // 规范接口处理
import * as handlers from './handlersForOld' // 旧系统接口处理

// https保留了axios的所有属性
// 请求拦截
https.interceptors.request.use(config => {
  return handlers.interReq(config)
}, error => {
  console.log('request err =', error) // for debug
  return Promise.reject(error)
})

// 响应拦截
https.interceptors.response.use(res => {
  return handlers.interRes(res)
}, error => {
  return handlers.interResErr(error)
})

const $https = {
  post: (url, param = {}, configs) => {
    return https.post(url, param, { ...defaultConfig, ...configs })
      .then(res => {
        return handlers.resFn(res) // 接口处理
      })
      .catch(err => {
        // 出现错误
        console.log('$https catch err =', err)
        Message({
          message: err.msg || 'Oops... 程序出错了 >.<',
          type: 'error',
          duration: 2 * 1000
        })
        return new Promise(() => {}) // 真正中断promise链, 不会继续走入业务的then而导致data为undefined
      })
  },
  get: (url, param = {}, configs) => {
    return https.get(url, param, { ...defaultConfig, ...configs })
      .then(res => {
        return handlers.resFn(res) // 接口处理
      })
      .catch(err => {
        // 出现错误
        console.log('$https catch err =', err)
        Message({
          message: err.msg || 'Oops... 程序出错了 >.<',
          type: 'error',
          duration: 2 * 1000
        })
        return new Promise(() => {}) // 真正中断promise链, 不会继续走入业务的then而导致data为undefined
      })
  },
  all: https.all,
}
export default $https
