import { useRouter } from 'vue-router';
import Request from '@/service/request/index.js'
import {message} from "@/utils/message.js";
const request = new Request({
  baseURL: import.meta.env.VITE_BASEURL,
  timeout: 1000 * 60 * 5,
  interceptors: {
    //实例的请求拦截器
    requestInterceptors: (config) => {
      const token=localStorage.getItem('token')
      config.headers['Content-Type'] = config.method === 'get' ?
        'application/x-www-form-urlencoded' :
        'application/json'
      if (config.isFormData) {
        config.headers['Content-Type'] = 'multipart/form-data';
        config.headers['Authorization'] = token
      } else {
        config.headers['Authorization'] = token
      }
      return config;
    },
    //实例的响应拦截器
    responseInterceptors: async (response) => {
      if (response.data.status===1){
        message.warning(response.data.msg)
      }
      if ([200, 201, 204].includes(response.status)) {
        return response.config.responseType === 'blob' ? response : response;
      } else {
        return Promise.reject(new Error(response.data.msg || 'An error occurred.'));
      }
    }
  }
});
const fontRequest = (config) => {
  if (['get', 'GET'].includes(config.method)) {
    config.params = config.data;
  }
  return request.request(config);
};

export default fontRequest;
