request.js
8.7 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
* @Date: 2022-09-19 14:11:06
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2026-02-02 18:32:59
* @FilePath: /manulife-weapp/src/utils/request.js
* @Description: HTTP 请求封装(简化版)
*/
import axios from 'axios-miniprogram'
import Taro from '@tarojs/taro'
import { parseQueryString } from './tools'
import BASE_URL, { REQUEST_DEFAULT_PARAMS } from './config'
import { routerStore } from '@/stores/router'
/**
* @description 获取 sessionid 的工具函数
* - sessionid 由 authRedirect.refreshSession 写入
* - 每次请求前动态读取,避免旧会话导致的 401
* @returns {string|null} sessionid或null
*/
export const getSessionId = () => {
try {
return Taro.getStorageSync('sessionid') || null
} catch (error) {
console.error('获取sessionid失败:', error)
return null
}
}
/**
* @description 设置 sessionid(一般不需要手动调用)
* - 正常情况下由 authRedirect.refreshSession 写入
* - 保留该方法用于极端场景的手动修复/兼容旧逻辑
* - 自动清理重复的 cookie(防止后端返回重复的 set-cookie)
* @param {string} sessionid cookie 字符串
* @returns {void} 无返回值
*/
export const setSessionId = sessionid => {
try {
if (!sessionid) {
return
}
// 自动清理重复的 cookie
// 例如:"PHPSESSID=xxx; ...,PHPSESSID=xxx; ..." -> "PHPSESSID=xxx; ..."
const cleaned = cleanupDuplicateCookies(sessionid)
Taro.setStorageSync('sessionid', cleaned)
} catch (error) {
console.error('设置sessionid失败:', error)
}
}
/**
* @description 清理重复的 cookie
* @description 如果 cookie 字符串中有重复项,只保留第一个
* @param {string} cookies cookie 字符串
* @returns {string} 清理后的 cookie 字符串
*
* @example
* cleanupDuplicateCookies('PHPSESSID=xxx; ...,PHPSESSID=xxx; ...')
* // 返回: 'PHPSESSID=xxx; ...'
*/
function cleanupDuplicateCookies(cookies) {
if (!cookies || typeof cookies !== 'string') {
return cookies
}
// 按逗号分割(重复的 cookie 用逗号连接)
const parts = cookies.split(',')
// 如果只有一个部分,直接返回
if (parts.length === 1) {
return cookies
}
// 提取 cookie 名称(用于去重)
const extractCookieName = (cookieStr) => {
const match = cookieStr.match(/^([^=]+)=/)
return match ? match[1].trim() : null
}
// 去重:只保留第一次出现的 cookie
const seen = new Set()
const uniqueParts = []
for (const part of parts) {
const name = extractCookieName(part)
if (name && !seen.has(name)) {
seen.add(name)
uniqueParts.push(part)
}
}
// 如果去重后只剩一个,直接返回
if (uniqueParts.length === 1) {
return uniqueParts[0]
}
// 否则用逗号连接
return uniqueParts.join(',')
}
/**
* @description 清空 sessionid(一般不需要手动调用)
* @returns {void} 无返回值
*/
export const clearSessionId = () => {
try {
Taro.removeStorageSync('sessionid')
} catch (error) {
console.error('清空sessionid失败:', error)
}
}
/**
* @description axios 实例
* - 统一 baseURL / timeout
* - 通过拦截器处理:默认参数、401 跳转登录页、弱网降级
*/
const service = axios.create({
baseURL: BASE_URL,
timeout: 5000,
})
let has_shown_timeout_modal = false
/**
* @description 判断是否为超时错误
* @param {Error} error 请求错误对象
* @returns {boolean} true=超时,false=非超时
*/
const is_timeout_error = (error) => {
const msg = String(error?.message || error?.errMsg || '')
if (error?.code === 'ECONNABORTED') return true
return msg.toLowerCase().includes('timeout')
}
/**
* @description 判断是否为网络错误(断网/弱网/请求失败等)
* @param {Error} error 请求错误对象
* @returns {boolean} true=网络错误,false=非网络错误
*/
const is_network_error = (error) => {
const msg = String(error?.message || error?.errMsg || '')
const raw = (() => {
try {
return JSON.stringify(error) || ''
} catch (e) {
return ''
}
})()
const lower = (msg + ' ' + raw).toLowerCase()
if (lower.includes('request:fail')) return true
if (lower.includes('request fail')) return true
if (lower.includes('network error')) return true
if (lower.includes('failed to fetch')) return true
if (lower.includes('the internet connection appears to be offline')) return true
if (lower.includes('err_blocked_by_client')) return true
if (lower.includes('blocked_by_client')) return true
return false
}
/**
* @description 是否需要触发弱网/断网降级逻辑
* - 超时:直接触发
* - 网络错误:直接触发(避免 wifi 但无网场景漏判)
* @param {Error} error 请求错误对象
* @returns {Promise<boolean>} true=需要降级,false=不需要
*/
const should_handle_bad_network = async (error) => {
if (is_timeout_error(error)) return true
return is_network_error(error)
}
/**
* @description 处理请求超时/弱网错误
* - 弹出弱网提示
* @returns {Promise<void>} 无返回值
*/
const handle_request_timeout = async () => {
if (has_shown_timeout_modal) return
has_shown_timeout_modal = true
// 提示用户检查网络连接
try {
await Taro.showToast({
title: '网络连接异常,请检查网络设置',
icon: 'none',
duration: 2000
})
} catch (e) {
console.error('show weak network toast failed:', e)
}
}
// 请求拦截器:合并默认参数
service.interceptors.request.use(
config => {
// 解析 URL 参数并合并
const url = config.url || ''
let url_params = {}
if (url.includes('?')) {
url_params = parseQueryString(url)
config.url = url.split('?')[0]
}
// 优先级:调用传参 > URL参数 > 默认参数
config.params = {
...REQUEST_DEFAULT_PARAMS,
...url_params,
...(config.params || {})
}
/**
* 动态获取 sessionid 并设置到请求头
* - 确保每个请求都带上最新的 sessionid
* - 注意:axios-miniprogram 的 headers 可能不存在,需要先兜底
*/
const sessionid = getSessionId()
if (sessionid) {
config.headers = config.headers || {}
config.headers.cookie = sessionid
}
// 增加时间戳
if (config.method === 'get') {
config.params = { ...config.params, timestamp: (new Date()).valueOf() }
}
return config
},
error => {
console.error('请求拦截器异常:', error)
return Promise.reject(error)
}
)
// 响应拦截器:401 跳转登录页 / 弱网降级
service.interceptors.response.use(
/**
* @description 响应成功拦截器
* - 处理 401 未授权,跳转到登录页
* - 处理其他自定义错误消息
*/
async response => {
const res = response.data
// 401 未授权处理
if (res.code === 401) {
// 获取当前页面路径
const pages = Taro.getCurrentPages()
const currentPage = pages[pages.length - 1]
const currentPath = currentPage?.route || ''
// 保存当前路径到 router store(用于登录后跳转回原页面)
if (currentPath && currentPath !== 'pages/login/index') {
const store = routerStore() // 调用函数获取 store 实例
store.add('/' + currentPath)
console.log('401: 保存当前路径到 router store:', '/' + currentPath)
} else {
console.log('401: 当前路径为空或是登录页,不保存路径:', currentPath)
}
// 根据导航栈长度选择跳转方式
if (pages.length <= 1) {
// 如果导航栈只有1个页面,使用 reLaunch 清空栈并跳转到登录页
// 这样可以避免"cannot navigate back"错误
console.log('401: 导航栈只有1页,使用 reLaunch')
Taro.reLaunch({
url: '/pages/login/index'
}).catch(() => {
console.warn('reLaunch 到登录页失败')
})
} else {
// 如果导航栈有多个页面,使用 redirectTo 替换当前页面
console.log('401: 导航栈有多个页面,使用 redirectTo')
Taro.redirectTo({
url: '/pages/login/index'
}).catch(() => {
// 如果跳转失败(如已经在登录页),则忽略
console.warn('跳转登录页失败,可能已在登录页')
})
}
}
// 处理特殊消息(不需要显示的错误)
if (['预约ID不存在'].includes(res.msg)) {
res.show = false
}
return response
},
/**
* @description 响应失败拦截器
* - 处理网络错误、超时等
*/
async error => {
// 处理弱网/断网
if (await should_handle_bad_network(error)) {
handle_request_timeout()
}
return Promise.reject(error)
}
)
export default service