老产品前端代码
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.

104 lines
2.4 KiB

import axios from "axios";
import Cookies from "js-cookie";
import router from "@/router";
import qs from "qs";
import { clearLoginInfo } from "@/utils";
import isPlainObject from "lodash/isPlainObject";
4 years ago
const http = axios.create({
baseURL: window.SITE_CONFIG["apiURL"],
4 years ago
timeout: 1000 * 900,
withCredentials: true,
});
4 years ago
/**
* 请求拦截
*/
http.interceptors.request.use(
(config) => {
config.headers["Accept-Language"] =
localStorage.getItem("language") || "zh-CN";
config.headers["token"] = localStorage.getItem("token") || "";
config.headers["Authorization"] = localStorage.getItem("token") || "";
// 默认参数
var defaults = {};
// 防止缓存,GET请求默认带_t参数
if (config.method == "get") {
config.params = {
...config.params,
...{ _t: new Date().getTime() },
};
4 years ago
}
if (isPlainObject(config.params)) {
config.params = {
...defaults,
...config.params,
};
4 years ago
}
if (isPlainObject(config.data)) {
config.data = {
...defaults,
...config.data,
};
if (
/^application\/x-www-form-urlencoded/.test(
config.headers["content-type"]
)
) {
config.data = qs.stringify(config.data);
}
4 years ago
}
return config;
},
(error) => {
return Promise.reject(error);
4 years ago
}
);
4 years ago
/**
* 响应拦截
*/
http.interceptors.response.use(
(response) => {
const code = [10007, 401, 10001, 10005];
if (code.includes(response.data.code)) {
clearLoginInfo();
// localStorage.setItem('userType', 'work')
// let userType = localStorage.getItem("userType");
// if (userType === 'work'){
// router.replace({ name: 'loginWork' })
// }else {
// router.replace({ name: 'login' })
// }
router.replace({ name: "login" });
return Promise.reject(response.data.msg);
}
return response;
},
(error) => {
console.error(error);
return Promise.reject(error);
4 years ago
}
);
// http.put = function (url, data, config) {
// http.post(url, data, {
// ...config,
// headers: {
// "X-HTTP-Method-Override": "PUT",
// },
// });
// };
// http.delete = function (url, config) {
// http.post(url, config.data || {}, {
// ...config,
// headers: {
// "X-HTTP-Method-Override": "DELETE",
// },
// });
// };
4 years ago
export default http;