var fs = require('fs');
var request = require('request');
var wx_conf = require('../../conf/wx_conf');//这里放了微信appid跟appSecret,文件没有引入进来,要用的时候,改一下吧。
var AccessToken = {
grant_type: 'client_credential',
appid: wx_conf.appId,
secret: wx_conf.appSecret
}
var wx_gettoken_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=' + AccessToken.grant_type + '&appid=' + AccessToken.appid + '&secret=' + AccessToken.secret;
//请求二维码的参数
var postData = {
path: "pages/index/index",
width: 430
}
var createQrcode = {
create: function() {
console.log('fn:create');
this.getToken();
},
//获取微信的token
getToken: function() {
console.log('fn:getToken');
var that = this;
new Promise((resolve, reject) => {
console.log('进入Promise方法了');
request({
method: 'GET',
url: wx_gettoken_url
}, function(err, res, body) {
if (res) {
resolve({
isSuccess: true,
data: JSON.parse(body)
});
} else {
console.log (err);
reject({
isSuccess: false,
data: err
});
}
})
}).then(proData => {
that.getQrcode(proData);
});
},
//生成二维码
getQrcode: function(proData) {
console.log ('fn:getQrcode');
if (proData.isSuccess) {
postData = JSON.stringify(postData);
request({
method: 'POST',
url: 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=' + proData.data.access_token,
body: postData
}).pipe(fs.createWriteStream('./public/images/index.png'));//路径自己定义吧
} else {
console.log('Promise请求数据出错');
}
}
}
module.exports = createQrcode;//暴露对象,调用create方法既可以创建二维码
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有