小程序中用到了扫描二维码打开指定的小程序页面功能,微信提供了指定页面的二维码生成借口,但是返回的不是二维码的网络地址,需要服务端转存一下;用node搞了一下
API地址
接口需要access_token,请求access_token涉及到appid,secret就没把代码贴出来;用下面的接口获取access_token,GET请求;懒得写代码的直接扔到浏览器获取即可;
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
直接上货
var http = require("http"),
fs = require("fs"),
data = {"path": "pages/homePage/homePage", "width": 430};
data = JSON.stringify(data);
var options = {
method: "POST",
host: "api.weixin.qq.com",
path: "/cgi-bin/wxaapp/createwxaqrcode?access_token=5-R9rTlaeHF9bdZPWWh-t9BefbthRfbe5idSzuOSXoSFfPcUOUcTca1QhLNjDVclBK489LX88-hUeT1kL9FV8GBR4fWY78PVxyx_OGnNbJsUEQgAAACMY",
//记得更换token token有效时间2h
headers: {
"Content-Type": "application/json",
"Content-Length": data.length
}
};
var req = http.request(options, function (res) {
res.setEncoding("binary");
var imgData = "";
res.on('data', function (chunk) {
imgData += chunk;
});
res.on("end", function () {
fs.writeFile("./wx_liteQR.jpeg", imgData, "binary", function (err) {
if (err) {
console.log("down fail");
}
console.log("down success");
});
});
});
req.write(data);
req.end();
网友评论