Giter VIP home page Giter VIP logo

think-wechat's Introduction

Think-wechat

微信中间件,同时支持 think.js 1.x 和 2.x,基于 [https://github.com/node-webot/wechat]

Getting start

$ npm install think-wechat

在 think.js 2.x 中使用

  • 编辑 config/hook.js 增加一个 hook
export default {
  payload_parse: ['prepend', 'parse_wechat'], //在前面追加解析 xml
}
  • 编辑 bootstrap/hook.js 加载 hook
var wechatMiddleware = require('think-wechat');

think.middleware('parse_wechat', wechatMiddleware({
        wechat:{
            token: '微信公众号token',
            appid: '微信公众号ID',
            encodingAESKey: '消息安全加密串'
        },
    }));
  • 增加对应的controller和action,默认为wechat
'use strict';

import Base from './base.js';

const DEFULT_AUTO_REPLY = '功能正在开发中~';

export default class extends Base {
  /**
   * index action
   * @return {Promise} []
   */
  indexAction(){
    let echostr = this.get('echostr');
    return this.end(echostr);
  }
  reply(message){
    this.http.res.reply(message);
  }
  textAction(){
    var message = this.post();
    var msg = message.Content.trim();
    this.reply('测试成功:'+msg);
  }
  eventAction(){
    var message = this.post();
    this.reply(JSON.stringify(message));
  }
  __call(){
    this.reply(DEFULT_AUTO_REPLY);
  }
}
  • 登录你的微信服务号,将微信开发者的接口ULR配置为:
http://your_hostname/wechat

高级配置

var wechatMiddleware = require('think-wechat');

think.middleware('parse_wechat', wechatMiddleware({
        pathname: 'wechat',    //默认,可配置为其他路径,与公众号对应的服务器URL设置一致
        route: {
          text: 'wechat/text', //文字转发
          image: 'wechat/image', //图片转发
          voice: 'wechat/voice', //语音转发
          video: 'wechat/video', //视频转发
          shortvideo: 'wechat/shortvideo', //小视频转发
          location: 'wechat/location', //地理位置转发
          link: 'wechat/link', //链接转发
          event: 'wechat/event', //推送事件转发
        },
        wechat:{
            token: '微信公众号token',
            appid: '微信公众号ID',
            encodingAESKey: '消息安全加密串'
        },
    }));

在 think.js 1.x 中使用

  • 配置支持用户tag,在 App/Conf/config.js 中设置 app_tag_on: true
module.exports = {
  //配置项: 配置值
  port: 8360, //监听的端口
  app_tag_on: true
};
  • 编辑 App/Conf/tag.js 文件内容
/**
 * 解析提交的json数据
 * @param  {[type]} http [description]
 * @return {[type]}      [description]
 */
var wechatMiddleware = require('think-wechat'); 

module.exports = {
  form_parse: [false, wechatMiddleware({
        wechat:{
            token: '微信公众号token',
            appid: '微信公众号ID',
            encodingAESKey: '消息安全加密串'
        },
    })],
};
  • 增加对应的Controller和Action,默认为WechatController
module.exports = Controller("Home/BaseController", function(){
  "use strict";
  return {
    indexAction: function(){
      var echostr = this.get('echostr');
      return this.end(echostr);
    },
    textAction: function(){
      var message = this.post();
      this.http.res.reply('消息接收成功!');
    },
    __call: function(action){
      var message = this.post();
      this.http.res.reply(JSON.stringify(message));
    }
  };
});

License

MIT

think-wechat's People

Contributors

akira-cn avatar

Watchers

杜鹏 avatar  avatar

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.