落日与鲸
2025-02-25 9a918deed10e5f1a2b668043b720b922940a794c
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
const GET = 'GET';
const POST = 'POST';
const PUT = 'PUT';
const FORM = 'FORM';
const DELETE = 'DELETE';
import config from '@/config/index.js'
const baseURL = config.BASE_URL;
 
const waitingList = [] //等待队列
const excutingList = [] //执行队列
let showLogoutModal = true //是否显示冻结弹窗
 
function request(method, url, data, type, lodingFlag) {
    if (lodingFlag) {
        uni.showLoading({
            mask: true,
        })
    }
 
    return new Promise(function(resolve, reject) {
 
        let token =
            'Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOjU2MDYyLCJ0eXBlIjoxLCJleHAiOjE3NDE3MDU5MTgsImNyZWF0ZWQiOjE3NDA0MDk5MTg3MDF9.k9ck3gWfVElSCl1uUI_5fZ3lEbFgtbQs_OaJku4I4bXoUO77kUnHsghkPBFdRknFHa49fTdAEdHn0wsb_XN5tg'
        //党员 eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOjE4OTI0ODk0OTc5Nzk5NDA4NjUsInR5cGUiOjEsImV4cCI6MTc0MDQ2NjQ3MCwiY3JlYXRlZCI6MTc0MDM4MDA3MDQzN30.oOlB8GBE50WmV4Kw-Uuy33rQpQ-homoJQ61QXs34rkQIr-jFlAn9dUOgebitI9bz5PUaFImSldhY6vokRjmYlg
        // 社区  eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOjE1OTAxNzYwNTgxNTY1NDgwOTgsInR5cGUiOjEsImV4cCI6MTc0MTcwNTE1OSwiY3JlYXRlZCI6MTc0MDQwOTE1OTEwMH0.AmeZFq2Pj2y2bRF1AolsRWHA4ehobBdx1-LtQHIJzub8WjEh_TxZ-PTEI7uiujlLWKhJE07PCVCggAdeuF3UIA
        // 市  eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOjE4OTI0MjE4MjgwODU4Mzc4MjYsInR5cGUiOjEsImV4cCI6MTc0MTY5ODg0OCwiY3JlYXRlZCI6MTc0MDQwMjg0ODMzN30.7IYRD37yTkhqcnKyXWcqKk_iTisMp3ar_ByfuVR7Go9rK8ZnGJrlwC3z4NF_ly7IIoBHgE2E4IAvfm3ccam8wg
        //街道  eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOjY2OTc5LCJ0eXBlIjoxLCJleHAiOjE3NDE3MDU4NDMsImNyZWF0ZWQiOjE3NDA0MDk4NDMyNTZ9.MOPT3X3Cw9AUmeuIpqDA1a1xZd7V85NSraJddxCrPHvOkaZ8dMI-aUB9gqninzqsMzwmMx6phbD8aBpTlYcWwA
        // 县级  eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOjU2MDYyLCJ0eXBlIjoxLCJleHAiOjE3NDE3MDU5MTgsImNyZWF0ZWQiOjE3NDA0MDk5MTg3MDF9.k9ck3gWfVElSCl1uUI_5fZ3lEbFgtbQs_OaJku4I4bXoUO77kUnHsghkPBFdRknFHa49fTdAEdHn0wsb_XN5tg
        let header = {
            'content-type': type ? 'application/x-www-form-urlencoded;charset=UTF-8' : 'application/json',
            'Authorization': 'Bearer ' + token,
            'lang': uni.getStorageSync('locale') === 'zh-Hans' ? 'zh_CN' : 'zh-tw',
        }
        const requestTask = uni.request({
            url: baseURL + url,
            method: method,
            data: data,
            header: header,
            success: (res) => {
                //判断状态码
                if (lodingFlag) {
                    uni.hideLoading()
                }
                if (res.data.code == 200) {
                    resolve(res.data);
                    return
                }
                if (res.data.code == 502) {
                    resolve(res.data);
                    uni.showToast({
                        title: res.data.msg,
                        duration: 2000,
                        icon: 'none',
                        mask: true,
                    })
                    return
                }
                if (res.data.code == 500 && url.includes('/applet/user/check')) {
                    resolve(res.data);
 
                    return
                }
                if (res.data.code == 1 || res.data.code == 500) {
                    uni.showToast({
                        title: res.data.msg || res.data.data || '服务器错误',
                        duration: 2000,
                        icon: 'none',
                        mask: true,
                    })
                    return
                }
                if (res.data.code == 401 || res.data.code == 510 || res.data.code == 504 || res.data
                    .code == 505) {
                    uni.removeStorageSync('token')
                    if (res.data.code == 504) {
                        uni.showToast({
                            title: res.data.msg,
                            duration: 2000,
                            icon: 'none',
                            mask: true,
                        })
                    } else if (res.data.code == 505) {
                        handleLogout('当前登录账号在其他设备登录')
                    } else {
                        handleLogout('登录失效,请重新登录')
                    }
                    return
                }
 
                if (res.data.code == 501) {
                    uni.showToast({
                        title: res.data.msg,
                        duration: 2000,
                        icon: 'none',
                        mask: true,
                    })
                    return
                }
                if (res.data.code == 506) {
                    resolve(res.data);
                    return
                }
                uni.showToast({
                    title: res.data.msg || '服务器错误',
                    duration: 2000,
                    icon: 'none',
                    mask: true,
                })
 
            },
            fail(err) {
                if (lodingFlag) {
                    uni.hideLoading()
                }
                reject(err)
            },
            complete: () => {
                const excutingIndex = excutingList.findIndex(item => item === requestTask)
                excutingList.splice(excutingIndex, 1)
                const [apiFn] = waitingList
                if (excutingList.length === 0 && typeof apiFn === 'function') {
                    apiFn()
                    waitingList.splice(0, 1)
                }
            },
        })
        excutingList.push(requestTask)
    })
}
 
// 被冻结跳转到登录页并取消后续请求
function handleLogout(str) {
    let routeStr = getCurrentPages()[getCurrentPages().length - 1].route
    if (excutingList.length >= 1) {
        showLogoutModal = true
    }
    if (showLogoutModal) {
        showLogoutModal = false
        uni.showModal({
            title: '提示',
            content: str,
            showCancel: false,
            success: function(res) {
                if (res.confirm) {
                    // if (routeStr == 'pages/login/index') {
                    //     uni.reLaunch({
                    //         url: '/pages/login/index'
                    //     })
                    //     return
                    // }
                    // uni.reLaunch({
                    //     url: '/pages/login/index'
                    // })
                }
            }
        });
        clearAllRequest()
    }
}
 
// 清除请求队列
function clearAllRequest() {
    if (excutingList.length > 0) {
        for (var i = 0; i < excutingList.length; i++) {
            const item = excutingList[i]
            if (item && item.abort) {
                item.abort()
            }
        }
        excutingList.length = 0
        waitingList.length = 0
    }
}
 
export default {
    get: (url, data, type = false, lodingFlag = true) => request(GET, url, data, type, lodingFlag),
    post: (url, data, type = false, lodingFlag = true) => request(POST, url, data, type, lodingFlag),
    put: (url, data, type = false, lodingFlag = true) => request(PUT, url, data, type, lodingFlag),
    FORM: (url, data, type = false, lodingFlag = true) => request(FORM, url, data, type, lodingFlag),
    delete: (url, data, type = false, lodingFlag = true) => request(DELETE, url, data, type, lodingFlag),
}