r-util-js/packages/uni-app/README.md

107 lines
2.7 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# @r-utils/uni-app
仅用于 uni-app 项目的工具包封装请求、上传、NFC、蓝牙、打印、Vue3 Hooks 和常用 uni API 辅助方法。
## 适用范围
- 适用于 uni-app 项目(含 uni 运行时能力)。
- 适用于需要复用请求、上传、蓝牙、NFC、打印、Vue3 Hooks 等能力的业务项目。
## 不适用范围
- 不适用于普通 Web Vue2/Vue3 项目。
- 不适用于没有 `uni` 运行时的纯 Node.js 环境。
## 安装
```bash
pnpm add @r-utils/uni-app
```
## 导入方式
### 推荐:根入口导入
大多数场景推荐从根入口导入,路径简单,使用心智负担更低。
```ts
import { Request, showToast, toPromise, upload, Printer, useShare } from "@r-utils/uni-app";
```
### 兼容:子路径导入
如果你希望模块边界更清晰,也可以使用子路径导入。两种方式都支持。
```ts
import { Request } from "@r-utils/uni-app/request";
import { showToast, toPromise } from "@r-utils/uni-app/uni-helper";
import { upload } from "@r-utils/uni-app/upload";
import { Printer } from "@r-utils/uni-app/printer";
import { nfcScan } from "@r-utils/uni-app/nfc";
import { BluetoothUtils } from "@r-utils/uni-app/bluetooth-utils";
import { useShare, usePageTab, useTopSticky } from "@r-utils/uni-app/vue3";
```
## 导出模块
| 子路径 | 说明 |
| --- | --- |
| `@r-utils/uni-app/request` | 类 axios 的 `uni.request` 封装 |
| `@r-utils/uni-app/uni-helper` | 常用 uni API 辅助函数 |
| `@r-utils/uni-app/upload` | 上传相关工具 |
| `@r-utils/uni-app/printer` | 打印机相关工具 |
| `@r-utils/uni-app/nfc` | NFC 相关工具 |
| `@r-utils/uni-app/bluetooth-utils` | 蓝牙相关工具 |
| `@r-utils/uni-app/vue3` | uni-app Vue3 Hooks |
## 使用示例
### 请求封装
```ts
import { Request } from "@r-utils/uni-app";
const request = new Request({
baseURL: "https://api.example.com",
});
const res = await request.get("/users", {
data: { pageNum: 1, pageSize: 10 },
});
```
### 使用 showToast
```ts
import { showToast } from "@r-utils/uni-app";
showToast("保存成功", "success");
```
### 子路径导入请求模块
```ts
import { Request } from "@r-utils/uni-app/request";
const request = new Request({
baseURL: "https://api.example.com",
});
```
### 使用 Vue3 Hooks
```ts
import { useShare } from "@r-utils/uni-app/vue3";
const { getShareOptions } = useShare({
title: "页面标题",
query: { id: "1" },
});
```
## 注意事项
- 推荐优先使用根入口导入;如果需要更精确的模块边界,也可以使用子路径导入。
- 本包依赖 uni-app 运行时能力,需在真实 uni-app 环境中验证平台相关功能。
- NFC、蓝牙、打印功能通常依赖具体平台和设备能力建议按目标端单独测试。