代码拉取完成,页面将自动刷新
import axios from "axios";
import qs from "qs";
import NProgress from "nprogress";
axios.defaults.baseURL = "http://bookapi.jiaynet.cn:8084/";
axios.defaults.timeout = 10000;
axios.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8;";
axios.interceptors.request.use(
(config) => {
var token = window.sessionStorage.getItem("Authorization");
if (token) {
config.headers.Authorization = token;
}
return config;
},
(error) => {
return Promise.error(error);
}
);
axios.interceptors.response.use((res) => {
if (res.data.code === 3001) {
Message({
message: "权限不足",
type: "error",
});
}
return res;
});
//uploadAsome
var uploadAsome = axios.create({
baseURL: "http://bookapi.jiaynet.cn:8084/",
timeout: 10000,
headers: { "Content-Type": "application/x-www-form-urlencoded" },
});
uploadAsome.interceptors.request.use(
(config) => {
var token = window.sessionStorage.getItem("Authorization");
if (token) {
config.headers.Authorization = token;
}
return config;
},
(error) => {
return Promise.error(error);
}
);
var uploadAsome1 = axios.create({
baseURL: "http://bookapi.jiaynet.cn:8084/",
timeout: 10000,
headers: { "Content-Type": "text/xml;charset=UTF-8/xml" },
});
uploadAsome1.interceptors.request.use(
(config) => {
var token = window.sessionStorage.getItem("Authorization");
if (token) {
config.headers.Authorization = token;
}
return config;
},
(error) => {
return Promise.error(error);
}
);
//uploadRequest
var uploadRequest = axios.create({
baseURL: "/api",
timeout: 10000,
headers: { "Content-Type": "multipart/form-data" },
});
uploadRequest.interceptors.request.use(
(config) => {
var token = window.sessionStorage.getItem("Authorization");
if (token) {
config.headers.Authorization = token;
}
return config;
},
(error) => {
return Promise.error(error);
}
);
var uploadRequest1 = axios.create({
baseURL: "/api",
timeout: 10000,
headers: { "Content-Type": "application/x-www-form-urlencoded" },
});
uploadRequest1.interceptors.request.use(
(config) => {
var token = window.sessionStorage.getItem("Authorization");
if (token) {
config.headers.Authorization = token;
}
return config;
},
(error) => {
return Promise.error(error);
}
);
var uploadRequest2 = axios.create({
baseURL: "/api",
timeout: 10000,
headers: { "Content-Type": "application/json" },
});
uploadRequest2.interceptors.request.use(
(config) => {
var token = window.sessionStorage.getItem("Authorization");
if (token) {
config.headers.Authorization = token;
}
return config;
},
(error) => {
return Promise.error(error);
}
);
//
var http = {
get(url, params) {
return new Promise((resolve, reject) => {
NProgress.start();
axios
.get(url, {
params: params,
})
.then((res) => {
NProgress.done();
// if (res.status === 200 && url === "/login") {
// window.sessionStorage.setItem("Authorization", res.headers.token);
// }
resolve(res.data);
})
.catch((err) => {
NProgress.done();
reject(err.data);
});
});
},
post(url, params, message = false) {
return new Promise((resolve, reject) => {
NProgress.start();
axios
.post(url, JSON.stringify(params))
.then((res) => {
NProgress.done();
if (message) {
Message({
message: res.data.message,
type: res.data.code === 200 ? "success" : "error",
});
}
resolve(res.data);
})
.catch((err) => {
NProgress.done();
reject(err.data);
});
});
},
asomeGet(url, params) {
return new Promise((resolve, reject) => {
NProgress.start();
uploadAsome
.get(url, {
params: params,
})
.then((res) => {
console;
NProgress.done();
if (res.status === 200 && url === "/login") {
window.sessionStorage.setItem("Authorization", res.headers.token);
}
resolve(res.data);
})
.catch((err) => {
NProgress.done();
reject(err.data);
});
});
},
asomePost(url, params, message = false) {
return new Promise((resolve, reject) => {
NProgress.start();
uploadAsome
.post(url, qs.stringify(params))
.then((res) => {
NProgress.done();
if (message) {
Message({
message: res.data.message,
type: res.data.code === 200 ? "success" : "error",
});
}
resolve(res.data);
})
.catch((err) => {
NProgress.done();
reject(err.data);
});
});
},
formPost(url, file) {
return new Promise((resolve, reject) => {
NProgress.start();
uploadRequest1
.post(url, file)
.then((res) => {
NProgress.done();
1;
resolve(res.data);
})
.catch((err) => {
NProgress.done();
reject(err.data);
});
});
},
Post(url, file) {
return new Promise((resolve, reject) => {
NProgress.start();
uploadRequest2
.post(url, file)
.then((res) => {
NProgress.done();
1;
resolve(res.data);
})
.catch((err) => {
NProgress.done();
reject(err.data);
});
});
},
upload(url, file) {
return new Promise((resolve, reject) => {
NProgress.start();
uploadRequest
.post(url, file)
.then((res) => {
NProgress.done();
resolve(res.data);
})
.catch((err) => {
NProgress.done();
reject(err.data);
});
});
},
download(url) {
let iframe = document.createElement("iframe");
iframe.style.display = "none";
iframe.src = url;
iframe.onload = function() {
document.body.removeChild(iframe);
};
document.body.appendChild(iframe);
},
install(Vue) {
Vue.prototype.http = this;
delete this.install;
},
};
export default http;
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。