Files
PC-vote/utils/http.js
DESKTOP-RQ919RC\Pc c79608c94f feat(投票页面): 更新投票详情页标签图标链接及优化搜索功能
- 修改投票详情页的推荐和精华标签图标为可点击链接
- 更新HTTP拦截器中的会话ID
- 优化搜索功能,修复搜索结果加载问题
- 调整样式文件和组件引用路径
- 更新构建输出文件版本号
2025-12-18 14:01:53 +08:00

131 lines
3.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import axios from "axios";
import QS from "qs";
import { ElMessage } from "element-plus";
axios.defaults.baseURL = "https://vote.gter.net";
axios.defaults.emulateJSON = true;
axios.defaults.withCredentials = true;
axios.interceptors.request.use(
//响应拦截
async (config) => {
// 开发时登录用的,可以直接替换小程序的 authorization
if (process.env.NODE_ENV !== "production") {
const miucms_session = "360b33b1b56ab9f3898d741e6205839a";
document.cookie = "miucms_session=" + miucms_session;
config["headers"]["authorization"] = miucms_session;
}
return config;
},
(error) => {
return Promise.error(error);
}
);
// 响应拦截器
axios.interceptors.response.use(
(response) => {
if (response.status === 200) return Promise.resolve(response); //进行中
else return Promise.reject(response); //失败
},
(error) => {
// 服务器状态码不是200的情况
if (error.response.status) {
switch (error.response.status) {
// 401: 未登录
case 401:
// goTologin() // 跳转登录页面
break;
default:
}
return Promise.reject(error.response);
}
}
);
/**
* get方法对应get请求
* @param {String} url [请求的url地址]
* @param {Object} params [请求时携带的参数]
*/
const get = (url, params) => {
return new Promise((resolve, reject) => {
axios
.get(url, { params })
.then((res) => resolve(res.data))
.catch((err) => reject(err.data));
});
};
/**
* post方法对应post请求
* @param {String} url [请求的url地址]
* @param {Object} params [请求时携带的参数]
*/
const post = (url, params) => {
return new Promise((resolve, reject) => {
//是将对象 序列化成URL的形式以&进行拼接
axios
.post(url, QS.stringify(params))
.then((res) => {
let data = res.data;
if (data.code == 401 && !process.server) goLogin();
resolve(data);
})
.catch((err) => {
if (err.data.code == 401) {
goLogin();
resolve(err.data);
} else reject(err.data);
});
});
};
const postV2 = (url, params) => {
return new Promise((resolve, reject) => {
//是将对象 序列化成URL的形式以&进行拼接
axios
.post(url, params)
.then((res) => {
let data = res.data;
if (data.code == 401 && !process.server) goLogin();
resolve(data);
})
.catch((err) => {
if (err.data.code == 401) {
goLogin();
resolve(err.data);
} else reject(err.data);
});
});
};
const del = (url, params) => {
return new Promise((resolve, reject) => {
//是将对象 序列化成URL的形式以&进行拼接
axios
.delete(url, { params })
.then((res) => {
let data = res.data;
if (data.code == 401 && !process.server) goLogin();
resolve(data);
})
.catch((err) => {
if (err.data.code == 401) {
goLogin();
resolve(err.data);
} else reject(err.data);
});
});
};
// 打开登录
const goLogin = () => {
if (typeof ajax_login === "function") ajax_login();
};
export default {
get,
post,
postV2,
del,
};