README
egg-wx
Install
$ npm i egg-wx --save
$ npm install xml2js egg-redis --save
Usage
// {app_root}/config/plugin.js
exports.wx = {
enable: true,
package: 'egg-wx',
};
Configuration
// {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
Example
async index() {
ctx.body = await app.wx.apiCommon.getAccessToken();
}
Classes
- ApiCommon
微信基本接口,包含获取AccessToken
- ApiCustom
客服接口
- ApiJssdk
jssdk接口
- ApiMenu
自定义菜单接口
- ApiMessage
消息接口
- ApiMiniProgramSession
小程序获取用户Session接口
- ApiQRCode
qrcode接口
- ApiServer
服务器验证接口
- ApiShortUrl
ShortUrl接口
- ApiWebAuth
用户授权接口
ApiCommon
微信基本接口,包含获取AccessToken
Kind: global class
- ApiCommon
- .getRedisCache() ⇒
object - .getAccessToken(type = 'wx') ⇒
string - .getAccessTokenFromApi(type = 'wx') ⇒
string
- .getRedisCache() ⇒
apiCommon.getRedisCache() ⇒ object
返回Redis对象
Kind: instance method of ApiCommon
Returns: object - redis对象
apiCommon.getAccessToken(type = 'wx') ⇒ string
获取AccessToken,如果cache存在则直接读取chace
Kind: instance method of ApiCommon
Returns: string - AccessToken
| Param | Type | Description |
|---|---|---|
| type | string |
类型 默认wx-公众号,mini-小程序 |
apiCommon.getAccessTokenFromApi(type = 'wx') ⇒ string
通过微信接口获取AccessToken
Kind: instance method of ApiCommon
Returns: string - AccessToken
| Param | Type | Description |
|---|---|---|
| type | string |
类型 默认wx-公众号,mini-小程序 |
ApiCustom
客服接口
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) ⇒
*
- .post(data, type) ⇒
apiCustom.post(data, type) ⇒ *
POST
Kind: instance method of ApiCustom
Returns: * - 返回信息
| Param | Type | Default | Description |
|---|---|---|---|
| data | object |
post数据 | |
| type | string |
"msg" |
请求类型 msg: 消息类请求 |
apiCustom.addCustomservice(email, nickname, password) ⇒ *
添加客服账号
Kind: instance method of ApiCustom
Returns: * - 正常返回true
| Param | Type | Description |
|---|---|---|
string |
邮箱 | |
| nickname | string |
昵称 |
| password | string |
密码 |
apiCustom.updateCustomservice(email, nickname, password) ⇒ *
修改客服账号
Kind: instance method of ApiCustom
Returns: * - 正常返回true
| Param | Type | Description |
|---|---|---|
string |
邮箱 | |
| nickname | string |
昵称 |
| password | string |
密码 |
apiCustom.deleteCustomservice(email) ⇒ *
删除客服账号
Kind: instance method of ApiCustom
Returns: * - 正常返回true
| Param | Type | Description |
|---|---|---|
string |
邮箱 |
apiCustom.getCustomserviceList() ⇒ *
获取客服列表
Kind: instance method of ApiCustom
Returns: * - 正常返回true
apiCustom.sendText(openid, content) ⇒ *
发送文字消息
Kind: instance method of ApiCustom
Returns: * - 正常返回true
| Param | Type | Description |
|---|---|---|
| openid | string |
用户Openid |
| content | string |
内容 |
apiCustom.sendImage(openid, mediaID) ⇒ *
发送图片消息
Kind: instance method of ApiCustom
Returns: * - 正常返回true
| Param | Type | Description |
|---|---|---|
| openid | string |
用户Openid |
| mediaID | string |
媒体ID |
apiCustom.sendVoice(openid, mediaID) ⇒ *
发送声音消息
Kind: instance method of ApiCustom
Returns: * - 正常返回true
| Param | Type | Description |
|---|---|---|
| openid | string |
用户Openid |
| mediaID | string |
媒体ID |
apiCustom.sendVideo(openid, mediaID, thumbMediaID, title, description) ⇒ *
发送视频消息
Kind: instance method of ApiCustom
Returns: * - 正常返回true
| Param | Type | Description |
|---|---|---|
| openid | string |
用户Openid |
| mediaID | string |
视频媒体ID |
| thumbMediaID | string |
视频媒体缩略图ID |
| title | string |
标题 |
| description | string |
描述 |
apiCustom.sendMusic(openid, title, description, musicurl, hqmusicurl, thumbMediaID) ⇒ *
发送音乐消息
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 |
apiCustom.sendNews(openid, articles) ⇒ *
发送图文消息
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" } ] |
apiCustom.sendMpNews(openid, mediaID) ⇒ *
发送图文消息
Kind: instance method of ApiCustom
Returns: * - 正常返回true
| Param | Type | Description |
|---|---|---|
| openid | string |
用户Openid |
| mediaID | string |
图文消息ID |
apiCustom.sendWxCard(openid, cardID) ⇒ *
发送卡券
Kind: instance method of ApiCustom
Returns: * - 正常返回true
| Param | Type | Description |
|---|---|---|
| openid | string |
用户Openid |
| cardID | string |
卡券ID |
ApiJssdk
jssdk接口
Kind: global class
- ApiJssdk
- .getRedisCache() ⇒
object - .getJsapiTicket() ⇒
object - .getJsapiTicketFromApi(accessToken) ⇒
object - .getSign(url, nonce, timestamp) ⇒
object - .getJsapiConfig(url) ⇒
object
- .getRedisCache() ⇒
apiJssdk.getRedisCache() ⇒ object
返回Redis对象
Kind: instance method of ApiJssdk
Returns: object - redis对象
apiJssdk.getJsapiTicket() ⇒ object
返回jsapi ticket对象,缓存读取
Kind: instance method of ApiJssdk
Returns: object - ticket对象
apiJssdk.getJsapiTicketFromApi(accessToken) ⇒ object
返回jsapi ticket对象,接口返回
Kind: instance method of ApiJssdk
Returns: object - ticket对象
| Param | Type | Description |
|---|---|---|
| accessToken | string |
access-token |
apiJssdk.getSign(url, nonce, timestamp) ⇒ object
返回签名
Kind: instance method of ApiJssdk
Returns: object - 返回签名
| Param | Type | Description |
|---|---|---|
| url | string |
授权网站 |
| nonce | string |
随机字符 |
| timestamp | string |
时间戳 |
apiJssdk.getJsapiConfig(url) ⇒ object
返回jssdk参数
Kind: instance method of ApiJssdk
Returns: object - 前端jssdk配置参数对象 {appId, timestamp, nonceStr, signature}
| Param | Type | Description |
|---|---|---|
| url | string |
授权网站 |
ApiMenu
自定义菜单接口
Kind: global class
- ApiMenu
- .createMenu(menuItems) ⇒
boolean - .getMenu() ⇒
object - .deleteMenu() ⇒
boolean
- .createMenu(menuItems) ⇒
apiMenu.createMenu(menuItems) ⇒ boolean
创建自定义菜单
Kind: instance method of ApiMenu
Returns: boolean - 创建结果
| Param | Type | Description |
|---|---|---|
| menuItems | object |
微信公众平台自定义菜单格式对象,详见微信接口文档 |
apiMenu.getMenu() ⇒ object
获取当前自定义菜单结构
Kind: instance method of ApiMenu
Returns: object - 菜单结构
apiMenu.deleteMenu() ⇒ boolean
删除当前自定义菜单
Kind: instance method of ApiMenu
Returns: boolean - 结果
ApiMessage
消息接口
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
- .getRandomStr(len) ⇒
apiMessage.getRandomStr(len) ⇒ string
返回指定长度的随机字符串
Kind: instance method of ApiMessage
Returns: string - 随机字符串
| Param | Type | Default | Description |
|---|---|---|---|
| len | integer |
16 |
随机字符长度 |
apiMessage.getIntervalRandom(min, max) ⇒ integer
返回指定整数区间的随机数
Kind: instance method of ApiMessage
Returns: integer - 随机整数
| Param | Type | Description |
|---|---|---|
| min | integer |
最小整数 |
| max | integer |
最大整数 |
apiMessage.getObjectFromXml(data) ⇒ object
解析xml为对象
Kind: instance method of ApiMessage
Returns: object - 解析结果
| Param | Type | Description |
|---|---|---|
| data | string |
来自微信服务器的xml内容 |
apiMessage.getXmlFromObject(data) ⇒ object
解析Object为xml
Kind: instance method of ApiMessage
Returns: object - 解析结果
| Param | Type | Description |
|---|---|---|
| data | string |
Object内容 |
apiMessage.messageEncrypt(msg) ⇒ string
返回微信消息加密文本
Kind: instance method of ApiMessage
Returns: string - 密文
| Param | Type | Description |
|---|---|---|
| msg | string |
信息文本 |
apiMessage.messageDecrypt(msg) ⇒ string
返回微信消息解密数据
Kind: instance method of ApiMessage
Returns: string - 解密文本
| Param | Type | Description |
|---|---|---|
| msg | string |
密文文本 |
apiMessage.sendTemplateMessage(openid, template_id, data, jumpUrl, miniprogram = null, type = 'wx') ⇒ 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-小程序 |
apiMessage.sendSubscribeMessage(openid, template_id, data, page = null) ⇒ string
发送订阅消息
Kind: instance method of ApiMessage
Returns: string - 发送结果
| Param | Type | Default | Description |
|---|---|---|---|
| openid | string |
接收openid | |
| template_id | string |
模板ID | |
| data | string |
模板数据 | |
| page | string |
跳转页面 |
apiMessage.checkSign(urlQuery, encryptText) ⇒ boolean
校验请求签名
Kind: instance method of ApiMessage
Returns: boolean - 校验结果
| Param | Type | Description |
|---|---|---|
| urlQuery | object |
ctx.query |
| encryptText | string |
加密密文 |
apiMessage.getSign(timestamp, nonce, encryptText) ⇒ string
生成签名
Kind: instance method of ApiMessage
Returns: string - 解密结果
| Param | Type | Description |
|---|---|---|
| timestamp | string/integer |
unix(10)时间戳 |
| nonce | string |
随机字符串 |
| encryptText | string |
加密密文 |
ApiMiniProgramSession
小程序获取用户Session接口
apiMiniProgramSession.getSession(js_code) ⇒ object
小程序通过code获取用户Session
Kind: instance method of ApiMiniProgramSession
Returns: object - jscode2session数据
| Param | Type | Description |
|---|---|---|
| js_code | * |
授权code |
ApiQRCode
qrcode接口
apiQRCode.createQRCode(type, sceneValue, expire_seconds) ⇒ object
生成二维码
Kind: instance method of ApiQRCode
Returns: object - 二维码值
| Param | Type | Default | Description |
|---|---|---|---|
| type | string |
二维码类型 | |
| sceneValue | string/integer |
场景值 | |
| expire_seconds | integer |
30 |
超时时间 |
ApiServer
服务器验证接口
apiServer.checkServer(ctx) ⇒ boolean/string
验证消息的确来自微信服务器,微信公众平台接口配置校验
Kind: instance method of ApiServer
Returns: boolean/string - 校验结果
| Param | Type | Description |
|---|---|---|
| ctx | object |
上下文对象 |
ApiShortUrl
ShortUrl接口
apiShortUrl.createShortUrl(longurl) ⇒ object
生成二维码
Kind: instance method of ApiShortUrl
Returns: object - 短连接
| Param | Type | Description |
|---|---|---|
| longurl | string |
长连接 |
ApiWebAuth
用户授权接口
Kind: global class
- ApiWebAuth
- .getUserWebAccessToken(code) ⇒
object - .refreshUserWebAccessToken(refresh_token) ⇒
object - .getUserWebInfo(access_token, openid, lang) ⇒
object
- .getUserWebAccessToken(code) ⇒
apiWebAuth.getUserWebAccessToken(code) ⇒ object
通过code获取用户网页授权access_token
Kind: instance method of ApiWebAuth
Returns: object - access_token授权数据
| Param | Type | Description |
|---|---|---|
| code | * |
授权code |
apiWebAuth.refreshUserWebAccessToken(refresh_token) ⇒ object
刷新用户网页授权access_token
Kind: instance method of ApiWebAuth
Returns: object - access_token授权数据
| Param | Type | Description |
|---|---|---|
| refresh_token | string |
刷新token |
apiWebAuth.getUserWebInfo(access_token, openid, lang) ⇒ object
获取用户信息
Kind: instance method of ApiWebAuth
Returns: object - 用户信息对象
| Param | Type | Default | Description |
|---|---|---|---|
| access_token | string |
用户网页授权access_token | |
| openid | string |
用户openid | |
| lang | string |
"zh_CN" |
返回地区语音 |