|
|
|
import axios from "axios";
|
|
|
|
import Util from "@/libs/util";
|
|
|
|
import router from "@/router/index";
|
|
|
|
import Setting from "@/setting";
|
|
|
|
import store from '@/store'
|
|
|
|
|
|
|
|
const service = axios.create({
|
|
|
|
baseURL: Setting.apiBaseURL,
|
|
|
|
timeout: 10000000
|
|
|
|
});
|
|
|
|
// post请求头
|
|
|
|
service.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
|
|
|
|
|
|
|
|
// 请求拦截器
|
|
|
|
service.interceptors.request.use(config => {
|
|
|
|
let token = Util.session.get(Setting.tokenKey);
|
|
|
|
if (token) config.headers.token = token
|
|
|
|
return config;
|
|
|
|
}, err => {
|
|
|
|
Util.errorMsg({
|
|
|
|
message: "退出登陆",
|
|
|
|
onClose: function () {
|
|
|
|
store.dispatch('user/logout')
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return Promise.reject(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
let logouted = 0;
|
|
|
|
// 响应拦截器
|
|
|
|
service.interceptors.response.use(
|
|
|
|
response => {
|
|
|
|
const res = response.data;
|
|
|
|
if (res.status == 200 || res.status == 10000 || res.status == 30001) {
|
|
|
|
return Promise.resolve(res).catch(e => { });
|
|
|
|
} else if (res.code === 401) {
|
|
|
|
// 账号互踢
|
|
|
|
if (!logouted) {
|
|
|
|
Util.local.remove(Setting.storeKey)
|
|
|
|
Util.local.remove(Setting.tokenKey)
|
|
|
|
Util.errorMsg(res.msg.includes('顶') ? '您的账号已在其他设备登录,您已被迫下线!' : '登录过期,请重新登录!')
|
|
|
|
setTimeout(() => {
|
|
|
|
store.dispatch('user/logout')
|
|
|
|
logouted = 0
|
|
|
|
}, 2000)
|
|
|
|
logouted = 1
|
|
|
|
}
|
|
|
|
} else if (!res.status) {
|
|
|
|
return Promise.resolve(res).catch(e => { });
|
|
|
|
} else {
|
|
|
|
res.message && Util.errorMsg(res.message)
|
|
|
|
return Promise.reject(res)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// 服务器状态码不是200的情况
|
|
|
|
error => {
|
|
|
|
if (error.response.status) {
|
|
|
|
switch (error.response.status) {
|
|
|
|
// 401: 未登录
|
|
|
|
// 未登录则跳转登录页面,并携带当前页面的路径
|
|
|
|
// 在登录成功后返回当前页面,这一步需要在登录页操作。
|
|
|
|
case 401:
|
|
|
|
if (!logouted) {
|
|
|
|
Util.local.remove(Setting.storeKey);
|
|
|
|
Util.local.remove(Setting.tokenKey);
|
|
|
|
Util.errorMsg("登录过期,请重新登录");
|
|
|
|
setTimeout(() => {
|
|
|
|
store.dispatch('user/logout')
|
|
|
|
}, 1000);
|
|
|
|
logouted = 1
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 500:
|
|
|
|
Util.errorMsg("网络错误");
|
|
|
|
break;
|
|
|
|
// 404请求不存在
|
|
|
|
case 404:
|
|
|
|
Util.errorMsg("网络请求不存在!");
|
|
|
|
break;
|
|
|
|
// 其他错误,直接抛出错误提示
|
|
|
|
default:
|
|
|
|
Util.errorMsg(error.response.data.message);
|
|
|
|
Promise.reject(res);
|
|
|
|
}
|
|
|
|
return Promise.reject(error.response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
function get (url, params) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
service.get(url, { params: params }).then(res => {
|
|
|
|
resolve(res);
|
|
|
|
}).catch(err => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function post (url, params) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
service.post(url, params).then(res => {
|
|
|
|
resolve(res);
|
|
|
|
}).catch(err => {
|
|
|
|
reject(err.data);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function del (url, params) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
service.delete(url, {
|
|
|
|
params
|
|
|
|
}).then(res => {
|
|
|
|
resolve(res);
|
|
|
|
}).catch(err => {
|
|
|
|
reject(err.data);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function put (url, params) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
service.put(url, params).then(res => {
|
|
|
|
resolve(res);
|
|
|
|
}).catch(err => {
|
|
|
|
reject(err.data);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export { get, post, del, put };
|