/**
 *  axios 工具函数层
 */
import axios from "axios";
import { Message } from "element-ui";
// import local from "@/utils/local";
// 请求超时时间
// axios.defaults.timeout = 15 * 1000;

// 设置统一服务器地址
// axios.defaults.baseURL = local.getLocal("serverUrl");
// 请求拦截
axios.interceptors.request.use(
  (config) => {
    // let token = local.getLocal('token')
    // if (token) {
    //     config.headers.Authorization = token
    // }
    return config;
  },
  (err) => {
    return Promise.reject(err);
  }
);

// 响应拦截
axios.interceptors.response.use(
  (response) => {
    if (response.data.code !== undefined && response.data.msg !== undefined) {
      // 取出数据
      let { code, msg } = response.data;
      if (code === -1) {
        Message.error({
          message: msg,
        });
      } else if (code === 401) {
        Message.error({
          message: msg,
        });
      }
    }
    return response;
  },
  (err) => {
    if (err.message.includes("timeout")) {
      Message.error({
        message: "请求超时，请稍后再试",
      });
    }
    return Promise.reject(err);
  }
);

export default axios;
