更新记录
1.0.0(2026-07-08) 下载此版本
基于uni.request封装的UTS网络请求插件,支持GET/POST/PUT/DELETE/文件上传
平台兼容性
uni-app x(4.87)
| Chrome | Safari | Android | iOS | 鸿蒙 | 微信小程序 |
|---|---|---|---|---|---|
| √ | √ | √ | √ | √ | √ |
wp-request
基于 uni.request 封装的 UTS 网络请求插件,支持 GET/POST/PUT/DELETE 及文件上传,适用于 uni-app x 项目。
快速开始
1. 创建请求初始化文件
在项目中创建 utils/request/index.uts,统一初始化配置并导出便捷方法:
import { WpRequest } from '@/uni_modules/wp-request'
import { BASE_URL, BASE_API } from '@/config/env'
import { WpStorage } from '@/uni_modules/wp-storage'
import { showTips } from '@/utils/uniTool'
WpRequest.create({
baseUrl: BASE_URL + BASE_API,
timeout: 120000,
tokenProvider: () : string | null => {
return WpStorage.getToken()
},
onUnauthorized: () : void => {
uni.reLaunch({ url: '/pages/login/index' })
},
onError: (msg : string) : void => {
showTips({ title: msg.length > 0 ? msg : '请求失败' })
}
})
export const get = WpRequest.get
export const post = WpRequest.post
export const put = WpRequest.put
export const del = WpRequest.delete
export const uploadFile = WpRequest.uploadFile
export const request = WpRequest.request
2. 在 API 文件中使用
import { get, post } from '@/utils/request'
export function bannerListAPI() : Promise<UTSJSONObject> {
return get('/app/banner/list').then((res) => res.data)
}
export function goodsDetailAPI(params : UTSJSONObject = {} as UTSJSONObject) : Promise<UTSJSONObject> {
return get('/app/goods/detail', params).then((res) => res.data)
}
export function createOrderAPI(params : UTSJSONObject = {} as UTSJSONObject) : Promise<UTSJSONObject> {
return post('/app/order/create', params).then((res) => res.data)
}
插件直接使用
也可以直接引入插件:
import { WpRequest } from "@/uni_modules/wp-request"
初始化配置(WpRequestConfig)
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| baseUrl | string | 是 | - | 接口基础地址 |
| timeout | number | 否 | 120000 | 请求超时时间(ms) |
| header | UTSJSONObject | 否 | {} | 全局默认请求头 |
| tokenProvider | () => string | null | 否 | null | Token 提供函数,返回值自动添加到 Authorization |
| tokenPrefix | string | 否 | "Bearer" | Authorization 前缀,设为空字符串则不添加前缀 |
| onUnauthorized | () => void | 否 | null | 401 未授权回调 |
| onError | (msg: string) => void | 否 | null | 请求错误回调 |
请求方法
GET
const res = await get('/user/info')
const res = await get('/user/list', { page: 1 } as UTSJSONObject)
const res = await get('/user/info', null, { 'X-Custom': 'val' } as UTSJSONObject)
POST
const res = await post('/order/create', { itemId: 123 } as UTSJSONObject)
PUT
const res = await put('/user/update', { nickname: '新名称' } as UTSJSONObject)
DELETE
const res = await del('/order/cancel', { orderId: 456 } as UTSJSONObject)
通用请求
const res = await request({
url: '/user/info',
method: 'GET',
data: { id: 1 } as UTSJSONObject,
header: { 'X-Token': 'xxx' } as UTSJSONObject,
timeout: 30000
})
WpRequestOptions
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| url | string | 是 | - | 接口路径(会拼接 baseUrl) |
| method | RequestMethod | 否 | GET | 请求方法 |
| data | UTSJSONObject | 否 | null | 请求参数 |
| header | UTSJSONObject | 否 | null | 请求头(覆盖全局配置) |
| timeout | number | 否 | 配置值 | 本次请求超时时间 |
| responseType | string | 否 | null | 响应数据类型 |
文件上传
const res = await uploadFile({
url: '/file/upload',
filePath: '/path/to/file.jpg',
name: 'file',
formData: { type: 'avatar' } as UTSJSONObject
})
WpUploadOptions
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| url | string | 是 | - | 上传接口路径 |
| filePath | string | 是 | - | 本地文件路径 |
| name | string | 否 | "file" | 文件对应的 key |
| header | UTSJSONObject | 否 | null | 请求头 |
| formData | UTSJSONObject | 否 | null | 额外的表单数据 |
| timeout | number | 否 | 配置值 | 上传超时时间 |
响应格式(WpResponse)
| 字段 | 类型 | 说明 |
|---|---|---|
| statusCode | number | HTTP 状态码 |
| data | UTSJSONObject | 响应数据 |
| header | UTSJSONObject | 响应头 |
动态修改配置
WpRequest.setConfig({
baseUrl: 'https://new-api.example.com/v2',
timeout: 60000
})
Token 自动注入
配置 tokenProvider 后,每次请求自动调用该函数获取 Token 并添加到请求头:
WpRequest.create({
baseUrl: 'https://api.example.com',
tokenProvider: () => uni.getStorageSync('access_token') as string | null
})
请求头自动添加:Authorization: Bearer <token>
自定义前缀或无前缀:
tokenPrefix: '' // Authorization: <token>
tokenPrefix: 'Token' // Authorization: Token <token>
错误码
| 错误码 | 说明 |
|---|---|
| 9040001 | 请求配置无效(baseUrl 为空) |
| 9040002 | 网络请求失败 |
| 9040003 | 服务器响应异常 |
| 9040004 | 未授权访问(401) |
| 9040005 | 文件上传失败 |
支持平台
- Android
- iOS
- HarmonyOS

收藏人数:
下载插件并导入HBuilderX
赞赏(0)
下载 6
赞赏 1
下载 12404259
赞赏 1930
赞赏
京公网安备:11010802035340号