You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
3.6 KiB

4 years ago
import axios from "axios";
import util from "@/libs/util";
import router from "@/router/index";
import Setting from "@/setting";
3 years ago
import store from '@/store'
4 years ago
const service = axios.create({
9 months ago
baseURL: Setting.apiBaseURL,
timeout: 10000000
4 years ago
});
4 years ago
// post请求头
4 years ago
service.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
4 years ago
// 请求拦截器
service.interceptors.request.use(config => {
9 months ago
let token = util.local.get(Setting.tokenKey);
if (token) config.headers.token = token;
return config;
4 years ago
}, err => {
9 months ago
util.errorMsg({
message: "退出登陆",
onClose: function () {
store.dispatch('user/logout')
}
});
return Promise.reject(err);
4 years ago
});
4 years ago
11 months ago
let logouted = 0;
4 years ago
// 响应拦截器
service.interceptors.response.use(
9 months ago
response => {
const res = response.data;
if (res.status == 200 || res.status == 10000 || res.status == 30001) {
return Promise.resolve(res).catch(e => { });
} else if (!res.status) {
return Promise.resolve(res).catch(e => { });
} else {
util.errorMsg(res.message);
return Promise.reject(res)
// return Promise.resolve(res).catch(e => {});
}
},
// 服务器状态码不是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;
// 403 token过期
// 登录过期对用户进行提示
// 清除本地token和清空vuex中token对象
// 跳转登录页面
case 403:
util.local.remove(Setting.storeKey);
util.local.remove(Setting.tokenKey);
util.errorMsg("登录过期,请重新登录");
// 清除token
// store.commit('loginSuccess', null);
// 跳转登录页面,并将要浏览的页面fullPath传过去,登录成功后跳转需要访问的页面
setTimeout(() => {
store.dispatch('user/logout')
}, 1000);
break;
// 404请求不存在
case 404:
util.errorMsg("网络请求不存在!");
break;
// 其他错误,直接抛出错误提示
default:
util.errorMsg(error.response.data.message);
Promise.reject(res);
}
return Promise.reject(error.response);
4 years ago
}
9 months ago
}
4 years ago
);
9 months ago
function get (url, params) {
return new Promise((resolve, reject) => {
service.get(url, { params: params }).then(res => {
resolve(res);
}).catch(err => {
reject(err);
4 years ago
});
9 months ago
});
4 years ago
}
9 months ago
function post (url, params) {
return new Promise((resolve, reject) => {
service.post(url, params).then(res => {
resolve(res);
}).catch(err => {
reject(err.data);
4 years ago
});
9 months ago
});
4 years ago
}
9 months ago
function del (url, params) {
return new Promise((resolve, reject) => {
service.delete(url, {
params
}).then(res => {
resolve(res);
}).catch(err => {
reject(err.data);
4 years ago
});
9 months ago
});
4 years ago
}
9 months ago
function put (url, params) {
return new Promise((resolve, reject) => {
service.put(url, params).then(res => {
resolve(res);
}).catch(err => {
reject(err.data);
4 years ago
});
9 months ago
});
4 years ago
}
4 years ago
export { get, post, del, put };