69 lines
1.9 KiB
JavaScript
69 lines
1.9 KiB
JavaScript
import axios from 'axios'
|
|
import store from '../store'
|
|
|
|
const baseURL = process.env.NODE_ENV === "production" ? "/" :
|
|
sessionStorage.getItem("prj") == "saas" ? "/online" : "/lan"
|
|
let instance = axios.create({
|
|
baseURL,
|
|
timeout: 600000,
|
|
withCredentials: true,
|
|
})
|
|
instance.interceptors.request.use(config => {
|
|
store.commit('initWaterMarker')
|
|
console.log(config)
|
|
if (/\/node\//.test(config.url)) {
|
|
config.baseURL = '/ns'
|
|
} else if (/AppCountryAlbum/.test(location.pathname) || config.module == 'AppCountryAlbum') {
|
|
config.baseURL = '/aca'
|
|
config.url = config.url.replace(/(app|auth|admin)\//, "api/")
|
|
} else if (/\/grid\//.test(location.pathname)) {
|
|
config.baseURL = '/wangge'
|
|
} else if (/\/project\/police\//.test(location.pathname)) {
|
|
config.baseURL = '/hnjc'
|
|
config.url = config.url.replace(/(app|auth|admin)\//, "api/")
|
|
} else if (sessionStorage.getItem("prj") == "saas") {
|
|
config.baseURL = '/online'
|
|
}
|
|
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) {
|
|
uni.hideLoading()
|
|
if (res.data.access_token) {
|
|
return res.data
|
|
}
|
|
if (res.data.code == 0) {
|
|
return res.data
|
|
} else if (res.data.code === 1) {
|
|
uni.showToast({
|
|
title: res.data.msg,
|
|
duration: 2000,
|
|
icon: 'none'
|
|
})
|
|
|
|
return res.data
|
|
} else if (res.data.code == 401) {
|
|
store.commit("logout");
|
|
uni.showToast({title: "请登录用户!", icon: "none"})
|
|
} else {
|
|
console.error(res.data.msg || "请求失败!")
|
|
return Promise.reject(res.data.msg)
|
|
}
|
|
} else {
|
|
console.error("服务器异常,请联系管理员!")
|
|
return res.data
|
|
}
|
|
}, err => {
|
|
uni.hideLoading()
|
|
console.error(err)
|
|
return Promise.reject(error)
|
|
})
|
|
|
|
export default instance
|