微信公众平台NodeJs开发之路--消息处理

浏览:484 发布日期:2016/01/26 分类:前端开发
现在贴出来如何处理消息的,xml的解析使用了node-xml这个模块 ,使用命令:npm install node-xml 安装.

001var crypto=require("crypto");
002var xml=require("./node-xml/lib/node-xml.js");
003var messageSender=require("./messageSender.js");
004function isLegel(signature,timestamp,nonce,token){
005var array=new Array();
006array[0]=timestamp;
007array[1]=nonce;
008array[2]=token;
009array.sort();
010var hasher=crypto.createHash("sha1");
011var msg=array[0]+array[1]+array[2];
012hasher.update(msg);
013var msg=hasher.digest('hex');
014if(msg==signature){
015return true;
016}else{
017return false;
018}
019}
020
021function processMessage(data,response){
022var ToUserName="";
023var FromUserName="";
024var CreateTime="";
025var MsgType="";
026var Content="";
027var Location_X="";
028var Location_Y="";
029var Scale=1;
030var Label="";
031var PicUrl="";
032var FuncFlag="";
033
034var tempName="";
035var parse=new xml.SaxParser(function(cb){
036cb.onstartElementNS(function(elem,attra,prefix,uri,namespaces){
037tempName=elem;
038});
039
040cb.onCharacters(function(chars){
041chars=chars.replace(/(^s*)|(s*$)/g, "");
042if(tempName=="CreateTime"){
043CreateTime=chars;
044}else if(tempName=="Location_X"){
045Location_X=cdata;
046}else if(tempName=="Location_Y"){
047Location_Y=cdata;
048}else if(tempName=="Scale"){
049Scale=cdata;
050}
051
052
053});
054
055cb.onCdata(function(cdata){
056
057if(tempName=="ToUserName"){
058ToUserName=cdata;
059}else if(tempName=="FromUserName"){
060FromUserName=cdata;
061}else if(tempName=="MsgType"){
062MsgType=cdata;
063}else if(tempName=="Content"){
064Content=cdata;
065}else if(tempName=="PicUrl"){
066PicUrl=cdata;
067}else if(tempName=="Label"){
068Label=cdata;
069}
070console.log("cdata:"+cdata);
071});
072
073cb.onEndElementNS(function(elem,prefix,uri){
074tempName="";
075});
076
077cb.onEndDocument(function(){
078console.log("onEndDocument");
079tempName="";
080var date=new Date();
081var yy=date.getYear();
082var MM=date.getMonth() + 1;
083var dd=date.getDay();
084var hh=date.getHours();
085var mm=date.getMinutes();
086var ss=date.getSeconds();
087var sss=date.getMilliseconds();
088var result=Date.UTC(yy,MM,dd,hh,mm,ss,sss);
089var msg="";
090if(MsgType=="text"){
091msg="谢谢关注,你说的是:"+Content;
092}else if (MsgType="location"){
093msg="你所在的位置: 经度:"+Location_X+"纬度:"+Location_Y;
094}else if (MsgType="image"){
095msg="你发的图片是:"+PicUrl;
096}
097messageSender.sendTextMessage(FromUserName,ToUserName,CreateTime,msg,FuncFlag,response);
098
099});
100});
101parse.parseString(data);
102}
103module.exports.isLegel=isLegel;
104module.exports.processMessage=processMessage;
调用过程:

view source print?

01var app=require("express").createServer();
02var wx=require('./lib/wx');
03var fs=require("fs");
04app.get('/',function(req,response){
05
06fs.readFile("./qrCode.jpg","binary",function(err,file){
07if(err){
08response.writeHead(500,{"Content-Type":"text/plain"});
09response.write(err+"n");
10}else{
11response.writeHead(200,{"Content-Type":"image/jpg"});
12response.write(file,"binary");
13}
14response.end();
15});
16});
17
18
19app.get('/wx',function(req,res){
20var signature=req.query.signature;
21var timestamp=req.query.timestamp;
22var nonce=req.query.nonce;
23var echostr=req.query.echostr;
24var check=false;
25check=wx.isLegel(signature,timestamp,nonce,token);//替换成你的token
26if(check){
27res.write(echostr);
28}else{
29res.write("error data");
30}
31res.end();
32});
33app.post('/wx',function(req,res){
34var response=res;
35var formData="";
36req.on("data",function(data){
37formData+=data;
38});
39req.on("end",function(){
40wx.processMessage(formData,response);
41});
42});
43app.listen(3000);
44
45<DIV>
46
47
48</DIV>
最佳答案
评论( 相关
后面还有条评论,点击查看>>