加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
http.js 2.88 KB
一键复制 编辑 原始数据 按行查看 历史
DM 提交于 2021-11-14 11:07 . '第一次提交'
import axios from "axios"
// import qs from "qs"
import NProgress from "nprogress"
import { Message } from "element-ui"
import 'nprogress/nprogress.css'
axios.defaults.baseURL = "/api"
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
})
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 http = {
get(url, params) {
return new Promise((resolve, reject) => {
NProgress.start()
axios
.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)
})
})
},
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.message,
// type:
// status.code === 200 ? "success" : "error",
})
}
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 助手
尝试更多
代码解读
代码找茬
代码优化