Recent development of small encounters an applet code generating function, normal operation is requested by the interface:
POST https://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN
But today, suddenly want to go unusual way, we decided to use a cloud development and play.
Man of few words said, the code it!
wxml:
< The Button bindstap = "creatCode" > Click for </ the Button >
Cloud function, I define a getcode function, the first to function under a cloud config.json file permissions are configured at call
config.json:
openapi.wxacode.get
Then write to the inside index.js logic code:
index.js:
const cloud = require('wx-server-sdk') cloud.init() exports.main = async (event, context) => { try { const result = await cloud.openapi.wxacode.get({ path: 'page/index/index', width: 430 }) console.log(result) return result } catch (err) { console.log(err) return err } }
Finally, call the above method creatCode cloud function
js:
creatCode(){ console.log('开始生成小程序码') wx.cloud.callFunction({ name:'getcode', data:{}, success:res=>{ console.log(res) let fileManager = wx.getFileSystemManager(); let filePath = wx.env.USER_DATA_PATH+'/qr.jpg'; fileManager.writeFile({ filePath:filePath, encoding:"binary", data:res.result.buffer, success:res=>{ console.log(res) wx.previewImage ({ //Image Preview URLs: [filePath], }) } }) }, Fail: ERR => { } }) }
Checked the official documents, the function returns the cloud data is a binary data objects and data types include, ArrayBuffer is a type of things
Here encountered a pit, this type directly into base64 and then call out the image tab on the micro-channel development tools can be out, but the real machine on gg, and directly blank
This data needs to keep costs to a temporary file
So by
let fileManager = wx.getFileSystemManager()
fileManager.writeFile
Save the binary data.
Get away!