微信公众平台NodeJs开发之路--消息处理
海南中立科技 | 2018-01-26 23:52:49 | 阅读:41154
关于微信开放平台的申请过程,请查看官方地址:http://mp.weixin.qq.com/cgi-bin/...-doc&lang=zh_CN现在贴出来如何处理消息的,XML的解析使用了node-xml这个模块,使用命令:npminstallnode-xml安装.001varcrypto=require("crypto");002varxml=require("./node-xml/lib/node-xml.js");003varmessageSender=require("./messageSender.js");004functionisLegel(signature,timestamp,nonce,token){005vararray=newArray();006array[0]=timestamp;007array[1]=nonce;008array[2]=token;009array.sort();010varhasher=crypto.createHash("sha1");011varmsg=array[0]+array[1]+array[2];012hasher.update(msg);013varmsg=hasher.digest('hex');014if(msg==signature){015returntrue;016}else{017returnfalse;018}019}020021functionprocessMessage(data,response){022varToUserName="";023varFromUserName="";024varCreateTime="";025varMsgType="";026varContent="";027varLocation_X="";028varLocation_Y="";029varScale=1;030varLabel="";031varPicUrl="";032varFuncFlag="";033034vartempName="";035varparse=newxml.SaxParser(function(cb){036cb.onStartElementNS(function(elem,attra,prefix,uri,namespaces){037tempName=elem;038});039040cb.onCharacters(function(chars){041chars=chars.replace(/(^s*)|(s*$)/g,"");042if(tempName=="CreateTime"){043CreateTime=chars;044}elseif(tempName=="Location_X"){045Location_X=cdata;046}elseif(tempName=="Location_Y"){047Location_Y=cdata;048}elseif(tempName=="Scale"){049Scale=cdata;050}051052053});054055cb.onCdata(function(cdata){056057if(tempName=="ToUserName"){058ToUserName=cdata;059}elseif(tempName=="FromUserName"){060FromUserName=cdata;061}elseif(tempName=="MsgType"){062MsgType=cdata;063}elseif(tempName=="Content"){064Content=cdata;065}elseif(tempName=="PicUrl"){066PicUrl=cdata;067}elseif(tempName=="Label"){068Label=cdata;069}070console.log("cdata:"+cdata);071});072073cb.onEndElementNS(function(elem,prefix,uri){074tempName="";075});076077cb.onEndDocument(function(){078console.log("onEndDocument");079tempName="";080vardate=newDate();081varyy=date.getYear();082varMM=date.getMonth()+1;083vardd=date.getDay();084varhh=date.getHours();085varmm=date.getMinutes();086varss=date.getSeconds();087varsss=date.getMilliseconds();088varresult=Date.UTC(yy,MM,dd,hh,mm,ss,sss);089varmsg="";090if(MsgType=="text"){091msg="谢谢关注,你说的是:"+Content;092}elseif(MsgType="location"){093msg="你所在的位置:经度:"+Location_X+"纬度:"+Location_Y;094}elseif(MsgType="image"){095msg="你发的图片是:"+PicUrl;096}097messageSender.sendTextMessage(FromUserName,ToUserName,CreateTime,msg,FuncFlag,response);098099});100});101parse.parseString(data);102}103module.exports.isLegel=isLegel;104module.exports.processMessage=processMessage;调用过程:viewsourceprint?01varapp=require("express").createServer();02varwx=require('./lib/wx');03varfs=require("fs");04app.get('/',function(req,response){0506fs.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});171819app.get('/wx',function(req,res){20varsignature=req.query.signature;21vartimestamp=req.query.timestamp;22varnonce=req.query.nonce;23varechostr=req.query.echostr;24varcheck=false;25check=wx.isLegel(signature,timestamp,nonce,token);//替换成你的token26if(check){27res.write(echostr);28}else{29res.write("errordata");30}31res.end();32});33app.post('/wx',function(req,res){34varresponse=res;35varformData="";36req.on("data",function(data){37formData+=data;38});39req.on("end",function(){40wx.processMessage(formData,response);41});42});43app.listen(3000);4445<DIV>464748</DIV>