| | |
| | | */ |
| | | module.exports = (vm) => {
|
| | | uni.$u.http.interceptors.response.use((response) => {
|
| | | /* 对响应成功做点什么 可使用async await 做异步操作*/
|
| | | uni.hideLoading();
|
| | | const data = response.data
|
| | | // 自定义参数
|
| | | // const custom = response.config?.custom
|
| | | // if (data.code !== 200) { // 服务端返回的状态码不等于200,则reject() |
| | | // uni.$u.toast(data.message) |
| | | // if (data.code === 401) { |
| | | // // console.log('未登录、登录失效'); |
| | | // // 未登录、登录失效 |
| | | // uni.reLaunch({ |
| | | // url: '/pages/login/login' |
| | | // }) |
| | | // } |
| | | // return Promise.reject(data)
|
| | | // } |
| | | // console.log(data.data);
|
| | | return data.data || {}
|
| | | if (response.data.code === 401) { // 处理登录过期
|
| | | uni.showToast({ title: '登录过期,准备自动重新登录', icon: 'none', duration: 2000, mask: true });
|
| | | setTimeout(() => {
|
| | | uni.reLaunch({ url: '/pages/login/login' });
|
| | | }, 2000)
|
| | | return
|
| | | }
|
| | | if (response.data.code !== 200) { // 请求报错
|
| | | uni.showToast({ title: response.data.message, icon: 'none', duration: 2000 });
|
| | | }
|
| | | return data || {}
|
| | | }, (response) => {
|
| | | /* 对响应错误做点什么 (statusCode !== 200)*/ |
| | | uni.hideLoading();
|
| | | let code;
|
| | | if (error.code === "ECONNABORTED") {
|
| | | code = 999
|
| | | } else {
|
| | | code = error.response.status
|
| | | }
|
| | | switch (code) {
|
| | | case 404:
|
| | | uni.showToast({ title: '请求资源不存在', icon: 'none', duration: 2000 });
|
| | | break;
|
| | | case 500:
|
| | | uni.showToast({ title: '服务器资源错误', icon: 'none', duration: 2000 });
|
| | | break;
|
| | | case 999:
|
| | | uni.showToast({ title: '请求超时', icon: 'none', duration: 2000 });
|
| | | break;
|
| | | } |
| | | return Promise.reject(response)
|
| | | })
|
| | | } |