官方文檔:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/functions/openapi.html
支持接口:https://developers.weixin.qq.com/miniprogram/dev/api-backend/index.html
一、創(chuàng)建云函數(shù)并配置相應(yīng)的權(quán)限
{
"permissions": {
"openapi": [
"wxacode.getUnlimited"
]
}
}
必須配置相關(guān)的調(diào)用權(quán)限瓦戚,否則無法正常調(diào)用
二吼砂、編寫云函數(shù)
1留荔、生成二維碼
2涣旨、上傳到云存儲(chǔ)
3怜奖、獲取臨時(shí)地址(有效期最多一天)
// 云調(diào)用~生成小程序二維碼
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妖胀、通過云調(diào)用生成二維碼
const result = await cloud.openapi.wxacode.getUnlimited({
scene: scene,
page: page,
width: width
})
console.log(result)
// 2峦剔、上傳圖片到云存儲(chǔ)
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
}
}
三、調(diào)用云函數(shù)
注意:頁(yè)面地址不能帶參數(shù)吝沫,否則報(bào)錯(cuò)
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"
}