最近的需求是需要用户扫描指定二维码进去填写表单之类的需求(暗骂提需求的人)
看了下文档,嘿开发团队还真懂我们、
果断上手,然后问题又来了,这个返回值是二进制啊,好办!!
看代码
云函数代码
const cloud = require('wx-server-sdk')
cloud.init()
exports.main = async (event, context) => {
try {
const result = await cloud.openapi.wxacode.createQRCode({
path: 'page/index/index',
width: 430
})
return result
} catch (err) {
return err
}
}
index.js
onLoad: function() {
wx.cloud.init({env: 'env-address'})
wx.cloud.callFunction({
name: 'qrcode',
})
.then(res => {
console.log(res.result.buffer);
let fileManager = wx.getFileSystemManager();//获取文件管理器
let filePath = wx.env.USER_DATA_PATH + '/tmp.jpg';//设置临时路径
fileManager.writeFile({//获取到的数据写入临时路径
filePath: filePath,//临时路径
encoding: 'binary',//编码方式,二进制
data: res.result.buffer,//请求到的数据
success: function(res) {
console.log(res)
console.log(filePath)//打印路径
wx.previewImage({//图片预览
urls: [filePath],
})
}
});
})
}
完毕,下课