2022-04-10 01:21:21 +08:00
|
|
|
|
/**
|
|
|
|
|
* 响应拦截
|
|
|
|
|
* @param {Object} http
|
|
|
|
|
*/
|
2022-04-20 03:08:59 +08:00
|
|
|
|
module.exports = vm => {
|
|
|
|
|
uni.$u.http.interceptors.response.use(
|
|
|
|
|
res => {
|
|
|
|
|
//对响应成功做点什么 可使用async await 做异步操作
|
|
|
|
|
//可以根据业务情况做相应的处理
|
2022-04-21 01:46:45 +08:00
|
|
|
|
if (res.data.code === 0) {
|
|
|
|
|
return res.data
|
2022-05-01 22:38:00 +08:00
|
|
|
|
} else if(res.data.code === 401) {
|
|
|
|
|
//用户未登录或登录token已过期
|
2022-05-02 17:58:48 +08:00
|
|
|
|
vm.$store.commit('CLEAR_LOGIN_INFO')
|
2022-04-21 01:46:45 +08:00
|
|
|
|
} else {
|
|
|
|
|
console.log(res)
|
2022-05-01 22:38:00 +08:00
|
|
|
|
//其他错误信息统一处理
|
2022-04-21 01:46:45 +08:00
|
|
|
|
uni.$u.toast(res.data.msg)
|
|
|
|
|
return Promise.reject(res)
|
|
|
|
|
}
|
2022-04-20 03:08:59 +08:00
|
|
|
|
},
|
|
|
|
|
err => {
|
|
|
|
|
//对响应错误做点什么 (statusCode !== 200)
|
2022-04-21 01:46:45 +08:00
|
|
|
|
console.log(err)
|
|
|
|
|
uni.$u.toast('响应错误' + err.statusCode)
|
2022-04-20 03:08:59 +08:00
|
|
|
|
return Promise.reject(err)
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|