云函数生成小程序码并上传到云存储
同理可以将网络其他文件上传到云存储 首先安装 request-promise npm 命令 npm install request-promise // 云函数入口文件 const cloud = require('wx-server-sdk') //npm install request-promise const rp = require('request-promise'); cloud.init() // 云函数入口函数 exports.main = async (event, context) => { //appid 和秘钥 const appid = 'wxxxxxxxx', secret = 'xxxxxxxxxxxx'; 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': "pages/index/index", 'width': 430, 'scene': "1111" }, json: true , encoding: null }; //获取二进制图片 const buffer = await rp(code_options); //数据大于10K 上传到云 if (buffer.length>1024*10) { const upload = await cloud.uploadFile({ cloudPath: 'demo5561.jpg', fileContent: buffer, }) return { upload} } return { reslut:buffer} }