# @bnfe/wx-utils

> wx-web

Latest version **1.2.7** (published 2026-01-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install @bnfe/wx-utils
pnpm add @bnfe/wx-utils
yarn add @bnfe/wx-utils
bun add @bnfe/wx-utils
```

## Health

**Score 55/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 1.2.7 |
| Published | 2026-01-09 |
| First published | 2023-10-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 37.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | zyh941109, whh2828, dujunhui15, topb25, zmlman |

## Links

- npm: https://www.npmjs.com/package/@bnfe/wx-utils
- npm.io page: https://npm.io/package/@bnfe/wx-utils

## Dependencies (1)

- [axios](https://npm.io/package/axios.md) ^1.6.0

## Recent versions

- 1.2.7 (latest) — 2026-01-09
- 1.2.6 — 2024-01-16
- 1.2.5 — 2023-11-22
- 1.2.4 — 2023-11-21
- 1.2.3 — 2023-11-16
- 1.2.2 — 2023-11-16
- 1.2.1 — 2023-11-14
- 1.2.0 — 2023-10-21
- 1.1.1 — 2023-10-08
- 1.1.0 — 2023-10-07
- 1.0.0 — 2023-10-07

## README

## @bnfe/wx-utils

这是一个集成wx和企微jssdk

## 安装

```sh
pnpm install @bnfe/wx-utils -S
```

## 引入

```js
import wxSDK from "@bnfe/wx-utils";
```

## 使用

### 公众号

> 参考文档

[公众号授权流程](https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html)

[开放平台授权流程](https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Before_Develop/Official_Accounts/official_account_website_authorization.html)

[公众号JS-SDK](https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html)

|   `事件名称`   |            `说明`            | `回调参数` |
| :------------: | :--------------------------: | :--------: |
|     wxAuth     |        发起公众号授权        |     -      |
|   thirdLogin   | 开放平台代公众号发起网页授权 |     -      |
| injectWxConfig |     微信注入权限验证配置     |  授权结果  |

```js
/**
 * 发起公众号授权
 * @param {string} env 环境变量，可选值为 'dev' 或 'prod'
 * @param {string} appId 应用的appId
 * @param {string} scope 需要授权的范围，可选值为'snsapi_userinfo'或'snsapi_base'，默认snsapi_base
 * @return
 */
wxSDK.wxAuth(env, appId, scope);
```

```js
/**
 * 开放平台代公众号发起网页授权
 * @param {string} env 环境变量
 * @param {string} appId 应用的appId
 * @param {string} scope 需要授权的范围,可选值为'snsapi_userinfo'或'snsapi_base'，默认snsapi_base
 * @return
 */
wxSDK.thirdLogin(env, appId, scope);
```

```js
/**
 * 微信注入权限验证配置
 * @param {string} env 环境变量，可选值为 'dev' 或 'prod'
 * @param {string} appId 企微应用的appId
 * @param {string[]} jsApiList 需要注入的权限
 */
wxSDK.injectWxConfig(env, appId, jsApiList);
```

### 企业微信

> 参考文档

[构造企业微信授权链接](https://developer.work.weixin.qq.com/document/path/91022)

[企业微信JS-SDK](https://developer.work.weixin.qq.com/document/path/90513)

|      `事件名称`       |        `说明`        |  `回调参数`  |
| :-------------------: | :------------------: | :----------: |
|      workWxAuth       |   发起企业微信授权   |      -       |
|    getWorkWxUserId    |  获取企业微信用户id  |  企微用户id  |
|   getWorkWxAuthLink   |   获取企微授权链接   |   授权链接   |
|  injectWorkWxConfig   | 企信注入权限验证配置 |   授权结果   |
| getCurExternalContact |   获取外部联系人id   | 外部联系人id |

```js
/**
 * 发起企业微信授权
 * @param {string} env 环境变量，可选值为 'dev' 或 'prod'
 * @param {string} appId 企微的corpId
 * @param {string} agentId 企微应用的appId
 */
wxSDK.workWxAuth(env, appId, agentId);
```

```js
/**
 * 企微授权获取企微用户id
 * @param {string} env 环境变量，可选值为 'dev' 或 'prod'
 * @param {string} appId 企微的corpId
 * @param {string} agentId 企微应用的appId
 */
wxSDK.getWorkWxUserId(env, appId, agentId);
```

```js
/**
 * 企微授权链接（一般在设置应用主页、配置到聊天工具栏、消息推送时使用，可避免页面重复跳转）
 * @param {string} env 环境变量，可选值为 'dev' 或 'prod'
 * @param {string} appId 企微的corpId
 * @param {string} agentId 企微应用的appId
 */
wxSDK.getWorkWxAuthLink(env, appId, agentId);
```

```js
/***
 * 企微注入
 * @param {string} env 环境变量，可选值为 'dev' 或 'prod'
 * @param {string} appId 企微corpId
 * @param {string} agentId 企微应用的id 例如：1000144
 * @param {string[]} jsApiList 需要注入的权限
 * @returns
 * */
wxSDK.injectWorkWxConfig(env, appId, agentId, jsApiList);
```

```js
/**
 * 获取外部联系人id（getContext获取入口环境，getCurExternalContact获取外部联系人id）
 * 从某些入口进入页面时，可调用该接口获取当前客户的userid（也即external_userid)，目前支持的入口有联系人详情页、外部单聊工具栏
 * @param {string} env 环境变量
 * @param {string} appId 企微corpId
 * @param {string} agentId 企微应用的id 例如：1000144
 * @returns
 * */
wxSDK.getCurExternalContact(env, appId, agentId);
```

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