官方文档:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/functions/openapi.html
支持接口:https://developers.weixin.qq.com/miniprogram/dev/api-backend/index.html
一、创建云函数并配置相应的权限
image.png{
"permissions": {
"openapi": [
"wxacode.getUnlimited"
]
}
}
必须配置相关的调用权限,否则无法正常调用
二、编写云函数
1、生成二维码
2、上传到云存储
3、获取临时地址(有效期最多一天)
// 云调用~生成小程序二维码
const cloud = require('wx-server-sdk')
cloud.init()
exports.main = async(event, context) => {
var scene = event.scene;
var page = event.page;
var width = event.width;
try {
// 1、通过云调用生成二维码
const result = await cloud.openapi.wxacode.getUnlimited({
scene: scene,
page: page,
width: width
})
console.log(result)
// 2、上传图片到云存储
const upload = await cloud.uploadFile({
cloudPath: scene + '.jpg',
fileContent: result.buffer,
})
console.log(upload)
// 3、返回图片地址
var fileID = upload.fileID;
console.log("fileId=" + fileID);
const fileList = [fileID]
const imgList = await cloud.getTempFileURL({
fileList: fileList,
})
return imgList.fileList
} catch (err) {
console.log(err)
return err
}
}
三、调用云函数
注意:页面地址不能带参数,否则报错
var path = 'pages/add_grade/add_grade'
var width = '430';
wx.cloud.callFunction({
name: 'generateEWM',
data: {
page: path,
width: width,
scene: objectId,
},
success: res => {
console.log(res)
wx.showToast({
title: '生成成功!',
})
},
fail: error => {
console.log(JSON.stringify(error))
wx.showToast({
title: '生成失败!',
})
}
});
返回JSON
{
"errMsg": "cloud.callFunction:ok",
"result": [{
"fileID": "cloud://release-3ea42b.7265-release-3ea42b-1258370407/9c4488c75cb7d2930121b6e46c3cefdb.jpg",
"status": 0,
"errMsg": "ok",
"tempFileURL": "https://7265-release-3ea42b-1258370407.tcb.qcloud.la/9c4488c75cb7d2930121b6e46c3cefdb.jpg"
}],
"requestID": "c4a20658-6181-11e9-818b-525400235f2a"
}
image.png
网友评论