Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Cloud development small program code


May 22, 2021 Mini Program Cloud Development Advanced



Through the service cloud function can obtain a small program arbitrary page of the small program code, scanning the small program code can go directly to the page corresponding to the small program, all generated small program code permanently valid, can be used for a long time. S mall program codes have better identification and advanced capabilities such as demonstrating "public attention components". W hen the user sweeps the small program code to open the small program, the developer can configure the public number attention component official-account the small program, the user can quickly pay attention to the public number.

First, get the small program code

1, small program code interface description

  • wxacode.get, which is suitable for business scenarios where a small number of codes is required, has a long acceptable path parameter and a maximum page path length of 128 bytes;

  • wxacode.getUnlimited, get small program codes, suitable for business scenarios where you need a very large number of codes. The generated small program code is permanent and unlimited in number, but this method only supports 32 characters.

  • wxacode.createQRCode (not recommended), similar to wxacode.get, only generates not small program code, but small program QR code

wxacode.get and wxacode.createQRCode have a total number of codes generated limited to 100,000, that is, the life of your small program, can only be generated in these two ways 100,000 small program code and small program QR code, but if the parameters are the same, is not counted, so 100,000 is quite a lot.

The difference between wxacode.get and wxacode.getUnlimited

If your small program page parameters are dynamically updated, it is recommended to use wxacode.getUnlimited, if your small program page contains a very large number of operational class parameters, 32 characters are not enough, or the dynamic page is small, then you can use wxacode.get, usually with wxacode.getUnlimited is more secure.

wxacode.getUnlimited may not be enough for 32 characters, such as the openid of a user who wants to track a user sharing a small program code, such as wanting to record more operational data, but even if it is not enough, there is an alternative, that is, to add a field ID to the database, the parameters you want to record with this short and unique ID, which would waste a bit of database performance, but also acceptable scope.

In addition, on the parameters passed at the time of the cloud call, wxacode.get must fill in the path (path is the page path of the small program, which contains page and scene), while wxacode.getUnlimited's page and scene are separate and can only be filled in with scene without having to fill in the page.

2, create a new cloud function and add permissions

First of all, we use developer tools, create a new cloud function such as wxacode, and then add the following permission configuration in config.json (the format of the permission profile json has been repeatedly emphasized earlier), that is, when we handle cloud calls, we must first add permissions, and the format of the permission file can not be wrong.

  1. {
  2. "permissions": {
  3. "openapi": [
  4. "wxacode.get",
  5. "wxacode.getUnlimited"
  6. ]
  7. }
  8. }

Then add the following code .js index, we first take the wxacode.getUnlimited interface as an example to get the small program code, and then upload the small program code to the cloud storage,

  1. const cloud = require('wx-server-sdk')
  2. cloud.init({
  3. env: cloud.DYNAMIC_CURRENT_ENV
  4. })
  5. exports.main = async (event, context) => {
  6. const wxacodeResult = await cloud.openapi.wxacode.getUnlimited({
  7. scene: 'uid=1jigsdff',
  8. //只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~,不能有空格之类的其他字符
  9. page: 'page/index/index',
  10. //注意这个必须是已经发布的小程序存在的页面(否则报错),根路径前不要填加 /,不能携带参数(参数请放在scene字段里),如果不填写这个字段,默认跳主页面;但是你要填写就不要写错路径
  11. })
  12. const uploadResult = await cloud.uploadFile({
  13. cloudPath: `wxacode.jpg`,
  14. fileContent: wxacodeResult.buffer,
  15. })
  16. return uploadResult.fileID
  17. }

And if you use the wxacode.get interface, it passes different parameters.

  1. const result = await cloud.openapi.wxacode.get({
  2. path: 'page/index/index?uid=1jigsdff',
  3. })

By calling this cloud function, you can see the generated wxacode code .jpg in the cloud store. We can write parameters such as the id of a field in the collection, or the page id, into the small program code.

Second, through the small program code into the small program

By tracking the small program code with parameters, we can know which small program code the user is generated by us into the small program, this function application has many scenarios, especially in operation is particularly useful, such as tracking the user's share to increase points or rebates, tracking the operation of various channels and so on, to complete such a step, in addition to generating a small program with parameters, but also need a small program can identify the small program code.

1, small program code and scene value

Scene values are used to describe the user's path into the small program, such as the public number article custom menu, template messages, articles, etc., QR code scanning, long press, by identifying the mobile phone album QR code, WeChat group chat or single chat, WeChat home page search box, etc. , that is, the user in the end through what way to enter our small program, there will be a corresponding scene value, scanning small program code is 1047, long by recognition of small program code 1048, The small program code selected in the scan phone album is 1049.

We can get the above scene values in the app lifecycle of onLaunch and onShow, or wx.getLaunchOptionsSync (note that this interface is an object, not a function), and the options object below will contain scene

  1. onLaunch (options) {
  2. console.log('onLaunch方法',options)
  3. },
  4. onShow (options) {
  5. console.log('onShow方法',options)
  6. },

This property is included in the options object, and the value of the property is the scene value:

  1. path: "" //页面路径
  2. query: {} //页面的参数
  3. referrerInfo: {} //来源小程序、公众号或 App 的 appId
  4. scene: 1047 //场景值
  5. shareTicket: //带 shareTicket 的转发可以获取到更多的转发信息,例如群聊的名称以及群的标识 openGId

2, get the parameters in the small program code

It is worth noting that the parameters with the small code generated using cloud.openapi.wxacode.get and cloud.openapi.wxacode.getUnlimited need to be simulated using the conditions of the development tool when debugging the custom parameters scene=xxxx, and the parameter values of the scene when the development tool simulates need to be encodeURIComponent.

First of all, we need the encodeURIComponent() method to encode the parameters we want to pass, for example, we want to pass parameters such as "a"a" and "b"4 and c=5", which we can encode directly in the console:

  1. encodeURIComponent('a=3&b=4&c=5')

The encoded result is "a%3D3%26b%3D4%26c%3D5", which can be added to the compilation mode and filled in the startup parameters when debugging

  1. scene=a%3D3%26b%3D4%26c%3D5

Small program code is not only a technical problem, more involved in operations, so that the effectiveness of operations can be quantified tracking, is the key to growth of hacking, data operations, scenario values can let us understand the growth source of small programs, and write some parameters into small program codes, can let us according to the parameters of different operational strategies, such as ad click, rebate, distribution, collage, sharing tracking and so on. As a developer, you can communicate more with your operations and make the growth of small programs more effective.