EasyWechat for Node.js
注:2.x分支针对 EasyWechat 的 5.x版本(由于4.x与5.x的结构基本一致,就不升级大版本了)。若您需要 EasyWechat 的 3.x版本,请切换到 1.x 分支。
EasyWechat 是一个由 安正超 大神用 PHP 开发的开源的微信非官方 SDK。其功能强大,使用方便,个人一直很喜欢,所以近日将其在 Node.js 上实现。本人会尽量还原其配置项以及接口的调用方式,但毕竟语言环境不同,具体的实现方式会有些许差别,还请各位开发者见谅。
注:虽然也使用了 EasyWechat 这个名称,但是和 安正超 大神没有任何关系,请各位开发者不要因使用本包产生的疑惑而去打扰大神,如有疑问请在本项目中提 issue,谢谢~
安装
npm install -S node-easywechat2
使用说明
绝大部分API都可以根据 EasyWechat 的文档 来使用。小部分(如获取请求相关数据、返回响应数据、支付证书等)的操作,由于语言环境的不同,会有不同处理。具体可以查看 node-easywechat-demo 以及下方的自定义模块说明 。如果仍有疑问,请提issue,谢谢~
let officialAccount = new EasyWechat.Factory.OfficialAccount({
});
let miniProgram = new EasyWechat.Factory.MiniProgram({
});
let payment = new EasyWechat.Factory.Payment({
});
let openPlatform = new EasyWechat.Factory.OpenPlatform({
});
let openPlatform = new EasyWechat.Factory.Work({
});
let app = EasyWechat.Factory.getInstance('OficialAccount', {
});
配置项示例
{
app_id: '',
secret: '',
token: '',
aes_key: '',
file_cache: {
path: './cache/',
fileMode: 0o666,
ext: '.cache'
},
oauth: {
scope: 'snsapi_userinfo',
redirect: 'http://node-easywechat.hpyer.cn/wxlogin/callback'
}
}
{
app_id: 'xxx',
mch_id: 'your-mch-id',
key: 'key-for-signature',
notify_url: 'http://xxx.com/pay/notify',
cert_path: 'path/to/your/cert.pfx',
}
{
app_id: 'your-app-id',
secret: 'your-app-secret'
}
{
corp_id: 'your-corp-id',
agent_id: 'your-agent-id',
secret: 'your-app-secret'
}
模块支持情况
自定义模块(模块替换)使用方法
使用自定义模块的主要目的,是为了消除不同框架、不同软件包的使用方法所带来的差异,以提升 node-easywechat 的使用效率。下面就来说说几个主要模块的自定义方法。
日志模块(log)
const Easywechat = require('node-easywechat2');
let officialAccount = new Easywechat.Factory.OfficialAccount({
...
});
const myLogger = () => {
return function() {
let args = arguments;
args[0] = 'myLogger: ' + args[0];
return console.log.apply(null, arguments);
}
}
officialAccount.rebind('log', myLogger);
请求模块(request)
let request = new EasyWechat.Http.Request(req);
let request = new EasyWechat.Http.Request(req, content);
officialAccount.rebind('request', request);
缓存模块(cache)
const Redis = require('redis');
const BlueBird = require('bluebird');
BlueBird.promisifyAll(Redis.RedisClient.prototype);
BlueBird.promisifyAll(Redis.Multi.prototype);
class RedisCache extends EasyWechat.CacheInterface
{
constructor (options)
{
super();
this.$client = null;
try {
this.$client = Redis.createClient(options);
}
catch (e) {
console.log('无法创建Redis客户端', e);
}
}
async get(id)
{
if (!this.$client) return false;
let content = null;
try {
content = JSON.parse(await this.$client.getAsync(id));
}
catch (e) {
console.log('获取Redis缓存失败', id, e);
return false;
}
return content;
}
async has(id)
{
if (!this.$client) return false;
try {
let res = await this.$client.existsAsync(id);
return res == 1;
}
catch (e) {
return false;
}
}
async set(id, data = null, lifeTime = 0)
{
if (!this.$client) return false;
try {
if (lifeTime > 0) {
await this.$client.setAsync(id, JSON.stringify(data), 'EX', lifeTime);
}
else {
await this.$client.setAsync(id, JSON.stringify(data));
}
}
catch (e) {
console.log('设置Redis缓存失败', id, data, e);
return false;
}
return true;
}
async delete(id)
{
if (!this.$client) return false;
try {
let res = await this.$client.delAsync(id);
return true;
}
catch (e) {
return false;
}
}
}
let redisCache = new RedisCache(req);
officialAccount.rebind('cache', redisCache);