更新记录
1.0.0(2026-07-19)
新增
- 自动重试能力:请求失败时根据配置自动重试
- 指数退避策略:支持固定延迟和指数退避两种重试间隔
- 灵活重试条件:通过
shouldRetry自定义重试条件,支持基于error和options灵活判断 - 全方法支持:
request/get/post/upload/download全部支持重试 - 可配置参数:支持自定义最大重试次数、基础延迟时间
平台兼容性
uni-app(3.8.0)
| Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | iOS | 鸿蒙 |
|---|---|---|---|---|---|---|---|---|
| √ | √ | √ | √ | √ | √ | √ | √ | √ |
| 微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 小红书小程序 | 快应用-华为 | 快应用-联盟 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| √ | √ | √ | √ | √ | √ | √ | √ | √ | √ | √ | √ |
uni-app x(3.8.0)
| Chrome | Safari | Android | iOS | 鸿蒙 | 微信小程序 |
|---|---|---|---|---|---|
| √ | √ | √ | √ | √ | √ |
mh-request-retry
请求重试插件,网络请求失败时自动重试,支持指数退避、自定义重试条件,灵活控制。
本插件是 mh-request 的配套插件,需配合使用。
特性
- 🔄 自动重试:请求失败时根据配置自动重试,无需额外编码
- 📈 指数退避:支持固定延迟和指数退避两种策略,避免瞬时高并发冲击服务端
- 🎯 灵活控制:通过
shouldRetry函数传入options,可根据 URL、Method 等任意条件决定是否重试 - 📦 全方法支持:
request/get/post/upload/download全部支持重试
快速开始
import { Request } from '@/uni_modules/mh-request'
import { retryPlugin } from '@/uni_modules/mh-request-retry'
const request = new Request({
baseURL: 'https://api.example.com',
plugins: [
{
plugin: retryPlugin,
config: {
maxRetries: 3,
retryDelay: 1000,
backoff: true
}
}
]
})
// 请求失败时会自动重试
const data = await request.get('/user/info')
配置项
| 属性 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| maxRetries | number |
❌ | 3 |
最大重试次数 |
| retryDelay | number |
❌ | 1000 |
基础延迟(毫秒) |
| backoff | boolean |
❌ | true |
是否启用指数退避。true 时延迟为 retryDelay * 2^(attempt-1),false 时固定为 retryDelay |
| shouldRetry | (error: any, options: any) => boolean |
❌ | 超时或 5xx 重试 | 自定义重试条件函数。参数 options 为请求配置,可用于灵活判断是否跳过重试 |
默认重试条件
(error) => {
if (error?.errMsg?.includes('timeout')) return true // 超时
if (error?.statusCode && error.statusCode >= 500) return true // 5xx 服务端错误
return false
}
工作原理
重试流程
发起请求 → 请求失败 → 判断是否满足重试条件
↓
满足 → 延迟后重试(重复直到成功或达到最大次数)
↓
不满足 → 直接抛出错误
指数退避示例
maxRetries: 3, retryDelay: 1000, backoff: true
| 重试次数 | 延迟时间 |
|---|---|
| 第 1 次 | 1000ms |
| 第 2 次 | 2000ms |
| 第 3 次 | 4000ms |
高级用法
排除特定 URL 不重试
const request = new Request({
plugins: [{
plugin: retryPlugin,
config: {
shouldRetry: (error, options) => {
// 不重试 /no-retry 路径的请求
if (options.url?.includes('/no-retry')) return false
// 只对超时和 5xx 重试
if (error?.errMsg?.includes('timeout')) return true
if (error?.statusCode >= 500) return true
return false
}
}
}]
})
仅对特定请求方法启用重试
const request = new Request({
plugins: [{
plugin: retryPlugin,
config: {
shouldRetry: (error, options) => {
// 只对 GET 请求重试
if (options.method !== 'GET') return false
if (error?.errMsg?.includes('timeout')) return true
if (error?.statusCode >= 500) return true
return false
}
}
}]
})
对业务错误码重试
const request = new Request({
plugins: [{
plugin: retryPlugin,
config: {
shouldRetry: (error, options) => {
// 业务错误码 10001 触发重试
if (error?.code === 10001) return true
// HTTP 429 限流重试
if (error?.statusCode === 429) return true
if (error?.errMsg?.includes('timeout')) return true
if (error?.statusCode >= 500) return true
return false
}
}
}]
})
调整重试间隔
// 固定间隔 3 秒
const request = new Request({
plugins: [{
plugin: retryPlugin,
config: {
maxRetries: 5,
retryDelay: 3000,
backoff: false // 禁用指数退避,固定间隔
}
}]
})
// 短间隔 + 指数退避(适合快速恢复场景)
const request = new Request({
plugins: [{
plugin: retryPlugin,
config: {
maxRetries: 3,
retryDelay: 500,
backoff: true // 500ms → 1000ms → 2000ms
}
}]
})
注意事项
重试条件区分网络错误和业务错误
error 对象来源:
- 网络错误(超时、断网等):来自
uni.request的fail回调,通常包含errMsg字段 - 业务错误(code 非成功码):来自
mh-request的reject,包含code、msg等业务字段
shouldRetry 函数同时支持两种错误类型,可根据实际情况灵活判断。
避免重试幂等性问题
对于非幂等请求(如支付、下单),建议通过 shouldRetry 排除:
shouldRetry: (error, options) => {
// POST /order 不重试,避免重复下单
if (options.url === '/order' && options.method === 'POST') return false
// 其他请求正常判断
if (error?.errMsg?.includes('timeout')) return true
if (error?.statusCode >= 500) return true
return false
}
重试次数不宜过大
建议 maxRetries 控制在 3~5 次,避免长时间阻塞用户操作。

收藏人数:
购买普通授权版(
试用
赞赏(0)
下载 7
赞赏 0
下载 12440709
赞赏 1934
赞赏
京公网安备:11010802035340号