|
@@ -1,17 +1,23 @@
|
|
import Vue from "vue";
|
|
import Vue from "vue";
|
|
import Axios from "axios";
|
|
import Axios from "axios";
|
|
|
|
+import router from "../router";
|
|
//axios配置 start
|
|
//axios配置 start
|
|
|
|
|
|
//请求拦截
|
|
//请求拦截
|
|
let $Message;
|
|
let $Message;
|
|
Axios.interceptors.request.use(
|
|
Axios.interceptors.request.use(
|
|
config => {
|
|
config => {
|
|
- // 在发送请求之前做某件事
|
|
|
|
-
|
|
|
|
- // 若是有做鉴权token , 就给头部带上token
|
|
|
|
- // if (localStorage.token) {
|
|
|
|
- // config.headers.Authorization = localStorage.token;
|
|
|
|
|
|
+ // if (window.localStorage.getItem("token")) {
|
|
|
|
+ // Axios.defaults.headers.common["token"] = response.data.token;
|
|
|
|
+ // Axios.defaults.headers.common["key"] = response.data.key;
|
|
|
|
+ // } else {
|
|
|
|
+ // if (config.url != "/api/ecs_core/auth/login") {
|
|
|
|
+ // router.push({
|
|
|
|
+ // name: 'login'
|
|
|
|
+ // })
|
|
|
|
+ // }
|
|
// }
|
|
// }
|
|
|
|
+
|
|
$Message = config.message;
|
|
$Message = config.message;
|
|
return config;
|
|
return config;
|
|
},
|
|
},
|
|
@@ -34,8 +40,7 @@ Axios.interceptors.response.use(
|
|
// 这里是返回状态码不为200时候的错误处理
|
|
// 这里是返回状态码不为200时候的错误处理
|
|
let status = error.response.status;
|
|
let status = error.response.status;
|
|
// 登录失效 跳转登录页面
|
|
// 登录失效 跳转登录页面
|
|
- if (status == 403 || status == 401) {
|
|
|
|
- }
|
|
|
|
|
|
+ if (status == 403 || status == 401) {}
|
|
return Promise.resolve(error);
|
|
return Promise.resolve(error);
|
|
}
|
|
}
|
|
);
|
|
);
|
|
@@ -54,25 +59,23 @@ let ajax = (url, params, type = "json", method = "get", thisObj) => {
|
|
let getData = method === "get" ? params : {};
|
|
let getData = method === "get" ? params : {};
|
|
let postData = method === "post" ? params : {};
|
|
let postData = method === "post" ? params : {};
|
|
let headers =
|
|
let headers =
|
|
- type == "form"
|
|
|
|
- ? {
|
|
|
|
- "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
|
|
|
|
- }
|
|
|
|
- : {
|
|
|
|
- Accept: "application/json",
|
|
|
|
- "Content-Type": "application/json; charset=UTF-8"
|
|
|
|
- };
|
|
|
|
|
|
+ type == "form" ? {
|
|
|
|
+ "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
|
|
|
|
+ } : {
|
|
|
|
+ Accept: "application/json",
|
|
|
|
+ "Content-Type": "application/json; charset=UTF-8"
|
|
|
|
+ };
|
|
|
|
|
|
- return new Promise(function(resolve, reject) {
|
|
|
|
|
|
+ return new Promise(function (resolve, reject) {
|
|
Axios({
|
|
Axios({
|
|
- method: method,
|
|
|
|
- url: url,
|
|
|
|
- params: getData,
|
|
|
|
- data: postData,
|
|
|
|
- headers: headers,
|
|
|
|
- withCredentials: true,
|
|
|
|
- message: thisObj.$Message
|
|
|
|
- })
|
|
|
|
|
|
+ method: method,
|
|
|
|
+ url: url,
|
|
|
|
+ params: getData,
|
|
|
|
+ data: postData,
|
|
|
|
+ headers: headers,
|
|
|
|
+ withCredentials: true,
|
|
|
|
+ message: thisObj.$Message
|
|
|
|
+ })
|
|
.then(res => {
|
|
.then(res => {
|
|
let status = res.status;
|
|
let status = res.status;
|
|
if (status >= 200 && status < 300) {
|
|
if (status >= 200 && status < 300) {
|
|
@@ -98,19 +101,19 @@ let ajax = (url, params, type = "json", method = "get", thisObj) => {
|
|
Vue.prototype.$http = Axios;
|
|
Vue.prototype.$http = Axios;
|
|
|
|
|
|
// get请求
|
|
// get请求
|
|
-Vue.prototype.$get = function(url, params) {
|
|
|
|
|
|
+Vue.prototype.$get = function (url, params) {
|
|
return ajax(url, params, "", "get", this);
|
|
return ajax(url, params, "", "get", this);
|
|
};
|
|
};
|
|
|
|
|
|
// post请求
|
|
// post请求
|
|
-Vue.prototype.$post = function(url, params, type) {
|
|
|
|
|
|
+Vue.prototype.$post = function (url, params, type) {
|
|
return ajax(url, params, type, "post", this);
|
|
return ajax(url, params, type, "post", this);
|
|
};
|
|
};
|
|
|
|
|
|
export default {
|
|
export default {
|
|
- install: function(Vue, Option) {
|
|
|
|
|
|
+ install: function (Vue, Option) {
|
|
Object.defineProperty(Vue.prototype, "$http", {
|
|
Object.defineProperty(Vue.prototype, "$http", {
|
|
value: Axios
|
|
value: Axios
|
|
});
|
|
});
|
|
}
|
|
}
|
|
-};
|
|
|
|
|
|
+};
|