request.ts 860 Bytes
import axios from 'axios';
import { message as $message } from 'antd';

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

Service.interceptors.request.use(
  config => {
    const aToken = sessionStorage.getItem('aToken') || 'bearer fff06119-e69f-4527-a4bc-d4ca613e09be';
    aToken && (config.headers.Authorization = aToken);
    return config;
  },
  error => {
    Promise.reject(error);
  }
);

Service.interceptors.response.use(
  (response: any) => {
    const res = response.data;
    if (res.code === 0 || res.success) {
      return Promise.resolve(res);
    } else {
      // if (res.code === 400 || res.code === 401) {
      $message.error(res.message);
      //   return
      // } else {
      return Promise.reject(res.message);
      // }
    }
  },
  error => {
    Promise.reject(error);
  }
);

export default Service;