110 行
3.1 KiB
Markdown
110 行
3.1 KiB
Markdown
|
|
# @xuqm/rn-common
|
|||
|
|
|
|||
|
|
XuqmGroup RN SDK 核心模块。负责 SDK 初始化、HTTP 请求、Token 管理、设备信息、XWebView Bridge、API hooks。
|
|||
|
|
|
|||
|
|
## 安装
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
yarn add @xuqm/rn-common @react-native-async-storage/async-storage
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
Peer dependencies:`react >= 18`,`react-native >= 0.76`,`axios >= 1.0.0`
|
|||
|
|
|
|||
|
|
## 初始化
|
|||
|
|
|
|||
|
|
### 方式 A — 配置文件自动初始化(推荐)
|
|||
|
|
|
|||
|
|
在宿主项目中配置 Babel alias `@xuqm/autoinit-config` → 加密配置文件:
|
|||
|
|
|
|||
|
|
```js
|
|||
|
|
// metro.config.js
|
|||
|
|
config.resolver.extraNodeModules = {
|
|||
|
|
'@xuqm/autoinit-config': './path/to/your.xuqmconfig',
|
|||
|
|
};
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
SDK 在 import 时自动调用 `initWithConfigFile()`,App 无需任何 init 代码。
|
|||
|
|
|
|||
|
|
### 方式 B — 手动初始化
|
|||
|
|
|
|||
|
|
```ts
|
|||
|
|
import { XuqmSDK } from '@xuqm/rn-common'
|
|||
|
|
|
|||
|
|
await XuqmSDK.initialize({ appKey: 'xxx' }) // 公有平台
|
|||
|
|
await XuqmSDK.initialize({ appKey: 'xxx', platformUrl: 'https://xxx' }) // 私有化
|
|||
|
|
await XuqmSDK.awaitInitialization() // 等待配置拉取
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## API
|
|||
|
|
|
|||
|
|
### 初始化
|
|||
|
|
|
|||
|
|
| API | 说明 |
|
|||
|
|
|-----|------|
|
|||
|
|
| `XuqmSDK.initialize(opts)` | 初始化,拉取平台配置 |
|
|||
|
|
| `XuqmSDK.awaitInitialization()` | 等待异步初始化完成 |
|
|||
|
|
| `isInitialized()` | 检查 SDK 是否就绪 |
|
|||
|
|
| `getConfig()` | 获取已解析的平台配置 |
|
|||
|
|
|
|||
|
|
### 用户信息
|
|||
|
|
|
|||
|
|
| API | 说明 |
|
|||
|
|
|-----|------|
|
|||
|
|
| `XuqmSDK.setUserInfo(info)` | 设置用户信息,触发所有子 SDK 登录 |
|
|||
|
|
| `XuqmSDK.setUserInfo(null)` | 登出,触发全局登出 |
|
|||
|
|
| `getUserId()` | 获取当前 userId |
|
|||
|
|
| `getUserInfo()` | 获取当前用户信息 |
|
|||
|
|
| `setUserId(id)` | 直接设置 userId(内部用) |
|
|||
|
|
|
|||
|
|
### HTTP
|
|||
|
|
|
|||
|
|
| API | 说明 |
|
|||
|
|
|-----|------|
|
|||
|
|
| `apiRequest(url, options?)` | 带 Bearer token 的 HTTP 请求 |
|
|||
|
|
| `configureHttp(opts)` | 覆盖 HTTP 基础配置 |
|
|||
|
|
| `_getToken()` | 获取当前 token(内部用) |
|
|||
|
|
| `_saveToken(token)` | 保存 token(内部用) |
|
|||
|
|
| `_clearToken()` | 清除 token(内部用) |
|
|||
|
|
|
|||
|
|
### 设备
|
|||
|
|
|
|||
|
|
| API | 说明 |
|
|||
|
|
|-----|------|
|
|||
|
|
| `getDeviceId()` | 获取设备稳定 UUID |
|
|||
|
|
| `getDeviceInfo()` | 获取设备品牌/型号/系统信息 |
|
|||
|
|
| `detectPushVendor()` | 检测设备推送厂商 |
|
|||
|
|
|
|||
|
|
### XWebView Bridge
|
|||
|
|
|
|||
|
|
| API | 说明 |
|
|||
|
|
|-----|------|
|
|||
|
|
| `openXWebView(url, options?)` | 打开 WebView |
|
|||
|
|
| `setXWebViewController(controller)` | 设置全局 WebView 控制器 |
|
|||
|
|
| `getXWebViewConfig()` | 获取当前 WebView 配置 |
|
|||
|
|
| `XWebViewControl` | WebView 控制器类 |
|
|||
|
|
|
|||
|
|
### API Hooks
|
|||
|
|
|
|||
|
|
| API | 说明 |
|
|||
|
|
|-----|------|
|
|||
|
|
| `useApi(method, url, opts?)` | React hook:带 loading/error 的 API 调用 |
|
|||
|
|
| `usePageApi(method, url, opts?)` | React hook:分页 API |
|
|||
|
|
| `useRequest(opts?)` | React hook:通用请求 |
|
|||
|
|
| `setGlobalApiErrorHandler(handler)` | 设置全局 API 错误处理 |
|
|||
|
|
|
|||
|
|
### UI
|
|||
|
|
|
|||
|
|
| API | 说明 |
|
|||
|
|
|-----|------|
|
|||
|
|
| `showToast(msg)` | 显示 toast |
|
|||
|
|
| `showAlert(title, msg)` | 显示 alert |
|
|||
|
|
| `showConfirm(title, msg)` | 显示确认对话框 |
|
|||
|
|
| `ScaledImage` | 等比缩放图片组件 |
|
|||
|
|
|
|||
|
|
### 常量
|
|||
|
|
|
|||
|
|
| 常量 | 说明 |
|
|||
|
|
|------|------|
|
|||
|
|
| `DEFAULT_TENANT_PLATFORM_URL` | 默认公有平台地址 |
|
|||
|
|
| `DEFAULT_IM_WS_URL` | 默认 IM WebSocket 地址 |
|