云函数生成小程序码的Demo
# 云函数
config.json配置,云调用wxacode.get API 的权限
{
"permissions": {
"openapi": [
"wxacode.get"
]
}
}
index.js
const cloud = require('wx-server-sdk')
cloud.init()
exports.main = async (event, context) => {
try {
console.log(event)
const page = event.page
const result = await cloud.openapi.wxacode.get({
path: page,
isHyaline: true,
})
// 将资源上传至云存储空间
const upload = await cloud.uploadFile({
cloudPath: 'wxacode.png',
fileContent: result.buffer,
})
let wxacodefileID = upload.fileID
return wxacodefileID
} catch (err) {
return err
}
}
# 页面调用
wx.cloud
.callFunction({
name: 'get_wxacode',
data: { page: 'xxx?name=' + this.name },
})
.then(res => {
this.xcxCodeUrl = res.result
})
# 调用结果
# 参考文档
- 获取小程序码-wxacode.get (opens new window)
- 资源上传云存储空间-uploadFile (opens new window)