zhaojs
2023-07-26 1cc2a702ff0338cfe2fc32f840c32209cbdc3e37
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
const requestx = (params) => {
    let baseUrl = uni.getStorageSync('baseUrl');
    /*
     * 1.初始化值
     */
    let _self = this;
    let url = params.url;
    let method = params.method || 'GET';
    let data = params.data || {};
    // data.token = "default-access_token" // default-access_token
    /*
     *2.判断token
     */
    // if (!params.token) { // 如果没有传递token
    //     let token = uni.getStorageSync('token'); // 在本地查找
    //     if (!token) { // 如果本地没有就跳转到登录页面
    //         uni.navigateTo({
    //             url: 'pages/views/login/index'
    //         });
    //     } else {
    //         data.token = '179509245-9c91827e0224bdc18d0b118b8be1b5af';
    //     }
    // }
    /*
     * 3.添加头部
     */
    let defaultOpot = {
        // 'Content-Type': 'application/x-www-form-urlencoded',
        // 'Terminal-Type': 'innerH5',
        'Content-Type': 'application/json;charset=UTF-8',
    }
    /*
     * 4.处理 POST
     */
    let header = {}
    // method = method.toUpperCase()
    // if (method == 'POST') {
    //     header = {
    //         'Content-Type': 'application/x-www-form-urlencoded',
    //     }
    // }
    // 5.请求地址
    const requestUrl = baseUrl + url;
    // debugger
    if (data.pageNo && data.pageNo > 1) {
 
    } else {
        uni.showLoading({
            title: '加载中...'
        });
    }
 
    // 6.用 Promise 创建回调
    return new Promise((resolve, reject) => {
        uni.request({
            url: requestUrl,
            method: method,
            // header:  defaultOpot,
            data: data,
            // dataType: 'json',
            success: (res) => {
                // debugger
                console.log('res', res)
                // 判断 请求api 格式是否正确
                if (res.statusCode && res.statusCode != 200) {
                    uni.showToast({
                        title: "api错误" + res.errMsg,
                        icon: 'none'
                    });
                    return;
                }
                uni.hideLoading();
                // 将结果抛出
                resolve(res.data)
                // return res.data
            },
 
        })
        // .finally(() => {
        //     console.log('不管是否成功都要执行')
        //     uni.hideLoading();
        // })
    })
}
export default requestx