加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
http.js 6.20 KB
一键复制 编辑 原始数据 按行查看 历史
DM 提交于 2021-11-14 10:52 . add files
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;
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化