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

axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8';

const service = axios.create({
  baseURL: '/deva-api',
  // baseURL: 'http://124.223.15.102:8888',
  timeout: 10000
});

service.interceptors.request.use(config => {
  config.headers['Authorization'] = 'Bearer ' + 'eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoxLCJ0ZW5hbnRpZCI6MSwidXNlcl9rZXkiOiI1NjFiNTA4My0zNzU3LTQ4NzAtOTQ1Mi01MDVjNmE4NzI1ZTkiLCJ1c2VybmFtZSI6ImFkbWluIn0.erDoYvf3eOkcLOqznRXyHL-yBoz9WNvCBcZfqTERV5f8cB7YyU6t6-5uxWM_-Drf7925hagMsJPk0q3Usbeu2Q';
  return config;
}, error => {
});

service.interceptors.response.use(res => {
    return res.data;
  },
  error => {
    console.log('err' + error);
    let { message } = error;
    if (message == 'Network Error') {
      message = '后端接口连接异常';
    } else if (message.includes('timeout')) {
      message = '系统接口请求超时';
    } else if (message.includes('Request failed with status code')) {
      message = '系统接口' + message.substr(message.length - 3) + '异常';
    }
    ElMessage({ message: message, type: 'error', duration: 5 * 1000 });
    return Promise.reject(error);
  }
);

function request({ method = 'get', url, data = {}, params = {} }) {
  return service({
    method,
    url,
    data,
    params
  });
}

export default request;
