1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- import axios from "axios";
- import { getToken } from "@/utils/storage/sessionToken";
- import { TOKEN } from "@/constant";
- import { loadingBar, message } from "@/plugin/naive-ui";
- // 显示消息
- const showMessage = (res) => {
- if (res.Code !== 200) {
- message.error(res.Msg);
- }
- };
- const service = axios.create({
- baseURL: import.meta.env.VITE_API_BASE_URL,
- timeout: 60000 * 15,
- });
- // 添加请求拦截器
- service.interceptors.request.use(
- function (config) {
- // 在发送请求之前做些什么
- loadingBar.start();
- if (config.url !== "/VerifyTemplateMapData/Pu") {
- const formData = new FormData();
- if (
- config.url !== "/Login_Admin_verification" &&
- config.url !== "/Login_verification"
- ) {
- const token = getToken();
- formData.append(TOKEN, token);
- }
- Object.entries(config.data).forEach(([key, value]) => {
- formData.append(key, value);
- });
- config.data = formData;
- }
- return config;
- },
- function (error) {
- // 对请求错误做些什么
- return Promise.reject(error);
- }
- );
- // 添加响应拦截器
- service.interceptors.response.use(
- function (response) {
- // 2xx 范围内的状态码都会触发该函数。
- // 对响应数据做点什么
- const { data: res } = response;
- showMessage(res);
- loadingBar.finish();
- return response;
- },
- function (error) {
- // 超出 2xx 范围的状态码都会触发该函数。
- // 对响应错误做点什么
- return Promise.reject(error);
- }
- );
- export default service;
|