113 lines
3.8 KiB
JavaScript
113 lines
3.8 KiB
JavaScript
|
|
import axios from "axios";
|
||
|
|
import $url from "./url";
|
||
|
|
import router from "../router";
|
||
|
|
import { encrypt } from "../utils/aes";
|
||
|
|
|
||
|
|
// 添加请求拦截器
|
||
|
|
axios.interceptors.request.use(function (config) {
|
||
|
|
// 在发送请求之前做些什么
|
||
|
|
if (config.headers) {
|
||
|
|
config.headers.uid = localStorage.data;
|
||
|
|
if (localStorage.access_token && localStorage.access_token !== "" && localStorage.access_token !== null && localStorage.access_token !== undefined && localStorage.access_token !== NaN) {
|
||
|
|
config.headers.authorization = localStorage.access_token;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return config;
|
||
|
|
}, function (error) {
|
||
|
|
// 对请求错误做些什么
|
||
|
|
return Promise.reject(error);
|
||
|
|
});
|
||
|
|
|
||
|
|
// 转到登录页面
|
||
|
|
const toLogin = () => {
|
||
|
|
localStorage.clear();
|
||
|
|
const data = { origin_url: window.location.href };
|
||
|
|
axios.post($url.toLogin, data).then((res) => {
|
||
|
|
if (res.data.code === 200) {
|
||
|
|
window.location.href = res.data.redirect;
|
||
|
|
} else {
|
||
|
|
router.push({ path: '/404page' });
|
||
|
|
}
|
||
|
|
});
|
||
|
|
};
|
||
|
|
|
||
|
|
// 添加响应拦截器
|
||
|
|
let result = true;
|
||
|
|
axios.interceptors.response.use(function (response) {
|
||
|
|
// 2xx 范围内的状态码都会触发该函数。
|
||
|
|
// 对响应数据做点什么
|
||
|
|
if (response.data.code === 200) {
|
||
|
|
if ("access_token" in response.data) {
|
||
|
|
localStorage.setItem("access_token", response.data.access_token);
|
||
|
|
}
|
||
|
|
if ("refresh_token" in response.data) {
|
||
|
|
localStorage.setItem("refresh_token", response.data.refresh_token);
|
||
|
|
}
|
||
|
|
if ("data" in response.data) {
|
||
|
|
localStorage.setItem("data", response.data.data);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if(response.data.code === 401 && result) {
|
||
|
|
result = false;
|
||
|
|
setTimeout(() => {
|
||
|
|
result = true;
|
||
|
|
}, 1000);
|
||
|
|
let getCode = window.location.href.indexOf('?');
|
||
|
|
if (getCode != '-1') {
|
||
|
|
let code = window.location.href.substring(getCode + 1);
|
||
|
|
const data = {
|
||
|
|
code: code,
|
||
|
|
type: encrypt('code')
|
||
|
|
};
|
||
|
|
axios.post($url.getToken, data).then((res) => {
|
||
|
|
if (res.data.code === 200) {
|
||
|
|
localStorage.setItem("data", res.data.data);
|
||
|
|
localStorage.setItem("access_token", res.data.access_token);
|
||
|
|
localStorage.setItem("refresh_token", res.data.refresh_token);
|
||
|
|
window.location.href = res.data.redirect;
|
||
|
|
} else {
|
||
|
|
toLogin();
|
||
|
|
}
|
||
|
|
});
|
||
|
|
} else if (localStorage.refresh_token && localStorage.refresh_token !== "" && localStorage.refresh_token !== null && localStorage.refresh_token !== undefined && localStorage.refresh_token !== NaN) {
|
||
|
|
const data = {
|
||
|
|
code: localStorage.refresh_token,
|
||
|
|
type: localStorage.data
|
||
|
|
};
|
||
|
|
axios.post($url.getToken, data).then((res) => {
|
||
|
|
if (res.data.code === 200) {
|
||
|
|
localStorage.setItem("data", res.data.data);
|
||
|
|
localStorage.setItem("access_token", res.data.access_token);
|
||
|
|
localStorage.setItem("refresh_token", res.data.refresh_token);
|
||
|
|
window.location.reload();
|
||
|
|
} else {
|
||
|
|
toLogin();
|
||
|
|
}
|
||
|
|
});
|
||
|
|
} else {
|
||
|
|
toLogin();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return response;
|
||
|
|
}, function (error) {
|
||
|
|
// 超出 2xx 范围的状态码都会触发该函数。
|
||
|
|
// 对响应错误做点什么
|
||
|
|
return Promise.reject(error);
|
||
|
|
});
|
||
|
|
|
||
|
|
export default {
|
||
|
|
get(url, query = {}) {
|
||
|
|
return axios.get(url, {
|
||
|
|
params: query
|
||
|
|
});
|
||
|
|
},
|
||
|
|
post(url, body) {
|
||
|
|
return axios.post(url, body);
|
||
|
|
},
|
||
|
|
put(url, body) {
|
||
|
|
return axios.put(url, body);
|
||
|
|
},
|
||
|
|
del(url) {
|
||
|
|
return axios.delete(url);
|
||
|
|
},
|
||
|
|
}
|