node实现微信扫码群发消息《附上github代码》

2018-01-13 20:33:58来源:cnblogs.com作者:北风吹雪人点击

分享

本篇文章就是为大家介绍一下我是如何用node去实现扫码群发功能,源代码地址在最后面

获取登录二维码 -> 扫码登录服务端

         

首先介绍一下主要流程,并附上关键代码

1.获取UUID并请求二维码图片

  调用接口:https://login.wx.qq.com/jslogin

  返回数据:code为200表示成功,并返回uuid

function getUUID(){         var hreq = https.get('https://login.wx.qq.com/jslogin?appid=wx782c26e4c19acffb&redirect_uri=https%3A%2F%2Fwx.qq.com%2Fcgi-bin%2Fmmwebwx-bin%2Fwebwxnewloginpage&fun=new&lang=zh_CN&_=1508239448402',function(ress){          ress.setEncoding('utf-8');          var str = '';        ress.on('end',function(){            var regxp = new RegExp(/^""$/)            var uuid = str.split('"')[1];            console.log('uuid=='+uuid)            var img = '<img src="https://login.weixin.qq.com/qrcode/'+uuid + '"/>'            res.send(img)            getTicket(uuid)        });          ress.on('data',function(chunk){            str+=chunk;        });           });    }

2,通过uuid轮训获取ticket

  调用接口:https://login.wx.qq.com/cgi-bin/mmwebwx-bin/login

  如果返回code是200,表示用户扫码,返回的信息redirect_uri中有登录微信用的ticket等信息

function getTicket(uuid){        var hreq = https.get('https://login.wx.qq.com/cgi-bin/mmwebwx-bin/login?loginicon=true&uuid='+uuid+'&tip=0&r=-880061546&_='+Date.now(),function(ress){          ress.setEncoding('utf-8');          var str = '';        ress.on('end',function(){            console.log('请求ticket。。。。。。');            console.log(str);            var code = str.split(';')[0].split('=')[1];            console.log('code='+code);            if(code == 200){                if(str.match(/wx2.qq.com/) != null) {                    wx2 = "2";                    headers.Host = "wx2.qq.com";                    headers.Referer = "https://wx2.qq.com/"                }                var ticket = str.split('ticket=')[1].split('&uuid')[0];                console.log('ticket=' + ticket);                getPassTicket(ticket,uuid);            }else if(code == 408 || code==201){                getTicket(uuid);            }else{                console.log(str);                console.log('超时');            }                    });          ress.on('data',function(chunk){            str+=chunk;        });           });    }

3. 获取微信登录唯一标志信息wxsid、skey、pass_ticket

  调用接口:https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage

  返回信息wxsid、skey、pass_ticket,并且会把cookie种植上

  到这一步获取到的信息,在后面所有的请求将畅行无阻

function getPassTicket(ticket,uuid){        request.get({          url:'https://wx'+wx2+'.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage?ticket='+ticket+'&uuid='+uuid+'&lang=zh_CN&scan='+parseInt(Date.now())+'&fun=new&version=v2&lang=zh_CN',        }, function(error, response, body){            console.log('请求PassTicket------->>>>>>>>');            console.log(body);            var str = body.toString();        pass_ticket = str.split('<pass_ticket>')[1].split('</pass_ticket>')[0];        skey = str.split('<skey>')[1].split('</skey>')[0];        sid = str.split('<wxsid>')[1].split('</wxsid>')[0];        uin = str.split('<wxuin>')[1].split('</wxuin>')[0];        setCookie(response.headers['set-cookie'])        wxInit()        });    }

4.获取联系人列表,并群发消息

  联系人列表调用接口:https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetcontact

  返回所有用户联系人信息,每个用户都有此次登录中唯一的ID

 

  发送消息调用接口:https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxsendmsg

  如果BaseResponse.ErrorMsg为空,表示消息发送成功

function getAllUsers(){        request.get({            headers: headers,          url:'https://wx'+wx2+'.qq.com/cgi-bin/mmwebwx-bin/webwxgetcontact?lang=zh_CN&pass_ticket='+pass_ticket+'&r='+Date.now()+'&seq=0&skey='+skey        }, function(error, response, body){            console.log('getAllUsers..........')            console.log(body)          var list = JSON.parse(body).MemberList;          console.log(list.length)          for (var i = 0; i < list.length; i++) {              var member = list[i];                console.log(member.NickName,member.UserName);                // 群发消息要慎重              // if(member.NickName == '北风吹雪') {                   // postMsg(myUserName,member.UserName,'消息内容');                   // break;              //  }          }        });    }

源代码GitHub地址https://github.com/ColdDay/wxPro(如果对你有帮助请给个star✨,星星到位了后面才会有更有趣的功能,)

最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台