$ npm i egg-wx --save
$ npm install xml2js egg-redis --save
// {app_root}/config/plugin.js
exports.wx = {
enable: true,
package: 'egg-wx',
};
// {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
async index() {
ctx.body = await app.wx.apiCommon.getAccessToken();
}- ApiCommon
微信基本接口,包含获取AccessToken
- ApiCustom
客服接口
- ApiJssdk
jssdk接口
- ApiMenu
自定义菜单接口
- ApiMessage
消息接口
- ApiMiniProgramSession
小程序获取用户Session接口
- ApiQRCode
qrcode接口
- ApiServer
服务器验证接口
- ApiShortUrl
ShortUrl接口
- ApiWebAuth
用户授权接口
微信基本接口,包含获取AccessToken
Kind: global class
- ApiCommon
- .getRedisCache() ⇒
object - .getAccessToken() ⇒
string - .getAccessTokenFromApi() ⇒
string
- .getRedisCache() ⇒
返回Redis对象
Kind: instance method of ApiCommon
Returns: object - redis对象
获取AccessToken,如果cache存在则直接读取chace
Kind: instance method of ApiCommon
Returns: string - AccessToken
通过微信接口获取AccessToken
Kind: instance method of ApiCommon
Returns: string - AccessToken
客服接口
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) ⇒
POST
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 |
|---|---|---|
string |
邮箱 | |
| nickname | string |
昵称 |
| password | string |
密码 |
修改客服账号
Kind: instance method of ApiCustom
Returns: * - 正常返回true
| Param | Type | Description |
|---|---|---|
string |
邮箱 | |
| nickname | string |
昵称 |
| password | string |
密码 |
删除客服账号
Kind: instance method of ApiCustom
Returns: * - 正常返回true
| Param | Type | Description |
|---|---|---|
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 |
jssdk接口
Kind: global class
- ApiJssdk
- .getRedisCache() ⇒
object - .getJsapiTicket() ⇒
object - .getJsapiTicketFromApi(accessToken) ⇒
object - .getSign(url, nonce, timestamp) ⇒
object - .getJsapiConfig(url) ⇒
object
- .getRedisCache() ⇒
返回Redis对象
Kind: instance method of ApiJssdk
Returns: object - redis对象
返回jsapi ticket对象,缓存读取
Kind: instance method of ApiJssdk
Returns: object - ticket对象
返回jsapi 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 |
时间戳 |
返回jssdk参数
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
- .createMenu(menuItems) ⇒
创建自定义菜单
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) ⇒
string - .checkSign(urlQuery, encryptText) ⇒
boolean - .getSign(timestamp, nonce, encryptText) ⇒
string
- .getRandomStr(len) ⇒
返回指定长度的随机字符串
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 |
最大整数 |
解析xml为对象
Kind: instance method of ApiMessage
Returns: object - 解析结果
| Param | Type | Description |
|---|---|---|
| data | string |
来自微信服务器的xml内容 |
解析Object为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 |
小程序跳转 |
校验请求签名
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 |
加密密文 |
小程序获取用户Session接口
小程序通过code获取用户Session
Kind: instance method of ApiMiniProgramSession
Returns: object - jscode2session数据
| Param | Type | Description |
|---|---|---|
| js_code | * |
授权code |
qrcode接口
生成二维码
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 |
上下文对象 |
ShortUrl接口
生成二维码
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
- .getUserWebAccessToken(code) ⇒
通过code获取用户网页授权access_token
Kind: instance method of ApiWebAuth
Returns: object - access_token授权数据
| Param | Type | Description |
|---|---|---|
| code | * |
授权code |
刷新用户网页授权access_token
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" |
返回地区语音 |