wechater v1.0.11
Wechater 微信SDK
使用typescript, 结合微信提供的API,提供到一个完善到SDK,支持智能代码提醒、功能拓展。
导航🧭
Install
npm install wechater
QuickStart
koa 接入wechat
const KoaApplication = require('koa');
const Wechater = require('wechater');
const koa = new KoaApplication();
const wechat = new Wechater({
appId: 'wxAppId',
appSecret: 'wxAppSecret',
token: 'wxToken',
encodingAesKey: 'wxEncodingAesKey',
isDebug: true,
});
koa.use(wechat.accessWxKoa((reply, ctx, next) => {
reply.replyText('hello koa wechater');
}));
koa.listen(1718,()=>{console.info('Listen 1718')});
express 接入wechat
const Express = require('express');
const Wechater = require('wechater');
const wechat = new Wechater({
appId: 'wxAppId',
appSecret: 'wxAppSecret',
token: 'wxToken',
encodingAesKey: 'wxEncodingAesKey',
isDebug: true,
});
const app = new Express();
app.all('/', wechat.accessWxExpress((reply) => {
reply.replyText('hello express wechater');
}));
app.listen(1728, ()=>{console.info('Listen 1728')});
获取AccessToken 创建菜单接口
const Express = require('express');
const Wechater = require('wechater');
const bodyParser = require('body-parser')
let accessToken = '';
let expireTimestamp = 0;
const wechat = new Wechater({
appId: 'wxAppId',
appSecret: 'wxAppSecret',
accessTokenFunc: async()=>{
const nowTime = Math.floor((new Date().getTime())/1000);
if(nowTime >= expireTimestamp){
const data = await wechat.getAccessToken();
if(data.errcode){
throw new Error(`Get AccessToken Error, ${data.errmsg}`)
}
accessToken = data.access_token;
expireTimestamp = nowTime + data.expires_in - 120;
}
return accessToken;
}
});
const app = new Express();
app.use(bodyParser.json());
app.post('/menuCreate', async (req, res)=>{
const result = await wechat.menuCreate(req.body);
res.send(result)
});
app.listen(1728, ()=>{console.info('Listen 1728')});
模拟请求
POST
ip:1728/menuCreate
, 公众号菜单会设置成功{ "button":[ { "type":"click", "name":"今日歌曲", "key":"V1001_TODAY_MUSIC" }, { "name":"菜单", "sub_button":[ { "type":"view", "name":"搜索", "url":"http://www.soso.com/" }, { "type":"click", "name":"赞一下我们", "key":"V1001_GOOD" }] }] }
Expand 自定义拓展SDK
由于SDK与微信存在一段时间更新期,我们建议用户自定义拓展SDK, 以新建菜单接口为例
const Wechater = require('wechater');
class NewWechater extends Wechater{ constructor(options){ super(options); } menuCreate(data){ return this.request('cgi-bin/menu/create',{ method: 'post', data, }) } }
module.exports = NewWechater;
#### Config
interface Options { appId?: string; appSecret?: string; token?: string; encodingAesKey?: string; host?: string; isDebug?: boolean; accessTokenFunc?: () => Promise; }
### 常用功能
1. 登录
```js
const result = await wechater.jscode2session(js_code)
- 获取access_token
const result = await wechater.getAccessToken()
- 解析数据
const resuult = wechater.decryptData(sessionKey, encryptedData, iv)
- 请求数据,根据access_token
const result = await wechater.request('url', AxiosRequestConfig) //url 为除去baseUrl路由部分
公众号SDK
用户管理
user
查看用户详情
消息管理-接收与被动回复
使用
wechater.accessWxKoa
或wechater.accessWxExpress
中间件,获取到hanler, 使用handler进行消息接收与被动回复信息1.
replyText回复文本 2.
replyImage回复图片 3.
replyVoice回复语音 4.
replyVideo回复视频 5.
replyMusic回复音乐 6.
replyToCustomer转发到客服系统 7.
replyNews` 发送图文信息
小程序SDK
微信支付SDK
企业微信SDK
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago