mirror of
https://codeup.aliyun.com/67762337eccfc218f6110e0e/vue/fe-manage.git
synced 2025-12-10 03:16:44 +08:00
71 lines
2.2 KiB
JavaScript
71 lines
2.2 KiB
JavaScript
|
|
import {message} from "ant-design-vue";
|
|
import axios from "axios";
|
|
import router from "@/router";
|
|
import Cookies from 'vue-cookies'
|
|
// import { getCookie } from '../api/method'
|
|
// const Qs = require("qs");
|
|
|
|
// axios.defaults.headers.post["Content-Type"] =
|
|
// "application/x-www-form-urlencoded";
|
|
// export const FILE_UPLOAD_URL = process.env.VUE_APP_BASE_API + '/file/upload'
|
|
// export const BATCH_IMPORT_SCORE = process.env.VUE_APP_BASE_API + '/admin/offcourse/batchImportScore'
|
|
axios.defaults.withCredentials = true;
|
|
const http = axios.create({
|
|
baseURL: '/userbasic',
|
|
timeout: 1000 * 15,
|
|
headers: {"Content-Type": "application/json",},
|
|
});
|
|
|
|
http.interceptors.request.use(
|
|
(config) => {
|
|
const token = Cookies.get("token")
|
|
if (token) {
|
|
config.headers.token = token; //测试1111
|
|
} else{
|
|
message.error('未获取到登录信息,请先登录')
|
|
}
|
|
return config;
|
|
},
|
|
(err) => {
|
|
console.log("登陆前拦截", err);
|
|
return Promise.reject(err);
|
|
}
|
|
);
|
|
|
|
http.interceptors.response.use(
|
|
(response) => {
|
|
// console.log('response', response)
|
|
const {
|
|
data: {code},
|
|
} = response;
|
|
if (code === 0 || code === 200) {
|
|
return response.data?response.data:response;
|
|
}
|
|
if(code==500){
|
|
return message.error('请求失败');
|
|
}
|
|
if(code==601){
|
|
message.error('token过期请重新登陆');
|
|
}
|
|
if (code === 1000) {
|
|
window.location.href = process.env.VUE_APP_LOGIN_URL + encodeURIComponent(window.location.protocol + process.env.VUE_APP_BOE_API_URL + process.env.VUE_APP_BASE + router.currentRoute.value.fullPath)
|
|
return Promise.reject(response);
|
|
}
|
|
// show && message.error(msg);
|
|
// console.log("api %o", msg);
|
|
// return Promise.reject(response);
|
|
return response
|
|
},
|
|
function (error) {
|
|
if (error.message == "timeout of 1ms exceeded") {
|
|
message.destroy();
|
|
message.error("请求超时");
|
|
}
|
|
console.log("api error %o", error);
|
|
return message.error(error.message);
|
|
}
|
|
);
|
|
|
|
export default http;
|