Skip to content

uni-app 多端 SDK ​

HealthGuard uni-app SDK(@health-guard/sdk-uniapp)一套代码同时覆盖 H5、微信小程序、支付宝小程序、抖音小程序和 App 等多个平台,自动检测运行环境并适配对应采集逻辑。

安装 ​

bash
npm install @health-guard/sdk-uniapp

基础用法 ​

ts
import { createUniAppClient } from '@health-guard/sdk-uniapp';

const client = createUniAppClient({
  appKey: 'your-app-key',
  endpoint: 'https://your-server.com/api/events/batch',
  autoCapture: true
});

平台自动检测 ​

SDK 会根据运行环境自动设置 platform 字段:

运行环境platform 值
H5uniapp-h5
微信小程序uniapp-wechat
支付宝小程序uniapp-alipay
抖音/头条小程序uniapp-douyin
Appuniapp-app
未知uniapp

配置选项 ​

选项类型必填默认值说明
appKeystring是-应用唯一标识
endpointstring是-Collector 上报地址
releasestring否-应用版本号
environmentstring否-环境:development / test / production
userIdstring否-业务用户标识
autoCaptureboolean | object否false是否自动捕获错误、请求和 Promise rejection
flushIntervalMsnumber否5000批量上报间隔(毫秒)
maxBatchSizenumber否10单次上报最大事件数
transportFailureRetryDelayMsnumber否30000transport 失败后回队等待重试的间隔(毫秒)

autoCapture 选项 ​

当 autoCapture 为 true 时,全部自动采集开启:

js
autoCapture: {
  errors: true,               // JS 运行时错误
  unhandledRejections: true,  // 未处理的 Promise rejection
  fetch: true,                // H5 fetch(仅在 H5 环境生效)
  xhr: true,                  // H5 XMLHttpRequest(仅在 H5 环境生效)
  request: true               // uni.request(仅在小程序/App 环境生效)
}

手动上报 API ​

captureException(error) ​

手动上报一个错误:

js
try {
  riskyOperation();
} catch (err) {
  client.captureException(err);
}

captureHttp(input) ​

手动上报一次 HTTP 请求:

js
client.captureHttp({
  method: 'POST',
  url: '/api/order',
  status: 500,
  duration: 240,
  success: false,
  errorMessage: 'Internal Server Error'
});

capturePerformance(input) ​

手动上报一个性能指标:

js
client.capturePerformance({
  name: 'custom-metric',
  value: 120,
  rating: 'good'
});

addBreadcrumb(breadcrumb) ​

添加一条面包屑:

js
client.addBreadcrumb({
  type: 'navigation',
  message: 'User navigated to /pages/order/detail',
  data: { from: '/pages/index/index' }
});

flush() ​

立即刷新队列,上报所有未发送事件(包括 transport 失败后重新入队的事件):

js
await client.flush();

隐私与脱敏 ​

  • URL Query 中的 authorization、password、token、secret、cookie 等字段会被自动过滤。
  • 不上报 request body 和 response body。
  • 使用业务方传入的 userId 或匿名 ID,不采集真实身份信息。

网络诊断(未发布源码) ​

2026-10-14 源码新增诊断与补传能力,配置方式、发布状态和不可用字段见 网络诊断。Flutter 接入见 Flutter SDK。

Released under the MIT License.