更新记录
1.0.0(2026-01-07)
支持 sse
平台兼容性
uni-app(4.87)
| Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | iOS | 鸿蒙 |
|---|---|---|---|---|---|---|---|---|
| √ | √ | - | - | - | - | - | - | - |
| 微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 快应用-华为 | 快应用-联盟 |
|---|---|---|---|---|---|---|---|---|---|---|
| - | - | - | - | - | - | - | - | - | - | - |
uni-app x(4.87)
| Chrome | Safari | Android | iOS | 鸿蒙 | 微信小程序 |
|---|---|---|---|---|---|
| - | - | √ | √ | - | - |
hans-request
uni-app / uni-app x APP 平台的 UTS 网络请求封装(Android: OkHttp;iOS: URLSession)。
当前阶段仅保留 网络请求模块:
request(options)(支持task.onHeadersReceived/task.onChunkReceived/task.onTextChunkReceived)setNetworkLogEnabled(enabled)(开启插件内部日志)
平台支持
APP-PLUS:Android / iOS- 其它平台:未实现(H5 / 小程序等)
API
request(options): RequestTask
options.url: stringoptions.method?: "GET" | "POST" | ...(默认GET)options.data?: UTSJSONObject | string | ArrayBuffer | objectoptions.header?: UTSJSONObjectoptions.timeout?: number(毫秒)options.responseType?: "json" | "text" | "arraybuffer"(默认json;SSE/流式建议text)options.enableChunked?: boolean(启用分块回调;iOS 必须为true才会触发 chunk 回调)options.success?: (res) => voidoptions.fail?: (err) => voidoptions.complete?: (resOrErr) => void
success 回调入参:
statusCode: numberdata: any | null(由responseType决定:json/text/arraybuffer)header: UTSJSONObjectcookies: string[]
RequestTask:
abort()onHeadersReceived(listener)/offHeadersReceived(id?)onChunkReceived(listener)/offChunkReceived(id?)(回调入参res.data: ArrayBuffer;uni-app 环境不支持,使用onTextChunkReceived)onTextChunkReceived(listener)/offTextChunkReceived(id?)(回调入参res.text: string;uni-app 必选,iOS 推荐)
setNetworkLogEnabled(enabled: boolean): void
开启/关闭插件内部 console.log 输出(默认关闭)。
使用示例
普通请求(JSON)
import { request } from '@/uni_modules/hans-request'
request({
url: 'http://10.0.2.2:8788/echo?hello=world',
method: 'GET',
responseType: 'json',
timeout: 60000,
success: (res) => console.log('success', res.statusCode, res.data),
fail: (err) => console.log('fail', err),
})
POST(JSON Body)
import { request } from '@/uni_modules/hans-request'
request({
url: 'http://10.0.2.2:8788/echo',
method: 'POST',
responseType: 'json',
header: { 'Content-Type': 'application/json' },
data: { hello: 'world' },
success: (res) => console.log('success', res.statusCode, res.data),
fail: (err) => console.log('fail', err),
})
SSE / 流式(分块回调)
说明:插件只提供原始文本/字节分块回调;SSE 协议解析需要在上层自行完成(见 demo 页)。
import { request } from '@/uni_modules/hans-request'
function decodeChunkToText(buf) {
const u8 = new Uint8Array(buf)
let out = ''
for (let i = 0; i < u8.length; i++) out += String.fromCharCode(u8[i])
return out
}
const task = request({
url: 'http://10.0.2.2:8788/sse?count=5&interval=200&text=tick-',
method: 'GET',
responseType: 'text',
enableChunked: true,
header: { Accept: 'text/event-stream' },
timeout: 10 * 60 * 1000,
})
task.onHeadersReceived((res) => console.log('headers', res.statusCode, res.header))
// uni-app(非 x):只能使用 onTextChunkReceived(onChunkReceived 在 uni-app 不可用)
if (typeof task.onTextChunkReceived === 'function') {
task.onTextChunkReceived((res) => console.log('chunk(text)', res.text))
} else {
task.onChunkReceived((res) => console.log('chunk(buf)', decodeChunkToText(res.data)))
}
// task.abort()
常见问题
responseType: "json"会对响应体执行JSON.parse;若不是合法 JSON,会触发fail(errCode=100001),并在err.data中保留原始文本。- iOS 访问
http://可能受 ATS 限制;建议优先使用https://或按项目需要配置放行策略。

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