import { useUserStore } from '@/stores/user';
import axios from 'axios';

const baseURL = 'https://wzpmc.cn:18080/';
const axiosInstance = axios.create({
  baseURL,
});
// 自动添加token到请求中.
axiosInstance.interceptors.request.use((config) => {
  const userStore = useUserStore();
  config.headers.setAuthorization(userStore.token, false);
  return config;
});
// 自动获取响应中的token.
axiosInstance.interceptors.response.use((response) => {
  const userStore = useUserStore();
  const authorization = response.headers['set-authorization'] as string | undefined;
  if (authorization) {
    console.log(123);
    userStore.token = authorization;
  }
  return response;
});
export default axiosInstance;
export type RawResp = Record<string, unknown>;
