mirror of
https://codeup.aliyun.com/67762337eccfc218f6110e0e/vue/learning-system-portal.git
synced 2025-12-12 04:16:45 +08:00
212 lines
6.1 KiB
JavaScript
212 lines
6.1 KiB
JavaScript
import axios from 'axios'
|
||
import qs from 'qs'
|
||
import { Notification, MessageBox, Message } from 'element-ui'
|
||
import store from '@/store'
|
||
import { getToken } from '@/utils/token'
|
||
import errorCode from '@/utils/errorCode'
|
||
/**
|
||
*request请求 axios.request(config)
|
||
*requestJson请求 axios.request(config)
|
||
*get请求 axios.get(url[, config])
|
||
*post请求 axios.post(url[, data[, config]])
|
||
*postJson请求 axios.post(url[, data[, config]])
|
||
*put请求 axios.put(url[, data[, config]])
|
||
*putJson请求 axios.put(url[, data[, config]])
|
||
*patch请求 axios.patch(url[, data[, config]])
|
||
*patchJson请求 axios.patch(url[, data[, config]])
|
||
*delete请求 axios.delete(url[, config])
|
||
*/
|
||
|
||
const ReLoginUrl = "/login";
|
||
const TokenName = 'XBOE-Access-Token';
|
||
/**axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded'**/
|
||
//只是用于发送json对象数据时使用post,put,patch
|
||
const jsonRequest = axios.create({
|
||
headers: { 'Content-Type': 'application/json;charset=utf-8' },
|
||
// axios中请求配置有baseURL选项,表示请求URL公共部分
|
||
baseURL: process.env.VUE_APP_STAT_BASE_API,
|
||
//超时
|
||
timeout: 10000,
|
||
});
|
||
//发送json对象的拦截器
|
||
jsonRequest.interceptors.request.use(config => {
|
||
//是否需要设置 token
|
||
const isToken = (config.headers || {}).isToken === false
|
||
if (getToken() && !isToken) {
|
||
config.headers[TokenName] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
|
||
}
|
||
return config
|
||
}, error => {
|
||
console.log(error)
|
||
Promise.reject(error)
|
||
})
|
||
|
||
// 响应拦截器
|
||
jsonRequest.interceptors.response.use(res => {
|
||
|
||
const code = res.data.status || 200;
|
||
if (code === 200) {
|
||
return res.data
|
||
} else {
|
||
if (code === 401) {
|
||
store.dispatch('LogOut').then(() => {
|
||
if (top !== window) { // 判断当前是否在iframe内
|
||
top.location.href = this.webBaseUrl + ReLoginUrl;
|
||
} else {
|
||
window.location.href = this.webBaseUrl + ReLoginUrl;
|
||
}
|
||
// location.href = this.webBaseUrl + ReLoginUrl;
|
||
})
|
||
} else if (code === 403) {
|
||
var msg = '当前操作没有权限';
|
||
Message({ message: msg, type: 'error' });
|
||
return Promise.reject(new Error(msg))
|
||
} else {
|
||
//Message({message: res.data.message, type: 'error'});
|
||
//console.log('err:' + res.data.error);
|
||
return res.data;
|
||
//return Promise.reject(new Error(res.data.message))
|
||
}
|
||
}
|
||
},
|
||
error => {
|
||
console.log('err' + error)
|
||
let { message } = error;
|
||
if (message == "Network Error") {
|
||
message = "网络异常,请稍后重试";
|
||
}
|
||
else if (message.includes("timeout")) {
|
||
message = "系统接口请求超时";
|
||
}
|
||
else if (message.includes("Request failed with status code")) {
|
||
message = "系统接口" + message.substr(message.length - 3) + "异常";
|
||
}
|
||
Message({
|
||
message: message,
|
||
type: 'error',
|
||
duration: 5 * 1000
|
||
})
|
||
return Promise.reject(error)
|
||
}
|
||
)
|
||
|
||
//用于普通的发送请求
|
||
const formRequest = axios.create({
|
||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
||
// axios中请求配置有baseURL选项,表示请求URL公共部分
|
||
baseURL: process.env.VUE_APP_STAT_BASE_API,
|
||
//超时
|
||
timeout: 10000,
|
||
})
|
||
//发送json对象的拦截器
|
||
formRequest.interceptors.request.use(config => {
|
||
//是否需要设置 token
|
||
const isToken = (config.headers || {}).isToken === false
|
||
if (getToken() && !isToken) {
|
||
config.headers[TokenName] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
|
||
}
|
||
return config
|
||
}, error => {
|
||
console.log(error)
|
||
Promise.reject(error)
|
||
});
|
||
formRequest.interceptors.response.use(res => {
|
||
const code = res.data.status || 200;
|
||
if (code === 200) {
|
||
return res.data
|
||
} else {
|
||
if (code === 401) {
|
||
store.dispatch('LogOut').then(() => {
|
||
if (top !== window) { // 判断当前是否在iframe内
|
||
top.location.href = this.webBaseUrl + ReLoginUrl;
|
||
} else {
|
||
window.location.href = this.webBaseUrl + ReLoginUrl;
|
||
}
|
||
// location.href = this.webBaseUrl + ReLoginUrl;
|
||
})
|
||
} else if (code === 403) {
|
||
var msg = '当前操作没有权限';
|
||
Message({ message: msg, type: 'error' });
|
||
return Promise.reject(new Error(msg))
|
||
} else {
|
||
//Message({message: res.data.message, type: 'error'});
|
||
//console.log('err' + res.data.error);
|
||
return res.data
|
||
}
|
||
}
|
||
},
|
||
error => {
|
||
console.log('err' + error)
|
||
let { message } = error;
|
||
if (message == "Network Error") {
|
||
message = "网络异常,请稍后重试";
|
||
}
|
||
else if (message.includes("timeout")) {
|
||
message = "系统接口请求超时";
|
||
}
|
||
else if (message.includes("Request failed with status code")) {
|
||
message = "系统接口" + message.substr(message.length - 3) + "异常";
|
||
}
|
||
Message({
|
||
message: message,
|
||
type: 'error',
|
||
duration: 5 * 1000
|
||
})
|
||
return Promise.reject(error)
|
||
}
|
||
)
|
||
|
||
//request请求
|
||
const request = function (cfg) {
|
||
if (cfg.data) {
|
||
cfg.data = qs.stringify(cfg.data);
|
||
}
|
||
};
|
||
//requestJson请求
|
||
const requestJson = jsonRequest.request;
|
||
//get请求
|
||
const get = formRequest.request;
|
||
//post请求
|
||
const post = function (url, data, config) {
|
||
if (data) {
|
||
data = qs.stringify(data);
|
||
}
|
||
return formRequest.post(url, data, config);
|
||
}
|
||
//postJson请求
|
||
const postJson = jsonRequest.post;
|
||
//put请求
|
||
const put = function (url, data, config) {
|
||
if (data) {
|
||
data = qs.stringify(data);
|
||
}
|
||
return formRequest.put(url, data, config);
|
||
}
|
||
//putJson请求
|
||
const putJson = jsonRequest.put;
|
||
//patch请求
|
||
const patch = function (url, data, config) {
|
||
if (data) {
|
||
data = qs.stringify(data);
|
||
}
|
||
return formRequest.patch(url, data, config);
|
||
}
|
||
//patchJson请求
|
||
const patchJson = jsonRequest.patch;
|
||
//delete请求
|
||
const del = formRequest.delete;
|
||
|
||
|
||
export default {
|
||
request,
|
||
requestJson,
|
||
get,
|
||
post,
|
||
postJson,
|
||
put,
|
||
putJson,
|
||
patch,
|
||
patchJson,
|
||
del
|
||
}
|