Files
dvcp_v2_wxcp_app/src/common/axios.js

69 lines
1.8 KiB
JavaScript
Raw Normal View History

2021-11-15 10:29:05 +08:00
import axios from 'axios'
import store from '../store'
2022-02-09 10:35:29 +08:00
const baseURL = process.env.NODE_ENV === "production" ? "/" :
sessionStorage.getItem("prj") == "saas" ? "/online" : "/lan"
2021-11-15 10:29:05 +08:00
let instance = axios.create({
2022-02-09 10:35:29 +08:00
baseURL,
2021-11-15 10:29:05 +08:00
timeout: 600000,
withCredentials: true,
})
instance.interceptors.request.use(config => {
store.commit('initWaterMarker')
2022-05-18 14:16:04 +08:00
if (/AppCountryAlbum/.test(location.pathname)) {
2022-05-30 15:38:40 +08:00
config.baseURL = '/aca'
2022-05-18 14:16:04 +08:00
config.url = config.url.replace(/(app|auth|admin)\//, "api/")
2022-05-24 11:14:29 +08:00
}
2022-05-27 11:47:33 +08:00
if (/\/grid\//.test(location.pathname)) {
2022-05-30 15:31:51 +08:00
config.baseURL = '/wangge'
}
if (/\/project\/police\//.test(location.pathname)) {
config.baseURL = '/hnjc'
2022-05-30 15:38:40 +08:00
config.url = config.url.replace(/(app|auth|admin)\//, "api/")
2022-05-27 11:47:33 +08:00
}
2022-05-24 11:14:29 +08:00
if (sessionStorage.getItem("prj") == "saas") {
config.url = config.url.replace(/(app|auth|admin)\//, "api/")
2022-05-18 18:06:39 +08:00
}
2021-11-15 10:29:05 +08:00
if (!config.withoutToken && store.state.token) {
config.headers["Authorization"] = store.state.token
}
return config
}, err => {
console.error(err)
})
instance.interceptors.response.use(res => {
if (res.data) {
2022-05-30 11:02:56 +08:00
uni.hideLoading()
2022-05-30 14:12:46 +08:00
if (res.data.access_token) {
return res.data
}
2022-05-30 11:02:56 +08:00
if (res.data.code == 0) {
return res.data
} else if (res.data.code === 1) {
uni.showToast({
title: res.data.msg,
duration: 2000,
icon: 'none'
})
2022-05-19 14:43:42 +08:00
2021-11-15 10:29:05 +08:00
return res.data
2022-05-30 11:02:56 +08:00
} else if (res.data.code == 401) {
store.commit("logout");
uni.navigateTo({url: "/pages/login"})
} else {
console.error(res.data.msg || "请求失败!")
return Promise.reject(res.data.msg)
2021-11-15 10:29:05 +08:00
}
} else {
console.error("服务器异常,请联系管理员!")
2022-05-30 14:12:46 +08:00
return res.data
2021-11-15 10:29:05 +08:00
}
}, err => {
2022-01-20 11:22:22 +08:00
uni.hideLoading()
2021-11-15 10:29:05 +08:00
console.error(err)
2022-01-20 11:22:22 +08:00
return Promise.reject(error)
2021-11-15 10:29:05 +08:00
})
export default instance