main.js中
require('./util/request/index')(app)
index.js中
module.exports = (vm) => {
require('./requestInterceptors')(vm)
require('./responseInterceptors')(vm)
}
requestInterceptors.js中
module.exports = (vm) => {
uni.$u.http.interceptors.request.use((config) => {
config.data = config.data || {}
return config
}, (config) =>
Promise.reject(config))
}
responseInterceptors.js中
module.exports = (vm) => {
uni.$u.http.interceptors.response.use((response) => {
const data = response
const custom = response.config?.custom
if (data.data.code != 200) {
if(data.data.id == 'tron' || data.config.url == '/slider/'){
return data || {}
}
if(data.data.code == 1005){
uni.$u.toast(data.data.message)
setTimeout(() => {
uni.navigateTo({
url: '/pages/login/index'
})
},1500)
}else {
uni.$u.toast(data.data.message || '服务器错误')
}
if (custom?.catch) {
return Promise.reject(data)
} else {
return new Promise(() => { })
}
}
return data || {}
}, (response) => {
return Promise.reject(response)
})
}
|