# xluni-utils

> uniapp框架api集成，包括http请求等等

Latest version **2.2.0** (published 2024-03-28) · ISC license · 0 weekly downloads

## Install

```sh
npm install xluni-utils
pnpm add xluni-utils
yarn add xluni-utils
bun add xluni-utils
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.2.0 |
| Published | 2024-03-28 |
| First published | 2023-08-24 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 15 |
| Unpacked size | 105.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | xl |
| Maintainers | xiawanhua |

## Links

- npm: https://www.npmjs.com/package/xluni-utils
- npm.io page: https://npm.io/package/xluni-utils

## Dependencies (15)

- [vue](https://npm.io/package/vue.md) ^3.2.45
- [vue-i18n](https://npm.io/package/vue-i18n.md) ^9.1.9
- [@dcloudio/uni-h5](https://npm.io/package/@dcloudio/uni-h5.md) 3.0.0-3081220230817001
- [@dcloudio/uni-app](https://npm.io/package/@dcloudio/uni-app.md) 3.0.0-3081220230817001
- [@dcloudio/uni-mp-jd](https://npm.io/package/@dcloudio/uni-mp-jd.md) 3.0.0-3081220230817001
- [@dcloudio/uni-mp-qq](https://npm.io/package/@dcloudio/uni-mp-qq.md) 3.0.0-3081220230817001
- [@dcloudio/uni-mp-lark](https://npm.io/package/@dcloudio/uni-mp-lark.md) 3.0.0-3081220230817001
- [@dcloudio/uni-app-plus](https://npm.io/package/@dcloudio/uni-app-plus.md) 3.0.0-3081220230817001
- [@dcloudio/uni-mp-baidu](https://npm.io/package/@dcloudio/uni-mp-baidu.md) 3.0.0-3081220230817001
- [@dcloudio/uni-mp-alipay](https://npm.io/package/@dcloudio/uni-mp-alipay.md) 3.0.0-3081220230817001
- [@dcloudio/uni-mp-weixin](https://npm.io/package/@dcloudio/uni-mp-weixin.md) 3.0.0-3081220230817001
- [@dcloudio/uni-components](https://npm.io/package/@dcloudio/uni-components.md) 3.0.0-3081220230817001
- [@dcloudio/uni-mp-toutiao](https://npm.io/package/@dcloudio/uni-mp-toutiao.md) 3.0.0-3081220230817001
- [@dcloudio/uni-mp-kuaishou](https://npm.io/package/@dcloudio/uni-mp-kuaishou.md) 3.0.0-3081220230817001
- [@dcloudio/uni-quickapp-webview](https://npm.io/package/@dcloudio/uni-quickapp-webview.md) 3.0.0-3081220230817001

## Recent versions

- 2.2.0 (latest) — 2024-03-28
- 2.0.1 — 2023-10-13
- 2.0.0 — 2023-10-13
- 1.1.0 — 2023-08-29
- 1.0.0 — 2023-08-24

## README

# xluni-utils

uniapp 框架的工具插件

## 安装

```shell
pnpm i xluni-utils
```

## http

在 `utils` 新建 `request.ts` 文件,如下

```typescript
import { http } from "xluni-utils";

const defHttp = http.create({
  baseUrl: "", // 接口根地址
});

// 请求拦截器 ，可以加token，返回false则中断请求
defHttp.interceptor.request = (config: any) => {
  // config.header.token = "1234";

  return config;
};

// 响应拦截器,返回所有的响应数据，return的值为false则会触发Promise.reject()
defHttp.interceptor.response = (response: any) => {
  console.log(response);
  return response;
};

export default defHttp;
```

### 类型列表

- CreateConfig

| 属性            | 类型    | 默认值                               | 可选值          | 备注                              |
| --------------- | ------- | ------------------------------------ | --------------- | --------------------------------- |
| baseUrl         | string  | --                                   | --              | 接口根地址                        |
| timeout         | number  | 60000                                | --              | 请求超时的时间，单位 ms           |
| method          | string  | GET                                  | POST/PUT/DELETE | 请求方式                          |
| header          | object  | {"content-type": "application/json"} | --              | 请求头                            |
| responseType    | string  | text                                 | arraybuffer     | 设置响应的数据类型                |
| sslVerify       | boolean | true                                 | false           | 验证 ssl 证书                     |
| withCredentials | boolean | false                                | true            | 跨域请求时是否携带凭证（cookies） |
| firstIpv4       | boolean | false                                | true            | DNS 解析时优先使用 ipv4           |

## 路由

### toPage(url,data,events)

保留当前页面，跳转到应用内的某个页面，使用 `backTo` 可以返回到原页面。

- `url` 必填，需要跳转的应用内非 tabBar 的页面的路径

- `data` 非必填，页面参数

- `events` 非必填，页面间通信,参考地址:https://uniapp.dcloud.net.cn/api/router.html#navigateto

### toTab(url)

跳转到 tabBar 页面，并关闭其他所有非 tabBar 页面。

- `url` 必填，需要跳转的应用内 tabBar 的页面的路径

### backTo(delta)

关闭当前页面，返回上一页面或多级页面。可通过 getCurrentPages() 获取当前的页面栈，决定需要返回几层。

- `delta` 非必填，返回的页面数，必须大于 0 的整数，默认为 1

### closeTo(url,data)

关闭当前页面，跳转到应用内的某个页面。

- `url` 必填，需要跳转的应用内非 tabBar 的页面的路径
- `data` 非必填，页面参数

### closeAllTo

关闭所有页面，打开到应用内的某个页面。

- `url` 必填，需要跳转的应用内页面路径
- `url` 非必填，页面参数，如果跳转的页面路径是 tabBar 页面则不能带参数

## 数据缓存

- `setStorage` 保存数据

- `getStorage` 获取数据

- `removeStorage` 删除数据

## 类型判断

- `typeOf` 类型判断，返回类型的小写

- `checkType` 类型检查，返回布尔值

- `isNumber` 数值类型

- `isString` 字符类型

- `isArray` 数组类型

- `isFunction` 函数类型

- `isObject` object 类型

---
_Source: https://npm.io/package/xluni-utils · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
