源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

详解nodejs微信公众号开发——3.封装消息响应模块

  • 时间:2021-05-15 23:38 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:详解nodejs微信公众号开发——3.封装消息响应模块
上一篇文章:[url=http://www.1sucai.cn/article/110753.htm]nodejs微信公众号开发(2)自动回复[/url],实现了简单的关注回复。采用拼接字符串的形式,并不是很方便,这里我们将其封装承接口。 [b]1. ejs模板引擎[/b] 不使用拼接字符串的方式,那么模板引擎就是较好的选择。Nodejs开源模板的选择很多,程序中使用 [code]EJS[/code],有[code]Classic ASP/PHP/JSP[/code]的经验用起[code]EJS[/code]来的确可以很自然,也就是说,你能够在 [code]<%...%> [/code]块中安排 JavaScript 代码,利用最传统的方式 [code]<%=输出变量%>[/code](另外 <%-输出变量是不会对 & 等符号进行转义的). [b]2. heredoc [/b] 在php、python中都有heredoc方式的字符串定义方法,JavaScript也实现了heredoc模块,主要解决大量字符串拼接问题。 新建模板文件[code]tpl.js[/code]:
'use strict'

var ejs = require('ejs');
var heredoc = require('heredoc');

var tpl = heredoc(function(content){/*
  <xml>
    <ToUserName><![CDATA[<%= toUserName %>]]></ToUserName>
    <FromUserName><![CDATA[<%= fromUserName %>]]></FromUserName>
    <CreateTime><%= createTime%></CreateTime>
    <MsgType><![CDATA[<%= msgType %>]]></MsgType>
    <% if(msgType ==='text') { %>
      <Content><![CDATA[<%= content %>]]></Content>
    <% }else if(msgType ==='image'){ %>
      <Image>
        <MediaId><![CDATA[<%= content.mediaId %>]]></MediaId>
      </Image>
    <% }else if(msgType ==='voice'){ %>
      <Voice>
        <MediaId><![CDATA[<%= content.mediaId %>]]></MediaId>
      </Voice>
    <% } %>else if(msgType ==='video'){ %>
      <Video>
        <MediaId><![CDATA[<%= content.mediaId %>]]></MediaId>
        <Title><![CDATA[<%= content.title %>]]></Title>
        <Description><![CDATA[<%= content.description %>]]></Description>
      </Video>   
    <% } %>else if(msgType ==='music'){ %>
      <Music>
        <Title><![CDATA[<%= content.title %>]]></Title>
        <Description><![CDATA[<%= content.description %>]]></Description>
        <MusicUrl><![CDATA[<%= content.musicUrl %>]]></MusicUrl>
        <HQMusicUrl><![CDATA[<%= content.hqMusicUrl %>]]></HQMusicUrl>
        <ThumbMediaId><![CDATA[<%= content.thumbMediaId %>]]></ThumbMediaId>  
      </Music>
    <% } %>else if(msgType ==='news'){ %>
      <ArticleCount><%= content.length %></ArticleCount>
      <Articles>
        <% content.forEach(function(item){ %>
        <item>
          <Title><![CDATA[<%= item.title %>]]></Title> 
          <Description><![CDATA[<%= item.description %>]]></Description>
          <PicUrl><![CDATA[<%= item.picUrl %>]]></PicUrl>
          <Url><![CDATA[<%= item.url %>]]></Url>
        </item>
        <% }) %>
      </Articles>
    <% } %>  
  </xml>
*/});

var compiled = ejs.compiled(tpl);

exports = module.exports = {
  compiled:compiled
};

[b]3. 处理接收到的消息[/b] 修改[code]generator.js[/code]中之前直接回复消息的那部分代码,我们将处理回复内容的逻辑交给业务层,等其处理完毕,继续执行下面的代码,封装消息内容成xml并回复出去。
var message = util.formatMessage(content.xml);
    
this.weixin = message; //挂载消息

yield handler.call(this,next);  //转到业务层逻辑

wechat.replay.call(this); //真正回复

[b]4.业务层的处理逻辑[/b] [code]app.js[/code]里面中间件的使用方式修改为:
var weixin = require('./weixin');
...
app.use(wechat(config.wechat,weixin.reply)); 
[code]weixin.reply[/code]即[code]generator.js[/code]中的[code]handler[/code],我们将公众号业务成的逻辑都写在[code]weixin.js[/code]里面,如回复消息、将来的爬取电影网站信息、支付等。
exports.reply = function* (next){
  var message = this.weixin;

  if(message.magType === 'event'){
    if(message.Event === 'subscribe'){
      if(message.EventKey) console.log('扫描二维码关注:'+message.EventKey+' '+message.ticket);
      this.body = '终于等到你,还好我没放弃';
    }else if(message.Event === 'unsubscribe'){
      console.log(message.FromUserName +' 悄悄地走了...');
    }
  }else{
    //
  }

  yield next;
}
[b]5.回复消息[/b] 我们在Wechat原型链上增加[code]replay[/code]方法:
Wechat.prototype.replay = function(){
  var content = this.body;
  var message = this.weixin;

  var xml = util.tpl(content,message);

  this.status = 200;
  this.type = 'application/xml';
  this.body = xml;
}

这样实现了[code]wechat.replay.call(this); [/code]的回复消息功能。 [b]6.总结[/b] 上面代码已经基本实现了消息的封装,回复规则和回复内容写在业务层代码[code]weixin.js[/code]中,里面简单的实现了关注和取关的事件处理。 由于[code]koa[/code]框架是基于[code]ES6[/code],里面充斥了大量的[code]Promise[/code]、[code]genarator[/code]、[code]yield[/code]等内容,对[code]ES6[/code]不了解的,可以学习一下此篇文章:[url=http://www.1sucai.cn/article/88824.htm]ECMAScript6快速入手攻略[/url] 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部