美文网首页
生成小程序二维码nodejs

生成小程序二维码nodejs

作者: 杨少东 | 来源:发表于2019-08-27 16:42 被阅读0次

    1.获取小程序token

    gettoken: function () {

        console.log("jhajaj");
    
        return new Promise((resolve, reject) => {
    
            let APPID = wxappletsconfig.appid;
    
            let secret = wxappletsconfig.secret;
    
            let url = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${APPID}&secret=${secret}`
    
            request.get(url, (err, response, data) => {
    
                console.log("err", err, data);
    
                if (err) {
    
                    resolve(false);
    
                } else {
    
                    let AccessToken = JSON.parse(data);
    
                    resolve(AccessToken);
    
                }
    
            })
    
        })
    
    },
    

    2.生成小程序码

    var accessToken = await wx_applets.gettoken(); //获取小程序token

    token = accessToken.access_token;
    
    var id = req.body.id;
    
    var userid = req.body.userid;  
    
    var pathstr =req.body.path; //小程序地址
    
    var filename = create_time(); //当前时间
    
    var data =  wx_applets.createwxaqrcode(userid,id,pathstr,token,filename);
    
    var picurl = config.url+"/"+data;
    
    var result = {
    
        "status": "200",
    
        "message": "success",
    
        "data":picurl
    
    }
    
    res.jsonp(result);
    

    3.创建

    createwxaqrcode:function(userid,id,path,accessToken,filename){

        // return new Promise((resolve, reject) => {
    
            var qrcodeurl = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='+accessToken
    
            let options = {
    
              method: 'POST',
    
              uri: qrcodeurl,
    
              encoding:null,
    
              body: {
    
                "page": path,
    
                "width":280,
    
                "is_hyaline":false,
    
                "scene":id+","+userid
    
              },
    
              json: true
    
            }
    
            var imgname='uploads/applet/'+filename+'.jpg'
    
            var imgnameback='applet/'+filename+'.jpg'
    
            fs.exists(imgname, function (exists) {
    
              //判断图片是否存在
    
              if(!exists){
    
                //通过微信接口获取小程序码
    
                requestp(options)
    
                .then(function (body) {
    
                  var base64Img = body.toString('base64'); // base64图片编码字符串
    
                  var dataBuffer = new Buffer(base64Img, 'base64');
    
                  //保存到本地
    
                  fs.writeFile(imgname, dataBuffer, function(err) {
    
                    if(err){
    
                      console.log(err);
    
                    }else{
    
                      console.log("保存成功!");
    
                    }
    
                  });
    
                })
    
                .catch(function (err) {
    
                  console.log(err)
    
                });
    
              }
    
            })
    
    
    
            return imgnameback;
    
      // });
    
    
    
    
    
    },

    相关文章

      网友评论

          本文标题:生成小程序二维码nodejs

          本文链接:https://www.haomeiwen.com/subject/qxoeectx.html