egg-wechat-alll

0.1.4 • Public • Published

egg-wechat-all

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Fork from

qdechochen/egg-wechat-all: A plugin for egg.js, provides a simple way to integrate with wechat.

Install

$ npm i egg-wechat-alll --save

Usage

// {app_root}/config/plugin.js
exports.wechatAll = {
  enable: true,
  package: 'egg-wechat-alll',
};
 

egg-redis is required.

Configuration

// {app_root}/config/config.default.js
exports.wechatAll = {
  appid: '',
  appsecret: '',
  token: '',
  encodingAESKey: '',
  payment: {
    partnerKey: '',
    mchId: '',
    notifyUrl: '',
    pfx: '',
  },
  modules: {
    message: true,  // enable or disable co-wechat
    api: true,  // enable or disable co-wechat-api
    oauth: true,  // enable or disable co-wechat-oauth
    payment: true,  // enable or disable co-wechat-payment
  },
;

see config/config.default.js for more details.

How

app.wechat.messageMiddleware //co-wechat middleware
app.wechat.api  // co-wechat-api
app.wechat.oauth  // co-wechat-oauth
app.wechat.payment  // co-wechat-payment

For more details, please refer to the following links.

co-wechat

co-wechat-api

co-wechat-oauth

co-wechat-payment

Example

'use strict';
const Controller = require('egg').Controller;
 
module.exports = app => {
  class WechatController extends Controller {
    async oauth() {
      const token = await app.wechat.oauth.getAccessToken(this.ctx.query.code);
      this.ctx.body = JSON.stringify({
        query: this.ctx.query,
        token,
        user: await app.wechat.api.getUser(token.data.openid),
      }, 2, 2);
    }
  }
 
  WechatController.prototype.index = app.wechat.messageMiddleware(async (message, ctx) => {
    ctx.app.wechat.api.sendNews(message.FromUserName, [{
      title: 'OAuth test',
      description: 'Please tap this message to start oauth test',
      url: app.wechat.oauth.getAuthorizeURL('[URL(Route to WechatController.oauth)]', 'MY_STATE', 'snsapi_userinfo'),
      picurl: '[PIC_URL]',
    }]);
    return `Received your message: ${message.Content}`;
  });
 
  return WechatController;
};
 
 

Questions & Suggestions

Please open an issue here.

License

MIT

Package Sidebar

Install

npm i egg-wechat-alll

Weekly Downloads

1

Version

0.1.4

License

MIT

Unpacked Size

13.4 kB

Total Files

9

Last publish

Collaborators

  • funme