2023-12-25 10:39:49 +00:00
|
|
|
|
import axios from 'axios';
|
|
|
|
|
import QS from 'qs';
|
2023-12-18 03:12:07 +00:00
|
|
|
|
import { ElMessage } from "element-plus";
|
|
|
|
|
|
|
|
|
|
|
2023-12-25 10:39:49 +00:00
|
|
|
|
axios.defaults.baseURL = 'https://interviewexperience.gter.net'
|
|
|
|
|
axios.defaults.emulateJSON = true
|
|
|
|
|
axios.defaults.withCredentials = true
|
2023-12-18 03:12:07 +00:00
|
|
|
|
|
2023-12-25 10:39:49 +00:00
|
|
|
|
axios.interceptors.request.use( //响应拦截
|
|
|
|
|
async config => {
|
|
|
|
|
// 开发时登录用的,可以直接替换小程序的 authorization
|
2024-05-15 02:58:10 +00:00
|
|
|
|
config['headers']['authorization'] = process.env.NODE_ENV !== "production" && "apd452evlcnw2vku5x8abv1ktf92gt71"
|
2024-01-03 10:31:15 +00:00
|
|
|
|
// config['headers']['authorization'] = "2lfrtq7h3ge634pl4ptlu2pbh2"
|
2023-12-25 10:39:49 +00:00
|
|
|
|
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);
|
2023-12-18 03:12:07 +00:00
|
|
|
|
}
|
2023-12-25 10:39:49 +00:00
|
|
|
|
});
|
2023-12-18 03:12:07 +00:00
|
|
|
|
|
2023-12-25 10:39:49 +00:00
|
|
|
|
/**
|
|
|
|
|
* get方法,对应get请求
|
|
|
|
|
* @param {String} url [请求的url地址]
|
|
|
|
|
* @param {Object} params [请求时携带的参数]
|
|
|
|
|
*/
|
|
|
|
|
const get = (url, params) => {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2024-01-09 07:11:03 +00:00
|
|
|
|
axios.get(url, { params }).then(res => {
|
|
|
|
|
resolve(res.data)
|
|
|
|
|
}).catch(err => reject(err.data))
|
2023-12-18 03:12:07 +00:00
|
|
|
|
});
|
2023-12-25 10:39:49 +00:00
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* post方法,对应post请求
|
|
|
|
|
* @param {String} url [请求的url地址]
|
|
|
|
|
* @param {Object} params [请求时携带的参数]
|
|
|
|
|
*/
|
|
|
|
|
const post = (url, params) => {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
//是将对象 序列化成URL的形式,以&进行拼接
|
2024-01-03 07:28:16 +00:00
|
|
|
|
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)
|
2023-12-25 10:39:49 +00:00
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-12-18 03:12:07 +00:00
|
|
|
|
|
2024-01-03 07:28:16 +00:00
|
|
|
|
// 打开登录
|
|
|
|
|
const goLogin = () => {
|
|
|
|
|
if (typeof ajax_login === "function") ajax_login()
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-25 10:39:49 +00:00
|
|
|
|
export default {
|
|
|
|
|
get,
|
|
|
|
|
post,
|
|
|
|
|
}
|