您的位置:首页 > 移动开发 > 微信开发

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

2019-03-07 17:36 971 查看
版权声明:转载请保留原文链接及作者 https://blog.csdn.net/u011236348/article/details/88314881

前言

更多内容,请访问我的 个人博客

首先,需要给对应的云函数安装

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',
fileContent: buffer,
})
return {
wxacodefileID: upload.fileID
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: