npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

midwayjs-art-wechat

v0.0.1

Published

基于 node-easywechat 封装的 Midway.js 微信开发组件

Readme

midwayjs-art-wechat

介绍

基于 node-easywechat 封装的 Midway.js 微信开发组件,专为 Midway.js 框架设计。重写了缓存机制,完美集成 Midway 的缓存管理器,提供统一的微信服务端开发能力。

支持微信公众号、小程序、开放平台、企业微信等全套微信开发场景,适合快速集成微信服务端功能。

特性

  • 🚀 基于 node-easywechat 3.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 支持