midwayjs-art-wechat
v0.0.1
Published
基于 node-easywechat 封装的 Midway.js 微信开发组件
Maintainers
Readme
midwayjs-art-wechat
介绍
基于 node-easywechat 封装的 Midway.js 微信开发组件,专为 Midway.js 框架设计。重写了缓存机制,完美集成 Midway 的缓存管理器,提供统一的微信服务端开发能力。
支持微信公众号、小程序、开放平台、企业微信等全套微信开发场景,适合快速集成微信服务端功能。
特性
- 🚀 基于
node-easywechat3.7+ 版本,API 稳定可靠 - 🔧 深度集成 Midway.js 框架,使用
@midwayjs/cache-manager作为缓存层 - 📦 支持多种微信开发场景:公众号、小程序、开放平台、企业微信等
- 🛡️ TypeScript 支持,类型安全
- ⚡ 单例模式,性能优化
- 🔄 自动 AccessToken 管理和刷新
支持的功能模块
OfficialAccount- 微信公众号MiniApp- 微信小程序OpenPlatform- 微信开放平台Work- 企业微信OpenWork- 企业微信开放平台Pay- 微信支付(不推荐,建议使用官方 SDK)
安装
npm install midwayjs-art-wechat node-easywechat
# 或
yarn add midwayjs-art-wechat node-easywechat配置
在 src/config/config.default.ts 中添加微信配置:
export const wechat = {
// 公众号配置
OfficialAccount: {
app_id: '你的公众号 AppID',
secret: '你的公众号 AppSecret',
token: '你的公众号 Token',
aes_key: '你的公众号 EncodingAESKey',
oauth: {
scope: 'snsapi_userinfo', // 网页授权类型
redirect: 'https://your-domain.com/oauth/callback', // 授权回调地址
},
use_stable_access_token: true,
},
// 小程序配置
MiniApp: {
app_id: '你的小程序 AppID',
secret: '你的小程序 AppSecret',
},
// 开放平台配置
OpenPlatform: {
app_id: '你的开放平台 AppID',
secret: '你的开放平台 AppSecret',
token: '你的开放平台 Token',
aes_key: '你的开放平台 EncodingAESKey',
},
// 企业微信配置
Work: {
corp_id: '你的企业微信 CorpID',
secret: '你的企业微信应用 Secret',
agent_id: '你的企业微信应用 AgentID',
},
};基本用法
1. 注入服务
import { Inject, Controller, Get } from '@midwayjs/core';
import { WechatService } from 'midwayjs-art-wechat';
@Controller('/wechat')
export class WechatController {
@Inject()
wechatService: WechatService;
}2. 获取公众号 AccessToken
@Get('/token')
async getAccessToken() {
// 获取公众号实例
const app = await this.wechatService.OfficialAccount();
// 获取 AccessToken
const accessToken = await app.getAccessToken();
const token = await accessToken.getToken();
return { token };
}3. 公众号消息回调处理
import { All, Inject, Controller } from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
@Controller()
export class WechatCallbackController {
@Inject()
ctx: Context;
@Inject()
wechatService: WechatService;
@All('/wechat/callback')
async callback() {
// 获取公众号实例
const app = await this.wechatService.OfficialAccount();
// 创建请求对象
const request = await this.wechatService.createRequest(this.ctx.req);
app.setRequest(request);
// 获取服务器实例
const server = app.getServer();
// 处理消息
server.with(async message => {
console.log('收到消息:', message);
if (message.MsgType === 'text') {
return `你说的是: ${message.Content}`;
}
if (message.MsgType === 'event') {
if (message.Event === 'subscribe') {
return '欢迎关注!';
}
}
return '收到消息了';
});
// 返回响应
const response = await server.serve();
this.ctx.type = response.getHeader('content-type');
this.ctx.body = response.getBody();
}
}4. 小程序登录
@Get('/miniapp/login')
async miniappLogin(@Query('code') code: string) {
// 获取小程序实例
const app = await this.wechatService.MiniApp();
// 通过 code 获取 session
const auth = app.getAuth();
const session = await auth.session(code);
return session;
}5. 网页授权
@Get('/oauth/redirect')
async oauthRedirect() {
const app = await this.wechatService.OfficialAccount();
const oauth = app.getOAuth();
// 生成授权链接
const url = oauth.redirect('https://your-domain.com/oauth/callback');
return { url };
}
@Get('/oauth/callback')
async oauthCallback(@Query('code') code: string) {
const app = await this.wechatService.OfficialAccount();
const oauth = app.getOAuth();
// 通过 code 获取用户信息
const user = await oauth.userFromCode(code);
return user;
}API 方法
WechatService
OfficialAccount(config?)- 获取公众号实例MiniApp(config?)- 获取小程序实例OpenPlatform(config?)- 获取开放平台实例Work(config?)- 获取企业微信实例OpenWork(config?)- 获取企业微信开放平台实例Pay(config?)- 获取支付实例(不推荐)createRequest(req)- 创建请求对象,用于消息回调处理
所有方法都支持传入自定义配置覆盖默认配置。
高级用法
自定义配置
// 使用自定义配置
const customApp = await this.wechatService.OfficialAccount({
app_id: 'custom_app_id',
secret: 'custom_secret',
// ... 其他配置
});缓存管理
组件自动使用 Midway 的缓存管理器,无需手动处理 AccessToken 的缓存和刷新。
更多文档
详细的 API 文档和使用方法请参考:
许可证
MIT License
更新日志
v1.0.0 (2024-01-01)
- 初始版本
- 支持公众号、小程序、开放平台、企业微信
- 集成 Midway.js 缓存管理器
- TypeScript 支持
