微信小程序云开发如何使用云函数生成二维码

2022-01-13,,,,

这篇文章主要为大家详细介绍了微信小程序开发如何使用函数生成二维码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了微信小程序云开发使用云函数生成二维码的具体代码,供大家参考,具体内容如下

首先,需要给对应的云函数安装 request-promise 依赖。(不会给云函数安装依赖的盆友请移步 微信小程序中的云开发如何使用npm安装依赖)

生成二维码的云函数如下:

 // 云函数入口文件 const cloud = require('wx-server-sdk') const rp = require('request-promise') cloud.init() // 云函数入口函数 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-600', fileContent: buffer, }) return { wxacodefileID: upload.fileID } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。

以上就是微信小程序云开发如何使用云函数生成二维码的详细内容,更多请关注本站其它相关文章!

《微信小程序云开发如何使用云函数生成二维码.doc》

下载本文的Word格式文档,以方便收藏与打印。