import axios from 'axios';
import { message, notify } from '@/utils/message.js';
import { load } from '../plugin/loading/loading';

const request = axios.create({
  baseURL: import.meta.env.VITE_SEVER_URL,
  timeout: 60000 // 请求超时时间
});

// 请求拦截器
request.interceptors.request.use(
  (config) => {
    load.show();
 
    return config;
  },
  (error) => {
    // 请求错误处理
    console.log(error); // for debug
    notify(error);
    Promise.reject(error);
  }
);

// 响应拦截器
request.interceptors.response.use(
  (response) => {
    load.hide();
    const res = response.data;
    // console.log(res);
    if (res.code == -1) {
      notify(res.desc);
    }
    return response;
  },
  (error) => {
    load.hide();
    console.log('err' + error); // for debug
    notify('服务端异常');
    return Promise.reject(error);
  }
);

export function apiGet(url, params) {
  return request({
    url: url,
    method: 'get',
    params: params
  });
}
export default request;
