[![NPM version][npm-image]][npm-url]
npm install egg-wx[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][codecov-image]][codecov-url]
[![David deps][david-image]][david-url]
[![npm download][download-image]][download-url]
[npm-image]: https://img.shields.io/npm/v/egg-wx.svg?style=flat-square
[npm-url]: https://npmjs.org/package/egg-wx
[travis-image]: https://img.shields.io/travis/eggjs/egg-wx.svg?style=flat-square
[travis-url]: https://travis-ci.org/eggjs/egg-wx
[codecov-image]: https://img.shields.io/codecov/c/github/eggjs/egg-wx.svg?style=flat-square
[codecov-url]: https://codecov.io/github/eggjs/egg-wx?branch=master
[david-image]: https://img.shields.io/david/eggjs/egg-wx.svg?style=flat-square
[david-url]: https://david-dm.org/eggjs/egg-wx
[snyk-url]: https://snyk.io/test/npm/egg-wx
[download-image]: https://img.shields.io/npm/dm/egg-wx.svg?style=flat-square
[download-url]: https://npmjs.org/package/egg-wx
```
$ npm i egg-wx --save
$ npm install xml2js egg-redis --save
``
// {app_root}/config/plugin.js
exports.wx = {
enable: true,
package: 'egg-wx',
};
`javascript
// {app_root}/config/config.default.js
config.bodyParser = {
extendTypes: {
text: [ 'application/xml', 'text/xml', 'text/html' ],
},
enableTypes: [ 'text' ],
};
config.wx = {
redisClient: '', // Set this if used Multi Clients
redisPrefix: 'egg-wx',
AppID: 'appid',
AppSecret: 'app_secret',
MiniAppID: 'mini_appid', // wechat mini program app id
MiniAppSecret: 'mini_app_secret', // wechat mini program app secret
token: 'egg-wx',
EncodingAESKey: 'EncodingAESKey',
};
`
see https://mp.weixin.qq.com/wiki for more detail
`javascript``
async index() {
ctx.body = await app.wx.apiCommon.getAccessToken();
}
微信基本接口,包含获取AccessToken
客服接口
jssdk接口
自定义菜单接口
消息接口
小程序获取用户Session接口
qrcode接口
服务器验证接口
ShortUrl接口
用户授权接口
Kind: global class
* ApiCommon
* .getRedisCache() ⇒ object
* .getAccessToken(type = 'wx') ⇒ string
* .getAccessTokenFromApi(type = 'wx') ⇒ string
Kind: instance method of ApiCommon
Returns: object - redis对象
Kind: instance method of ApiCommon
Returns: string - AccessToken
| Param | Type | Description |
| --- | --- | --- |
| type | string | 类型 默认wx-公众号,mini-小程序 |
Kind: instance method of ApiCommon
Returns: string - AccessToken
| Param | Type | Description |
| --- | --- | --- |
| type | string | 类型 默认wx-公众号,mini-小程序 |
Kind: global class
* ApiCustom
.post(data, type) ⇒ \
.addCustomservice(email, nickname, password) ⇒ \
.updateCustomservice(email, nickname, password) ⇒ \
.deleteCustomservice(email) ⇒ \
.getCustomserviceList() ⇒ \
.sendText(openid, content) ⇒ \
.sendImage(openid, mediaID) ⇒ \
.sendVoice(openid, mediaID) ⇒ \
.sendVideo(openid, mediaID, thumbMediaID, title, description) ⇒ \
.sendMusic(openid, title, description, musicurl, hqmusicurl, thumbMediaID) ⇒ \
.sendNews(openid, articles) ⇒ \
.sendMpNews(openid, mediaID) ⇒ \
.sendWxCard(openid, cardID) ⇒ \
Kind: instance method of ApiCustom
Returns: \* - 返回信息
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| data | object | | post数据 |
| type | string | "msg" | 请求类型 msg: 消息类请求 |
Kind: instance method of ApiCustom
Returns: \* - 正常返回true
| Param | Type | Description |
| --- | --- | --- |
| email | string | 邮箱 |
| nickname | string | 昵称 |
| password | string | 密码 |
Kind: instance method of ApiCustom
Returns: \* - 正常返回true
| Param | Type | Description |
| --- | --- | --- |
| email | string | 邮箱 |
| nickname | string | 昵称 |
| password | string | 密码 |
Kind: instance method of ApiCustom
Returns: \* - 正常返回true
| Param | Type | Description |
| --- | --- | --- |
| email | string | 邮箱 |
Kind: instance method of ApiCustom
Returns: \* - 正常返回true
Kind: instance method of ApiCustom
Returns: \* - 正常返回true
| Param | Type | Description |
| --- | --- | --- |
| openid | string | 用户Openid |
| content | string | 内容 |
Kind: instance method of ApiCustom
Returns: \* - 正常返回true
| Param | Type | Description |
| --- | --- | --- |
| openid | string | 用户Openid |
| mediaID | string | 媒体ID |
Kind: instance method of ApiCustom
Returns: \* - 正常返回true
| Param | Type | Description |
| --- | --- | --- |
| openid | string | 用户Openid |
| mediaID | string | 媒体ID |
Kind: instance method of ApiCustom
Returns: \* - 正常返回true
| Param | Type | Description |
| --- | --- | --- |
| openid | string | 用户Openid |
| mediaID | string | 视频媒体ID |
| thumbMediaID | string | 视频媒体缩略图ID |
| title | string | 标题 |
| description | string | 描述 |
Kind: instance method of ApiCustom
Returns: \* - 正常返回true
| Param | Type | Description |
| --- | --- | --- |
| openid | string | 用户Openid |
| title | string | 标题 |
| description | string | 描述 |
| musicurl | string | 音乐地址 |
| hqmusicurl | string | HQ音乐地址 |
| thumbMediaID | string | 缩略图ID |
Kind: instance method of ApiCustom
Returns: \* - 正常返回true
| Param | Type | Description |
| --- | --- | --- |
| openid | string | 用户Openid |
| articles | array | 图文消息列表格式为: [ { "title": "Happy Day", "description": "Is Really A Happy Day", "url": "URL", "picurl": "PIC_URL" } ] |
Kind: instance method of ApiCustom
Returns: \* - 正常返回true
| Param | Type | Description |
| --- | --- | --- |
| openid | string | 用户Openid |
| mediaID | string | 图文消息ID |
Kind: instance method of ApiCustom
Returns: \* - 正常返回true
| Param | Type | Description |
| --- | --- | --- |
| openid | string | 用户Openid |
| cardID | string | 卡券ID |
Kind: global class
* ApiJssdk
* .getRedisCache() ⇒ object
* .getJsapiTicket() ⇒ object
* .getJsapiTicketFromApi(accessToken) ⇒ object
* .getSign(url, nonce, timestamp) ⇒ object
* .getJsapiConfig(url) ⇒ object
Kind: instance method of ApiJssdk
Returns: object - redis对象
Kind: instance method of ApiJssdk
Returns: object - ticket对象
Kind: instance method of ApiJssdk
Returns: object - ticket对象
| Param | Type | Description |
| --- | --- | --- |
| accessToken | string | access-token |
Kind: instance method of ApiJssdk
Returns: object - 返回签名
| Param | Type | Description |
| --- | --- | --- |
| url | string | 授权网站 |
| nonce | string | 随机字符 |
| timestamp | string | 时间戳 |
Kind: instance method of ApiJssdk
Returns: object - 前端jssdk配置参数对象 {appId, timestamp, nonceStr, signature}
| Param | Type | Description |
| --- | --- | --- |
| url | string | 授权网站 |
Kind: global class
* ApiMenu
* .createMenu(menuItems) ⇒ boolean
* .getMenu() ⇒ object
* .deleteMenu() ⇒ boolean
Kind: instance method of ApiMenu
Returns: boolean - 创建结果
| Param | Type | Description |
| --- | --- | --- |
| menuItems | object | 微信公众平台自定义菜单格式对象,详见微信接口文档 |
Kind: instance method of ApiMenu
Returns: object - 菜单结构
Kind: instance method of ApiMenu
Returns: boolean - 结果
Kind: global class
* ApiMessage
* .getRandomStr(len) ⇒ string
* .getIntervalRandom(min, max) ⇒ integer
* .getObjectFromXml(data) ⇒ object
* .getXmlFromObject(data) ⇒ object
* .messageEncrypt(msg) ⇒ string
* .messageDecrypt(msg) ⇒ string
* .sendTemplateMessage(openid, template_id, data, jumpUrl, miniprogram = null, type = 'wx') ⇒ string
* .sendSubscribeMessage(openid, template_id, data, page = null) ⇒ string
* .checkSign(urlQuery, encryptText) ⇒ boolean
* .getSign(timestamp, nonce, encryptText) ⇒ string
Kind: instance method of ApiMessage
Returns: string - 随机字符串
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| len | integer | 16 | 随机字符长度 |
Kind: instance method of ApiMessage
Returns: integer - 随机整数
| Param | Type | Description |
| --- | --- | --- |
| min | integer | 最小整数 |
| max | integer | 最大整数 |
Kind: instance method of ApiMessage
Returns: object - 解析结果
| Param | Type | Description |
| --- | --- | --- |
| data | string | 来自微信服务器的xml内容 |
Kind: instance method of ApiMessage
Returns: object - 解析结果
| Param | Type | Description |
| --- | --- | --- |
| data | string | Object内容 |
Kind: instance method of ApiMessage
Returns: string - 密文
| Param | Type | Description |
| --- | --- | --- |
| msg | string | 信息文本 |
Kind: instance method of ApiMessage
Returns: string - 解密文本
| Param | Type | Description |
| --- | --- | --- |
| msg | string | 密文文本 |
Kind: instance method of ApiMessage
Returns: string - 发送结果
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| openid | string | | 接收openid |
| template_id | string | | 模板ID |
| data | string | | 模板数据 |
| jumpUrl | string | | 跳转链接 |
| miniprogram | string | null | 小程序跳转 |
| type | string | 类型 默认wx-公众号,mini-小程序 |
Kind: instance method of ApiMessage
Returns: string - 发送结果
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| openid | string | | 接收openid |
| template_id | string | | 模板ID |
| data | string | | 模板数据 |
| page | string | | 跳转页面 |
Kind: instance method of ApiMessage
Returns: boolean - 校验结果
| Param | Type | Description |
| --- | --- | --- |
| urlQuery | object | ctx.query |
| encryptText | string | 加密密文 |
Kind: instance method of ApiMessage
Returns: string - 解密结果
| Param | Type | Description |
| --- | --- | --- |
| timestamp | string/integer | unix(10)时间戳 |
| nonce | string | 随机字符串 |
| encryptText | string | 加密密文 |
Kind: instance method of ApiMiniProgramSession
Returns: object - jscode2session数据
| Param | Type | Description |
| --- | --- | --- |
| js_code | \* | 授权code |
Kind: instance method of ApiQRCode
Returns: object - 二维码值
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| type | string | | 二维码类型 |
| sceneValue | string/integer | | 场景值 |
| expire_seconds | integer | 30 | 超时时间 |
Kind: instance method of ApiServer
Returns: boolean/string - 校验结果
| Param | Type | Description |
| --- | --- | --- |
| ctx | object | 上下文对象 |
Kind: instance method of ApiShortUrl
Returns: object - 短连接
| Param | Type | Description |
| --- | --- | --- |
| longurl | string | 长连接 |
Kind: global class
* ApiWebAuth
* .getUserWebAccessToken(code) ⇒ object
* .refreshUserWebAccessToken(refresh_token) ⇒ object
* .getUserWebInfo(access_token, openid, lang) ⇒ object
Kind: instance method of ApiWebAuth
Returns: object - access_token授权数据
| Param | Type | Description |
| --- | --- | --- |
| code | \* | 授权code |
Kind: instance method of ApiWebAuth
Returns: object - access_token授权数据
| Param | Type | Description |
| --- | --- | --- |
| refresh_token | string | 刷新token |
Kind: instance method of ApiWebAuth
Returns: object - 用户信息对象
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| access_token | string | | 用户网页授权access_token |
| openid | string | | 用户openid |
| lang | string | "zh_CN" | 返回地区语音 |