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" ? "/" :
|
2022-02-09 16:24:15 +08:00
|
|
|
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)) {
|
|
|
|
|
config.url = config.url.replace(/(app|auth|admin)\//, "api/")
|
|
|
|
|
}
|
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) {
|
|
|
|
|
if (res.data.code) {
|
|
|
|
|
if (res.data.code == 0) {
|
|
|
|
|
return res.data
|
|
|
|
|
} else if (res.data.code == 401) {
|
|
|
|
|
store.commit("logout");
|
2021-11-15 15:53:44 +08:00
|
|
|
uni.navigateTo({url: "/pages/login"})
|
2021-11-15 10:29:05 +08:00
|
|
|
} else {
|
|
|
|
|
console.error(res.data.msg || "请求失败!")
|
|
|
|
|
return Promise.reject(res.data.msg)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return res.data
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
console.error("服务器异常,请联系管理员!")
|
2022-01-20 11:22:22 +08:00
|
|
|
return Promise.reject(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
|