使用工具:微信开发者工具
最低基础库:2.8.1
//wxcode index.js
const cloud = require('wx-server-sdk')
const axios = require('axios')
var rp = require('request-promise');
cloud.init()
// 云函数入口函数
exports.main = async (event, context) => {
console.log(event)
try {
const resultValue = await rp('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=appid&secret=secret')
const token = JSON.parse(resultValue).access_token;
console.log('------ TOKEN:', token);
const response = await axios({
method: 'post',
url: 'https://api.weixin.qq.com/wxa/getwxacodeunlimit',
responseType: 'stream',
params: {
access_token: token,
},
data: {
page: event.page,
width: 350,
scene: "id=" + event.id,
},
});
return await cloud.uploadFile({
cloudPath: 'xcxcodeimages/' + Date.now() + '.png',
fileContent: response.data,
});
} catch (err) {
console.log('>>>>>> ERROR:', err)
}
}
//js页面
wx.cloud.callFunction({
name: 'wxcode', // 云函数名称
data: { // 小程序码所需的参数
page: "pages/card/othercard/othercard",
id: res.data.data.myCard.id,
},
complete: res => {
console.log(res)
const base64 = wx.arrayBufferToBase64(res.result.buffer)
const base64ImgUrl = "data:image/png;base64," + base64
console.log(base64ImgUrl)
base64src(base64ImgUrl, res => {
_this.setData({
'qrcode': res
})
});
}
})
//返回结果
既然都用云函数了,为什么不用云调用?却用云函数调用传统API?