小程序二维码生成与获取

/* * @Author: Echonessy * @Date:2020-10-15 * @Function : 小程序二维码生成与获取 */ const request = require('request'); const appid = '你的小程序appid'; const appSecret = '你的小程序appSecret '; //String buffer 转base64 const toBase64 = (body) => { let base64Img = new Buffer.from(body, 'binary').toString('base64'); let decodeImg = new Buffer.from(base64Img, 'base64'); let binaryBuffer = new Buffer.from(decodeImg, 'binary'); let src = 'data: image/png; base64,' + binaryBuffer.toString('base64'); return src }// 获取token const getAccessToken = (appid,appSecret) => { return new Promise((resolve, reject) => { let url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential'; url +="&appid=" + appid; url +="&secret=" + appSecret; let options = {url: url,method: "POST",} request(options,(error, response, body)=>{ console.log('------------------getAccessToken-----------------') console.log(body) console.log('------------------getAccessToken-----------------') try { resolve(JSON.parse(body).access_token) }catch (e) { resolve('') } }) }) }// 获取小程序二维码请求,适用于需要的码数量较少的业务场景。通过该接口生成的小程序码,永久有效,有数量限制 const createQrcodeRequest = (token,subData) =>{ return new Promise((resolve, reject) => { const url = 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token='+ token; const options = { url:url, method: "POST", form:JSON.stringify(subData), encoding:"binary" // 二进制流 }; request(options,(error, response, body)=>{ let src = https://www.it610.com/article/toBase64(body) console.log('------------------createQrcodeRequest-----------------') console.log(src) console.log('------------------createQrcodeRequest-----------------') resolve(src); }) }) }// 获取小程序码请求, 适用于需要的码数量较少的业务场景。通过该接口生成的小程序码,永久有效,有数量限制 const getWxCodeRequest = (token,subData) => { return new Promise((resolve, reject) => { const url = 'https://api.weixin.qq.com/wxa/getwxacode?access_token='+ token; const options = { url:url, method: "POST", form:JSON.stringify(subData), encoding:"binary" // 二进制流 } request(options,(error, response, body)=>{ let src = https://www.it610.com/article/toBase64(body); console.log('------------------getWxCodeRequest-----------------') console.log(src) console.log('------------------getWxCodeRequest-----------------') resolve(src); }) }) }// 获取小程序码请求,适用于需要的码数量极多的业务场景。通过该接口生成的小程序码,永久有效,数量暂无限制。 const getUnlimitedCodeRequest = (token,subData) => { return new Promise((resolve, reject) => { const url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='+ token; const options = { url:url, method: "POST", form:JSON.stringify(subData), encoding:"binary" // 二进制流 } request(options,(error, response, body)=>{ let src = https://www.it610.com/article/toBase64(body); console.log('------------------getUnlimitedCodeRequest-----------------') console.log(src) console.log('------------------getUnlimitedCodeRequest-----------------') resolve(src); }) }) }// 创建小程序二维码,返回base64地址,适用于需要的码数量较少的业务场景。通过该接口生成的小程序码,永久有效,有数量限制 const getBaseCode = () => { return new Promise((resolve, reject) => { getAccessToken(appid,appSecret).then(token =>{ let creatOptions = {token:token,path:"pages/index/index",width:300} createQrcodeRequest(token,creatOptions).then(base64 =>{ resolve(base64) }) }) }) }// 获取小程序码,返回base64地址, 适用于需要的码数量较少的业务场景。通过该接口生成的小程序码,永久有效,有数量限制 const getWxaCode = () => { return new Promise((resolve, reject) => { getAccessToken(appid,appSecret).then(token =>{ let getOptions = { path:"pages/index/index", // 扫码进入的小程序页面路径 width:300, // 小程序码大小 line_color:{"r":"17","g":"171","b":"153"}, // 小程序码线条颜色 is_hyaline:false //是否需要透明底色,为 true 时,生成透明底色的小程序码 } getWxCodeRequest(token,getOptions).then(base64 =>{ resolve(base64) }) }) }) }// 获取小程序码,返回base64地址, 适用于需要的码数量极多的业务场景。通过该接口生成的小程序码,永久有效,数量暂无限制。 const getUnlimitedWxaCode = () => { return new Promise((resolve, reject) => { getAccessToken(appid,appSecret).then(token =>{ let getOptions = { path:"pages/index/index", // 扫码进入的小程序页面路径 width:300, // 小程序码大小 scene:"Echonessy",// 标识 line_color:{"r":"17","g":"171","b":"153"}, // 小程序码线条颜色 is_hyaline:false //是否需要透明底色,为 true 时,生成透明底色的小程序码 } getUnlimitedCodeRequest(token,getOptions).then(base64 =>{ resolve(base64) }) }) }) }module.exports = { getBaseCode:getBaseCode, getWxaCode:getWxaCode, getUnlimitedWxaCode:getUnlimitedWxaCode, }

    推荐阅读