Admin
2025-12-02 9e42f0dafa019f5ecf6b0ff425ecb966b002171e
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
94
95
96
97
98
99
100
101
102
103
104
105
106
const requestUrl = "http://localhost:8098/api/";
//const requestUrl = "http://192.168.0.52:20050/api/";
//"http://192.168.0.103:20050/api/";  
// 记录调用 request 方法的次数
let ajaxTimes = 0;
let params = {
    methods: '',
    url: '',
    data: '',
    token: ''
};
const AjaxRequest = {};
AjaxRequest.RequestUrl = requestUrl;
AjaxRequest.Params = function(method, url, data, token) {
    params.methods = method;
    params.url = url;
    params.data = { ...data,
        Extra: 'APP'
    };
    params.token = token;
}
AjaxRequest.Request = function() {
    ajaxTimes++;
    // uni.showLoading({
    //     title: '加载中',
    //     mask: true
    // });
    //console.log(params);
    return new Promise(function(resolve, reject) {
        //console.log(params.data);
        uni.request({
            ...params,
            method: params.methods,
            url: requestUrl + params.url,
            data: params.data,
            dataType: 'json',
            header: {
                //'Content-Type': 'application/x-www-form-urlencoded'
                'Content-Type': 'application/json; charset=utf-8',
                'Authorization': 'Bearer ' + params.token
            },
            success(res) {
                //console.log(res.data.code);
                if (res.data.code == "401") {
                    uni.showToast({
                        icon: 'none',
                        title: "请求后台异常," + res.data.message,
                        duration: 2000
                    });
                    // console.log(params);
                    //关闭所有页面,打开到应用内的某个页面。
                    setTimeout(function() {
                        uni.reLaunch({
                            url: '/pages/index/index',
                        });
                    }, 2000)
                }
                resolve(res);
            },
            fail(err) {
                reject(err);
            },
            complete() {
                // 每当完成一个请求,让 ajaxTimes 计数器自减一
                ajaxTimes--;
                if (ajaxTimes === 0)
                    uni.hideLoading(); // 停止加载数据的提示
            }
        })
    })
}
 
AjaxRequest.RequestAsync = function() {
    return new Promise(function(resolve, reject) {
        uni.request({
            ...params,
            method: params.methods,
            url: requestUrl + params.url,
            data: params.data,
            dataType: 'json',
            header: {
                'Content-Type': 'application/json; charset=utf-8',
                'Authorization': 'Bearer ' + params.token
            },
            success(res) {
                if (res.data.code == "401") {
                    uni.showToast({
                        icon: 'none',
                        title: "请求后台异常," + res.data.message,
                        duration: 2000
                    });
                    setTimeout(function() {
                        uni.reLaunch({
                            url: '/pages/index/index',
                        });
                    }, 2000)
                }
                resolve(res);
            },
            fail(err) {
                reject(err);
            },
        })
    })
}
export default AjaxRequest;