# ts-wxsdk

> > 申请测试好，请移步：https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

Latest version **1.0.9** (published 2020-08-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install ts-wxsdk
pnpm add ts-wxsdk
yarn add ts-wxsdk
bun add ts-wxsdk
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.9 |
| Published | 2020-08-17 |
| First published | 2020-08-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 55.5 KB |
| Known vulnerabilities | 0 (+25 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 2 |
| Author | jimyan |
| Maintainers | jimyan |
| Keywords | wxapi, wxapi ts, wxapi typescript |

## Links

- npm: https://www.npmjs.com/package/ts-wxsdk
- Repository: https://github.com/JimYan/ts-wxsdk
- Homepage: https://github.com/JimYan/ts-wxsdk.git
- Issues: https://github.com/JimYan/ts-wxsdk/issues
- npm.io page: https://npm.io/package/ts-wxsdk

## Dependencies (5)

- [yaml](https://npm.io/package/yaml.md) ^1.10.0
- [axios](https://npm.io/package/axios.md) ^0.19.2
- [tunnel](https://npm.io/package/tunnel.md) ^0.0.6
- [xml-js](https://npm.io/package/xml-js.md) ^1.6.11
- [axios-https-proxy-fix](https://npm.io/package/axios-https-proxy-fix.md) ^0.17.1

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.0.9 (latest) — 2020-08-17
- 1.0.8 — 2020-08-14
- 1.0.7 — 2020-08-14
- 1.0.6 — 2020-08-14
- 1.0.5 — 2020-08-14
- 1.0.4 — 2020-08-14
- 1.0.3 — 2020-08-14
- 1.0.2 — 2020-08-14
- 1.0.1 — 2020-08-02
- 1.0.0 — 2020-08-01

## README

#微信公众号 API 接口

> 申请测试好，请移步：https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

[API 文档](https://jimyan.github.io/ts-wxsdk/)

## proxy

如果你的服务器需要通过代理服务器访问外网，需要在调用任何一个 API 之前设置代理，全局只需要设置一次。
如果服务器是直接连外网，那么忽略。
设置方法：

```typescript
import {setProxy} from 'ts-wxsdk';

setProxy({
    host: 'your proxy server host',
    port: 'your proxy server port',
});
```

## token

获取全局 accessToken 或 jsapi ticket：

```typescript
import {accessToken, ticket} from 'ts-wxsdk/token';

// 获取微信全局accesstoken，2小时有效期，建议定时刷新内部保存。
const token = await accessToken(appid, appsecret); // appid和appsecret在微信公众号管理后台获取

// 微信卡券api接口ticket或者jsapi ticket
const jsticket = await ticket(token, 'jsapi');
```

## userinfo

用户登录

```typescript
import {code2AccessToken, refreshAccessToken, userinfo, code2userinfo, code2Session} from 'ts-wxsdk/user';

// 微信公众号登录，使用code换用户的accessToken和openId
const accessToken = await code2AccessToken(appid, appsecret, code);
const userinfo = await userinfo(appid, openID);

// 小程序登录
const info = await code2Session(appid, appsecret, code);
```

## template

发送模板消息

```typescript
import {template} from 'ts-wxsdk/template';

template<{
    name: {
        value: string;
        color?: string;
    };
}>(accessToken, {
    touser: 'TO USER OPENID',
    template_id: '3W-7l84S25TMqdzJPN7jaqE5Fj451aGobsc1wMD7NXU',
    url: 'https://www.baidu.com/',
    topcolor: '#00ff00',
    data: {
        name: {
            value: 'name',
            color: '#173177',
        },
    },
});
```

## 解析微信事件通知

> 微信消息通知具体见：https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Receiving_standard_messages.html

此 API 是把通知过来的 XML 内容解析成 JSON 格式。todo: 加密消息解密（当前版本没有处理）

```typescript
import {parse} from 'ts-wxsdk/message';
const l = parse(wxSendMessage);
```

## 回复消息

> 具体见：https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Passive_user_reply_message.html

这个 API 负责把对应的消息内容组装成 XML 格式。

```typescript
import {text, image, video, voice, news} from 'ts-wxsdk/message.response';
const r = text({
    ToUserName: 'a',
    FromUserName: 'b',
    Content: 's',
});

const r = image({
    ToUserName: 'a',
    FromUserName: 'b',
    Image: {
        MediaId: 'b',
    },
});

const r = video({
    ToUserName: 'a',
    FromUserName: 'b',
    Video: {
        MediaId: '11',
        Title: 'hello video',
        Description: 'desc',
    },
});

const r = voice({
    ToUserName: 'a',
    FromUserName: 'b',
    Voice: {
        MediaId: 'voiceid',
    },
});

const r = news({
    ToUserName: 'a',
    FromUserName: 'b',
    ArticleCount: 2,
    Articles: {
        item: [
            {
                Title: 't1',
                Description: 'd1',
                PicUrl: 'pu1',
                Url: 'u1',
            },
            {
                Title: 't2',
                Description: 'd2',
                PicUrl: 'pu2',
                Url: 'u2',
            },
        ],
    },
});
```

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