preface

Small program because of the spread of fast, easy to share, now has become the size of the company’s standard, from once you have a public number, into you have a small program? You can imagine the popularity of small programs

So in applets, how do you generate applets? Although the small program can be shared to the circle of friends, but the spread of the small program code whether directly shared with friends, or as a picture to share, or as a line scan code entrance, is an important entrance to drainage

Effect of instance

The specific implementation

Small program side code that binds events to view elements

<view bindtap="onViewTap">Copy the code

Small program side logic code

Page({data: {}, // bind the click event function onViewTap() {this.createQrcode (); CreateQrCode () {this.showloading (); createQrCode() {this.showloading (); Wx.cloud. callFunction({getQrCode name: 'getQrCode',}). Then ((res) => {console.log(res); const fileId = res.result; Wx.previewimage ({// small program, generated directly preview, foreground display urls: [fileId], current: fileId,}); this.hideLoading(); }); ShowLoading () {wx.showloading ({title: 'creating... ', icon: 'none', }); }, hideLoading() { wx.hideLoading(); }});Copy the code

On the small program side just a few lines of code

Cloud function side implementation code

Create getQrCode cloud function under cloudFunctions folder, will be created by default config. The json, index, js, package. The json three files

In config.json, is

{
  "permissions": {
    "openapi": [
      "wxacode.getUnlimited"
    ]
  }
}
Copy the code

The configuration above is to generate the applets with wxacode.getunlimited. This configuration is fixed

The code in index.js is as follows

// const cloud = require('wx-server-sdk'); cloud.init(); // exports.main = async (event, context) => {const wxContext = cloud.getwxContext (); / / get the context const result = await cloud. Openapi. Wxacode. GetUnlimited ({/ / call generates small interface of program code to carry some parameters, such as: scene scene: wxContext.OPENID, }); // console.log(result) const upload = await cloud.uploadFile({// Upload the generated small program code to cloudPath: 'qrcode/' + date.now () + '-' + math.random () + '.png', return upload.fileID; // Return the fileID of the file, that is, the image};Copy the code

Generate small program code is the above a few lines of simple cloud function code can be achieved, mainly using the wxacode.getunlimited interface

Obtain small program code, suitable for the business scenario where the number of required codes is very large. The small program code generated through this interface is valid forever and the number is temporarily unlimited

The related documents

  • Wxacode.getunlimited applet code generates interface documentation
  • Cloud. uploadFile Uploads local resources to cloud storage

conclusion

There are two ways to generate applets in applets: one is HTTPS call, and the other is cloud call. In this paper, cloud call is the simplest one, which eliminates access_token and authentication exemption

At the applet end, the cloud function invokes the cloud to generate the applet code according to the interface wxcode.getUnlimited, and then uploads the applet code to the cloud storage, returns the fileID of the picture in the cloud storage, and at the applet end, the cloud function returns the fileID according to the file ID displays the small program code

Applets – Cloud development – Implementation generates applets