Giter VIP home page Giter VIP logo

Comments (3)

ilaipi avatar ilaipi commented on June 11, 2024

我实现了这个。首先在每个公众号的开发者配置那,你配置的地址后面加个参数。比如你要监听的地址是:http://baidu.com,那就设置成:http://baidu.com?originId=gh_xxxxx,这个originId是公众号的原始id。然后,在服务端,初始化所有公众号api,

// accounts 是所有的公众号配置
forEach(accounts, ({ originId, appid, aes, token }) => {
    allApi[originId] = originWechat({
      appid: appid,
      encodingAESKey: aes,
      token: token
    });
  });

export const getApi = ({ originId }) => allApi[originId];

实现中间件:
middleware.js

import logger from 'bunyan';
import { getApi } from 'path/to/initial/all/accounts';

const wechat = () => {
  return async (ctx, next) => {
    const { originId } = ctx.request.query;
    const wechat = getApi({ originId });
    const fn = wechat.middleware;
    const api = fn.apply(wechat, [(message) => {
      logger.info({ message }, 'wechat push message');
      switch (message.MsgType) {
        case 'event':
          /* 延迟100ms执行,立即响应微信服务器,避免出现服务不可用的情况,以及重复推送事件的情况 */
          setTimeout(() => logger.info('=====hahah===='), 100);
          break;
        case 'text':
        case 'audio':
          break;
      }
      return 'hehe';
    }]);
    await api(ctx, next);
  };
};

export default wechat;

最后,在app.js中:

import wechat from 'path/to/middleware';

app.use(wechat());

from co-wechat.

JacksonTian avatar JacksonTian commented on June 11, 2024

#28 看看这个。

http://url/wechat?appid=1
http://url/wechat?appid=2

app.use(async function (ctx, next) {
  ctx.wx_token = getToken(ctx.query.appid);
  // or
  ctx.wx_cryptor = getCryptor(ctx.query.appid);
});
app.use(wechat(config).middleware(async function (message) {
  // TODO
}));

from co-wechat.

JacksonTian avatar JacksonTian commented on June 11, 2024

已经发布了。

from co-wechat.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.