/*
 * @Author: code4eat awesomedema@gmail.com
 * @Date: 2024-08-26 14:05:03
 * @LastEditors: code4eat awesomedema@gmail.com
 * @LastEditTime: 2025-03-14 13:59:18
 * @FilePath: /MediScreen/src/utils/request.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import axios from 'axios';
import { message } from 'antd'; // 假设你在使用 Ant Design 的 message 组件来显示提示框

const request = axios.create({
  baseURL: '/api',
  timeout:1800000,
});

// 请求拦截器
request.interceptors.request.use(
  config => {
    config.headers['Content-Type'] = 'application/json';
    config.headers['Accept'] = '*/*';
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

// 响应拦截器
request.interceptors.response.use(
  response => {
    if (response.data && response.data.code === 200) {
      return response.data.data;
    } else {
      message.error(response.data.msg || '请求失败');
      return Promise.reject(new Error(response.data.msg || '请求失败'));
    }
  },
  error => {
    const { response } = error;
    if (response && response.data) {
      const errorMessage = response.data.errorMessage || '服务器发生错误';
      message.error(`错误 ${response.data.errorCode}: ${errorMessage}`);
    } else {
      message.error('网络错误，请稍后重试');
    }
    return Promise.reject(error);
  }
);

export default request;
