
// // import { getToken } from './token'

// export default function request (url, options = {}) {
//   const headers = new Headers();
//   headers.append('Content-Type', 'application/json;charset=utf8');
//   // headers.append('X-Authorization-JWT', getToken());

//   Object.keys(options.headers || {}).forEach(key => headers.append(key, options.headers[key]))

//   return fetch(url, { ...options, headers, credentials: 'include', mode: 'cors' })
//     .then(response => response.json())
//     .then(data => {
//       if (data.code === 1000) return data.data
//       return Promise.reject(data)
//     })
// }


import axios from 'axios'
import store from '../store'

export const domain = process.env.NODE_ENV === 'development' ? '' : 'http://colmo-service.dianyang.njyunzhi.com'

export default function (url, options) {
  const { params, header, ...leftOptions } = options || {}

  return new Promise((resolve, reject) => {
    const { code, token } = store.state.user
    let header = {
      'X-Authorization-JWT': token,
    }
    axios.request({
      ...leftOptions,
      url: `${domain}${url}`,
      // header: header,
      headers: header,
    }).then(res => {
      const { code, data, message } = res.data
      if (code === 1000) {
        resolve(data)
      }
    }).catch(err => {
      const message = err.message || err.errMsg || err
      console.error("🚀 错误~ err", err)


      if (err.message.includes('timeout')) {
        // 请求超时
        // Message.error('请求超时')
      }
      reject(message)
    }).finally(() => {
      // 请求结束
    })
  })
}


