fix
13404089107
2026-04-30 5a965f2cdf57b9ac4fbba238c77a9bbf94ddc397
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
const GET = 'GET';
const POST = 'POST';
const PUT = 'PUT';
const FORM = 'FORM';
const DELETE = 'DELETE';
import config from '@/config/index.js'
import { clearJPushAliasBinding } from '@/utils/push.js'
const baseURL = config.BASE_URL;
 
const waitingList = [] //等待队列
const excutingList = [] //执行队列
let appAuthRedirecting = false
 
/**
 * 判断当前响应码是否属于成功态。
 */
function isSuccessCode(code) {
    return code === 0 || code === 200
}
 
/**
 * 从响应体中提取可展示的错误文案。
 */
function resolveMessage(payload) {
    if (!payload) {
        return '服务器错误'
    }
    if (typeof payload.data === 'string' && payload.data) {
        return payload.data
    }
    return payload.msg || payload.message || '服务器错误'
}
 
/**
 * 统一发起请求,并兼容当前仓库 `code=0` 的后端成功协议。
 */
function request(method, url, data, type, lodingFlag = true) {
    if (lodingFlag) {
        uni.showLoading({
            mask: true,
        })
    }
    return new Promise(function(resolve, reject) {
        let token = uni.getStorageSync('token')
        let header = {
            'content-type': type ? 'application/x-www-form-urlencoded;charset=UTF-8' : 'application/json',
            'Authorization': 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()
                }
                const payload = res.data || {}
                if (isSuccessCode(payload.code)) {
                    resolve(payload);
                    return
                }
                if (payload.code == 502) {
                    resolve(payload);
                    uni.showToast({
                        title: resolveMessage(payload),
                        duration: 2000,
                        icon: 'none',
                        mask: true,
                    })
                    return
                }
                if (payload.code == 1 || payload.code == 500) {
                    uni.showToast({
                        title: resolveMessage(payload),
                        duration: 2000,
                        icon: 'none',
                        mask: true,
                    })
                    resolve(payload)
                    return
                }
                if (payload.code == 401 || payload.code == 510 || payload.code == 504 || payload.code == 505) {
                    handleUnauthorized(payload)
                    resolve(payload)
                    return
                }
 
                if (payload.code == 501) {
                    uni.showToast({
                        title: resolveMessage(payload),
                        duration: 2000,
                        icon: 'none',
                        mask: true,
                    })
                    resolve(payload)
                    return
                }
                if (payload.code == 506) {
                    resolve(payload);
                    return
                }
 
                uni.showToast({
                    title: resolveMessage(payload),
                    duration: 2000,
                    icon: 'none',
                    mask: true,
                })
                resolve(payload)
            },
            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)
    })
 
 
}
 
/**
 * 收到未登录响应时清理 APP 登录态并跳转登录页。
 */
function handleUnauthorized(payload) {
    const routeStr = getCurrentRoute()
    clearLoginSession()
    clearAllRequest()
    if (routeStr === 'pages/login/login' || appAuthRedirecting) {
        return
    }
    appAuthRedirecting = true
    uni.showToast({
        title: resolveMessage(payload) || '请先登录',
        duration: 1500,
        icon: 'none',
        mask: true,
    })
    setTimeout(() => {
        uni.reLaunch({
            url: '/pages/login/login'
        })
        setTimeout(() => {
            appAuthRedirecting = false
        }, 300)
    }, 150)
}
 
/**
 * 清除 APP 登录态缓存。
 */
function clearLoginSession() {
    uni.removeStorageSync('token')
    uni.removeStorageSync('appUserInfo')
    uni.removeStorageSync('loginRedirectMeta')
    uni.setStorageSync('showLogoutModal', 0)
    clearJPushAliasBinding()
}
 
/**
 * 获取当前页面路由,便于避免在登录页重复跳转。
 */
function getCurrentRoute() {
    const currentPages = getCurrentPages()
    if (!currentPages || currentPages.length === 0) {
        return ''
    }
    return currentPages[currentPages.length - 1].route || ''
}
 
/**
 * 清除请求队列。
 */
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
    }
}
 
/**
 * 发起 GET 请求。
 */
function get(url, data, type = false, lodingFlag = true) {
    return request(GET, url, data, type, lodingFlag)
}
 
/**
 * 发起 POST 请求。
 */
function post(url, data, type = false, lodingFlag = true) {
    return request(POST, url, data, type, lodingFlag)
}
 
/**
 * 发起 PUT 请求。
 */
function put(url, data, type = false, lodingFlag = true) {
    return request(PUT, url, data, type, lodingFlag)
}
 
/**
 * 发起表单编码请求。
 */
function form(url, data, type = false, lodingFlag = true) {
    return request(FORM, url, data, type, lodingFlag)
}
 
/**
 * 发起 DELETE 请求。
 */
function remove(url, data, type = false, lodingFlag = true) {
    return request(DELETE, url, data, type, lodingFlag)
}
 
export default {
    get,
    post,
    put,
    FORM: form,
    delete: remove,
}