今天初来乍到cnode.js,也应该贡献贡献.看到微信公众平台,开始有点兴奋,能做个机器人玩玩,,随后用Node.js写了一个,觉得其实这没什么意思.很快就觉得腻了,于是有了做发送微信接口的想法.首先要做的我们就要模拟公众平台的登陆.对于微信的这些lib,当然不能直接写在routes里面,,那要怎么办呢?没错,就要封装起来,方便复用.你可以打开控制台看到公众平台的登录请求,还有所需的参数,其中密码它是用它本身的md5进行加密的,那么我们需要做的只是将它copy过来放在一个helpers/wx/md5.js文件里就可以直接用了,以下是微信公众平台解析后格式化的js提交代码
submit: function() { if (!n()) return; var e = d.getVal(); t.post("/cgi-bin/login?lang=zh_CN", { username: e.account, pwd1: t.md5(e.password.substr(0, 15)), pwd2: t.md5(e.password), imgcode: f.data("isHide") ? "": e.verify, register: e.isRegister, f: "json" },
我们要建立一个login的方法
request = require 'superagent'require __basename + '/helpers/wx/md5'config = require __basename + '/config/config'module.exports = login: (fn) -> wx_usr = config.wx.user wx_pwd = md5 config.wx.pwd.substr(0, 16) request .post('http://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN') .type('form') .send( username: wx_usr pwd: wx_pwd imgcode : '' f : 'json' register : 0 ) .end (res) -> //在这里你已经成功获取cookie了
但是经过分析我想你会发现,这里的cookie其实并非你想要的cookie,因为它包含一些没用的信息Path=,我们设置cookie的时候,事实上是不能用直接设置这样的cookie,应该是一个cookie里面不应该有其他的东西,而分号后面的path应该将它去掉,这里是返回的结果:
["mp_user=xxxxxx; Path=/","mp_sid=NlJ2Tm5hb1NXRGxOU3V1MzF2a25tSFVWRHhTNkhwek1nMXlEOVZzMnZMUG1lZ29nSkdENGt3WlgwUjBJZnhydndYNkZSd0ZsaHRHdEozSHBIa3QwT3FWTmdXc3RxVFhYUDBCR3dnWkxIRWVvRlZObG15UC83SzU1aEZPZWpocU8=; Path=/"]
以下是完整的login代码:
login: (fn) -> wx_usr = config.wx.user wx_pwd = md5 config.wx.pwd request .post('http://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN') .type('form') .send( username: wx_usr pwd1: wx_pwd pwd2: wx_pwd imgcode : '' f : 'json' ) .end (res) -> cookie = '' for rs in res.header['set-cookie'] cookie += rs.replace(/Path=\//g, '') fn null, cookie
在这里,我们已经完成登录的操作了,接下来,我们要做的是进行发送,在发送的时候,要把这个cookie设置在请求的地址中,接下来的代码比较简单:(注意:这里面的fakeid是微信公众平台的id,我们可以用控制台去微信公众平台的用户管理中查看,如何获取用户好友的fakeid,接下来一章我会讲)
sender: (options, fn) -> msg = options.msg fakeid = options.fakeid unless msg fn error: 'missing msg' return unless fakeid fn error: 'missing fakeid' return psotParams = type: 1 content: msg error: false tofakeid : fakeid ajax : 1 request .post('http://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response&lang=zh_CN') .type('form') .send(psotParams) .set('Cookie', options.cookie) .end (res) -> fn null, JSON.parse res.text
这里,我们已经能完全发送了,因为返回的结果是一个json,所要最好先JSON.parse一下,里面的成功判断大家可以加上,返回的接口有个叫ret的参数,0为发送成功
{ret: "0",msg: "ok"}
下一章,我会为大家深入讲解怎么发送信息给好友,及获取微信头像等等技术
在调用的时候其实也比较简单,刚忘写了补上:
app.get '/wx/login', (req, res) -> msg = req.query.msg wx.login (err, cookie) -> res.json err if err data = msg : msg fakeid : 'xxxx' cookie : cookie wx.sender data, (err, results) -> res.json err if err res.json results
能给个qq号码?想请教一些东西
sender目前只能实现文字text的发送,但这并不完善,也可以实现图文的发送,但是却必须要上传一张封面图片,上传后需要在发送图文信息的时候把返回结果中的formId
拿到,http://mp.weixin.qq.com/cgi-bin/uploadmaterial?cgi=uploadmaterial&type=2&t=iframe-uploadfile&lang=zh_CN&formId=1
其中的参数type
是上传的类型,语音和视频是0,formId为null.上传封面图片接口如下:
uploadmaterial: (fn) -> [@login](/user/login) (err, cookie) -> request .post('http://mp.weixin.qq.com/cgi-bin/uploadmaterial?cgi=uploadmaterial&type=2&t=iframe-uploadfile&lang=zh_CN&formId=1') .type('form') .set('Cookie', cookie) .end (res) -> results = JSON.parse(res.text).match(/formId, '(\d+)'/)[2] fn null, results
成功后,可以进行图文消息的发送了.
send_appmsg: (options, fn) -> [@login](/user/login) (err, cookie) -> psotParams = error : false count : 1 AppMsgId : null title0 : options.title digest0 : '正文内容' # content0 : '<p>te<img src="//www.e-research-solutions.com/system/cms/themes/default/img/top_l.png" /></p><p><span style="color:red">测试标题</span></p>' content0 : options.msg fileid0 : '10000001' #此处的id即为封面图片的id preusername : options.username ajax : 1 request .post('http://mp.weixin.qq.com/cgi-bin/operate_appmsg?sub=preview&t=ajax-appmsg-preview') .type('form') .set('Cookie', cookie) .send(psotParams) .end (res) -> results = JSON.parse res.text fn null, results['msg']
联系客服