import axios from "axios";
|
import pinia from './pinia.js';
|
import { storeToRefs } from 'pinia'
|
import { useUserStore } from "@/stores/user.js";
|
const { serverIp } = storeToRefs(useUserStore(pinia));
|
|
const axiosInstance = axios.create();
|
axiosInstance.defaults.withCredentials = true; // 保持请求头
|
axiosInstance.interceptors.request.use(
|
config => {
|
// 在发送请求之前做些什么,例如添加 token
|
return config;
|
},
|
error => {
|
// 对请求错误做些什么
|
return Promise.reject(error);
|
}
|
);
|
// 响应拦截器
|
axiosInstance.interceptors.response.use(
|
response => {
|
// 对响应数据做点什么
|
return response;
|
},
|
error => {
|
// 对响应错误做点什么
|
return Promise.reject(error);
|
}
|
);
|
|
export default async function request(config) {
|
|
// 构建完整的 URL
|
const fullUrl = `${location.protocol}//${serverIp.value}:8100/bl/${config.url}`; // 直接使用 userStore.serverIp
|
|
// 发送请求,使用提取或默认的配置
|
const response = await axiosInstance({
|
...config,
|
url: fullUrl, // 覆盖原始配置中的 url
|
});
|
|
return response;
|
}
|