bug
jiangping
2023-12-06 1f4e7d0f73a73e7350cf5a1df279d5f30904c5d5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
 * 响应拦截
 * @param {Object} http 
 */
module.exports = (vm) => {
    uni.$u.http.interceptors.response.use((response) => {
        uni.hideLoading();
        const data = response.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 || {}
    }, (error) => {
        uni.hideLoading();
        let code  = error.code
        if (error.code === "ECONNABORTED") {
            code = 999
        } 
        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(error)
    })
}