background

In the small program, you can directly share the small program to the chat window, but can not share the small program to the circle of friends, but the circle of friends is an important promotion site, so sharing the small program code has become an important way. One application scenario is the posters with small program codes that are often flooded on moments of friends.

Common share small program code, directly in the background to put a common small program code can be. However, if you need to share a particular path + parameter applets, you must use the API provided by the applets to do so.

This time to tell you how to use the cloud function to achieve a specific path + parameters of the small program code.

interface

The cloud function provides three apis for getting applets:

The name of the Functional specifications
wxacode.createQRCode Obtain the qr code of a small program, which is applicable to service scenarios where a small number of codes are required
wxacode.get Obtain the small program code, suitable for the service scenario that requires a small number of codes
wxacode.getUnlimited Obtain small program code, suitable for the business scenario where the number of required codes is very large

The first two are limited in number and cannot be used for scenarios like detailed sharing, so the only path left is the wxacode.getunlimited interface.

Let’s take a look at the main parameters of this interface:

attribute instructions
scene The value contains a maximum of 32 visible characters, including digits, uppercase and lowercase characters, and some special characters:! # $& ‘() *, + / :; =? @-._~, other characters should be encoded as legitimate characters (Chinese characters cannot be processed by Urlencode because % is not supported, please use other encoding methods).
page Pages /index/index, do not add/before the root path, and do not carry parameters (please put the parameters in the scene field). If you do not fill in this field, the default page will be skipped

Other parameters are not listed, there is a need to go directly to the official document. According to the above parameters, we can specify any page, and also pass in page parameters. Finally, when the page of the applet is loading, we can obtain the scene through options, so as to load data.

implementation

Cloud function

Let’s take a look at how the cloud functions are configured. After creating a cloud function qrcode, configure it in config.json as follows:

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

Then add the following code to index.js:

// cloud function entry file const cloud = require('wx-server-sdk'Cloud.init () // cloud function exports.main = async(event, context) => {try {letScene: event. Scene, param = {scene: event. Scene, // can be any page page:'pages/index/index'}; / / call interface var result = await cloud. Openapi. Wxacode. GetUnlimited (param)return result
  } catch (err) {
    return err
  }
}
Copy the code

Then deploy to the cloud, and the cloud function part is done.

Small program

Let’s see how the applet side calls:

requestQrcode(scene) {
    let that = this;
    wx.cloud.callFunction({
        name: 'qrcode',
        data: {
          scene: scene
        }
      })
      .then((res) => {
        let path = wx.env.USER_DATA_PATH + '/' + res.requestID + '.jpg'
        if (res.result.contentType == 'image/jpeg'Wx.getfilesystemmanager ().writefile ({filePath: path, data: res.result.buffer, encoding:'base64',
              success: (res) => {
              },
              fail: (res) => {
              }
            })
        } else{// exception handling}})},Copy the code

In the onLoad(options) section of the page, we can get the parameter scene:

onLoad: function(options) {
  let scene = options.scene;
},
Copy the code

This completes the small code that uses the cloud function to generate a specific path + parameter, which you can then use to draw the poster.

The problem

Have you noticed that the scene parameter has a limit of 32 visible characters? It can be easily exceeded.

Nothing stumbles a programmer! If there is, it must be unreasonable demand ~

Short chain, a good solution. Generate a unique short chain for all parameters and pass it as scene parameter. Finally, parse the short chain when loading, and get the super-long parameter

tip

If you do not need to pass parameters to the page, directly in the applets management platform at the top, there is a tool -> Generate applets, fill in the page path can generate applets material ~

At the end

Finally share a finished product ~