本文實例為大家分享了微信小程序云開發(fā)使用云函數(shù)生成二維碼的具體代碼,供大家參考,具體內(nèi)容如下
首先,需要給對應(yīng)的云函數(shù)安裝 request-promise 依賴。(不會給云函數(shù)安裝依賴的盆友請移步 微信小程序中的云開發(fā)如何使用npm安裝依賴)
生成二維碼的云函數(shù)如下:
// 云函數(shù)入口文件const cloud = require('wx-server-sdk')const rp = require('request-promise')cloud.init()// 云函數(shù)入口函數(shù)exports.main = async (event, context) => { const page = event.page const scene = event.scene //appid和秘鑰 const appid = '***', secret = '***'; const AccessToken_options = { method: 'GET', url: 'https://api.weixin.qq.com/cgi-bin/token', qs: { appid, secret, grant_type: 'client_credential' }, json: true }; //獲取AccessToken const resultValue = await rp(AccessToken_options); const token = resultValue.access_token; //獲取小程序碼配置 const code_options = { method: 'POST', url: 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' + token, body: { 'page': page, 'width': 430, 'scene': scene }, json: true, encoding: null }; //獲取二進制圖片 const buffer = await rp(code_options); const upload = await cloud.uploadFile({ cloudPath: 'wxacode.png', fileContent: buffer, }) return { wxacodefileID: upload.fileID }}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持錯新站長站。
新聞熱點
疑難解答