Giter VIP home page Giter VIP logo

think-wechat's Introduction

Think-wechat

npm status build status dependency status coverage status

微信中间件,基于 node-webot/wechat,支持 thinkJS 3.0

Getting start

$ npm install think-wechat

配置 middleware

  • 编辑 config/middleware.js
const path = require('path');
const wechat = require('think-wechat')
const isDev = think.env === 'development';

module.exports = [
  [...]
  {
    handle: wechat,
    match: '/wechat',
    options: {
      token: <微信开发者token>,
      appid: <appID>,
      encodingAESKey: <消息对称加密串>,
      checkSignature: true // 可选,默认为true。由于微信公众平台接口调试工具在明文模式下不发送签名,所以如要使用该测试工具,请将其设置为false
    }
  },
  {
    handle: 'payload',
    options: {}
  },
  [...]
];

注意:think-wechat 必须要在 payload 中间件前面加载,它会代替 payload 处理微信发过来的 post 请求中的数据。

  • 根据 match 配置,增加对应的 controller 和 action

支持的 action 包括:textAction、imageAction、voiceAction、videoAction、shortvideoAction、locationAction、linkAction、eventAction、deviceTextAction、deviceEventAction。

const Base = require('./base.js');
const DEFULT_AUTO_REPLY = '功能正在开发中~';

export default class extends Base {
  /**
   * index action
   * @return {Promise} []
   */
  indexAction(){
    // 验证开发者服务器
    // 这里只是演示,所以没做签名校验,实际上应该要根据微信要求进行签名校验
    const echostr = this.get('echostr');
    return this.end(echostr);
  }
  // textAction(){
      // 发送文本消息
  //  const {Content} = this.post();    
  //  this.success('你发送给我的是:' + Content.trim());
  // }
  async textAction(){
  	const {Content} = this.post();
  	const music = await myService.search(Content.trim());
  	
  	if(music){
  		const {title, description, url} = music;
  		this.success({
  			type: 'music',
        	content: {
	        title,
	        description,
	        musicUrl: url,
	        hqMusicUrl: url,
	        thumbMediaId: "thisThumbMediaId"
	      }			
  		});
  	}else{
  		this.fail('你所找的歌曲不存在');
  	}
  }
  eventAction(){
    const message = this.post();
    
    this.success(JSON.stringify(message));
  }
  __call(){
    this.success(DEFULT_AUTO_REPLY);
  }
}
  • 登录你的微信服务号,将微信开发者的接口URL配置为:
http://your_hostname/wechat

this.success 支持返回所有 node-webot/wechat 支持的格式。

License

MIT

think-wechat's People

Contributors

akira-cn avatar liyanlong avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

think-wechat's Issues

Unhandled rejection Error: PREVENT_NEXT_PROCESS

通过微信给服务端发送消息后,服务端报错

Unhandled rejection Error: PREVENT_NEXT_PROCESS
    at Object.think.prevent (/home/project/weixin-demo/node_modules/thinkjs/lib/core/think.js:511:13)
    at _class.end (/home/project/weixin-demo/node_modules/thinkjs/lib/controller/base.js:426:18)
    at _class.indexAction (/home/project/weixin-demo/app/wechat/controller/index.js:21:14)
    at _class._callee$ (/home/project/weixin-demo/node_modules/thinkjs/lib/core/base.js:78:44)
    at tryCatch (/home/project/weixin-demo/node_modules/thinkjs/node_modules/babel-runtime/regenerator/runtime.js:88:40)
    at GeneratorFunctionPrototype.invoke [as _invoke] (/home/project/weixin-demo/node_modules/thinkjs/node_modules/babel-runtime/regenerator/runtime.js:341:22)
    at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (/home/project/weixin-demo/node_modules/thinkjs/node_modules/babel-runtime/regenerator/runtime.js:121:21)
    at step (/home/project/weixin-demo/node_modules/thinkjs/node_modules/babel-runtime/helpers/asyncToGenerator.js:17:30)
    at /home/project/weixin-demo/node_modules/thinkjs/node_modules/babel-runtime/helpers/asyncToGenerator.js:35:7
    at Promise._execute (/home/project/weixin-demo/node_modules/thinkjs/node_modules/bluebird/js/release/debuggability.js:180:9)
    at Promise._resolveFromExecutor (/home/project/weixin-demo/node_modules/thinkjs/node_modules/bluebird/js/release/promise.js:460:18)
    at new Promise (/home/project/weixin-demo/node_modules/thinkjs/node_modules/bluebird/js/release/promise.js:76:14)
    at _class.<anonymous> (/home/project/weixin-demo/node_modules/thinkjs/node_modules/babel-runtime/helpers/asyncToGenerator.js:14:12)
    at _class.invoke (/home/project/weixin-demo/node_modules/thinkjs/lib/core/base.js:102:18)
    at _class.action (/home/project/weixin-demo/node_modules/thinkjs/lib/core/http_base.js:152:25)
    at _class.execAction (/home/project/weixin-demo/node_modules/thinkjs/lib/core/app.js:188:19)

[2016-03-02 14:43:57] [HTTP] POST /wechat/?signature=bd70de24ea2404e87a45883a4c405e334a0d42e6&timestamp=1456901036&nonce=1532463979 200 13ms

感觉这个中间件没起作用,没有进入处理文字消息的action

textAction(){
        var message = this.post();
        console.log(message);
        var msg = message.Content.trim();
        this.reply('测试成功:'+msg);
    }

能否提供个完整的配置流程

您好,我的thinkjs版本是2.2,折腾了半天也没有搞定,问题如下:
第一张图是middleware的配置:
image
第二张图是controller里的配置:
image
我这样配置了以后为什么微信发信息,还是在wechat而不是在wechat/text接收到?

公众号显示“获取中...”数秒才消失

Thinkjs 3.0 使用过程中,

async eventAction(){ const message = this.post(); think.logger.info('eventAction:'); think.logger.info(message); return this.success(123); }

在公众号点击菜单事件,文本直接立刻返回123,但是公众号仍会显示“获取中...”数秒才消失,请问如何优化

控制台一直输出/wechat<>weixin/wechat

按照官网的例子,控制台一直输出以下信息:
/wechat<>weixin/wechat
You have triggered an unhandledRejection, you may have forgotten to catch a Promise rejection:
Error: PREVENT_NEXT_PROCESS

nodejs : 7.4
thinkjs : 2.2.16

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.