request.js
4.46 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
/*
* @Date: 2022-09-19 14:11:06
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2026-02-02 18:00:00
* @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'
/**
* @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 || {})
}
// 增加时间戳
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) {
// 跳转到登录页
Taro.navigateTo({
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