schangxiang@126.com
2025-05-20 3a61cb05bd4339b89127b15c489ae76370905404
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// 同时发送异步请求的次数,防止一次点击有多次请求
let ajaxTime = 0;
// export const baseUrl = 'http://10.10.40.166:2066' // http://localhost:5005
export const baseUrl = 'http://192.168.1.100:2001'
// export const baseUrl = 'http://192.168.1.100:2001'
 
// 公共的request方法
function request(option) {
    //请求头.
    const header = {};
    // 返回方式
    header['Content-Type'] = option.contentType || 'application/json';
    // 判断是否是登录请求,不是在header中加上token
    if (option.url != '/login') {
        header['Authorization'] = uni.getStorageSync('user_token') ? 'Bearer ' + uni.getStorageSync('user_token') : '';
        header['x-Authorization'] = uni.getStorageSync('refresh_token') ? 'Bearer ' + uni.getStorageSync('refresh_token') : '';
        header.Authorization = uni.getStorageSync('user_token') ? 'Bearer ' + uni.getStorageSync('user_token') : ''
    }
    ajaxTime++;
    // 显示加载中效果
    uni.showLoading({
        title: '加载中...',
        mask: true
    });
 
    return new Promise((resolve, reject) => {
        uni.request({
            url: baseUrl + option.url,
            method: option.method || 'POST',
            data: option.data || {},
            header,
            timeout: 10000,
            success: (res) => {
                if(res.statusCode == 200) {
                    console.log(res,'res');
                    const result = res.data;
                    if (result.code === 200||!Boolean(result.code)) {
                        // 请求状态正常,返回数据
                        resolve(result)
                    } else if (result.code === 401) { 
                        //token失效,清除token关闭当前页面,跳转到登录
                        uni.showModal({
                            title: '提示',
                            content: 'token失效,请重新登陆',
                            showCancel: false,
                            success:()=>{
                                // 清楚token
                                uni.removeStorageSync('token')
                                // 跳转到登录
                                uni.redirectTo({
                                    url: '/pages/login/index'
                                })
                            }
                        })
                        
                    } else {
                        uni.showModal({
                            title: '提示',
                            content: `${result.code}:${JSON.stringify(result.message)}`,
                            showCancel: false
                        })
                        // 异常,返回异常code和message
                        reject(result)
                    }
                    
                }else {
                    uni.showModal({
                        title: '提示',
                        content: `${res.statusCode}:${JSON.stringify(res.errMsg)}`,
                        showCancel: false
                    })
                    
                    const {statusCode,errMsg} = res;
                    reject({statusCode,errMsg})
                }
            },
            fail: (err) => {
                // 请求失败
                reject(err);
            },
            complete: () => {
                // 请求完成后判断状态
                ajaxTime--;
                // 只有值等于0,才清除转圈效果
                if (ajaxTime == 0) {
                    uni.hideLoading()
                }
            }
        })
    })
}
 
export default request