After obtaining the scheme code of the small program, you can pull up the small program through channels other than wechat, such as SMS, email and external web pages, just like opening web links. The form of URL scheme link is weixin://dl/business/? T = * TICKET *.

Description of Scheme codes

The Scheme code allows you to customize the page path of an incoming applet, carry parameters, and set whether it is valid forever or expires (such as within two hours or seven days). Carry parameters, set valid time and scenario value (the scenario value of the small program opened through URL Scheme is 1065), so that we can use Scheme code to carry out activity promotion, channel promotion, rebate sharing and other tracking. Scheme code is only open to domestic impersonal applets.

Android phones cannot recognize URL Scheme directly, we need to use H5 page transfer, such as location.href = ‘weixin://dl/business/? T = *TICKET*’, which can be invoked immediately when the user opens H5 (no further action is required) or after the user triggers an event. In addition to using this method, iOS can also recognize URL Scheme and jump applet directly in SMS.

It is worth mentioning that URL Scheme does not support opening applets in wechat, that is to say, applets cannot be opened with URL Scheme through wechat chat, moments, public accounts, wechat browser and other channels. If opened in wechat, it will prompt “Sorry, the current page is not accessible”.

Scheme code acquisition and use

1. Use cloud call to get scheme code quickly

We can call cloud.urlscheme.generate through the cloud to get Scheme code, use wechat developer tools to create a Node.js cloud function, which can be named urlScheme, and then open index.js with the editor and input the following code. After saving the code, Right-click index.js and select “Cloud function incremental Upload: Update file” in the popup window.

const cloud = require('wx-server-sdk')
cloud.init({
	env: cloud.DYNAMIC_CURRENT_ENV, 
})

exports.main = async (event, context) => {
  try {
    const result = await cloud.openapi.urlscheme.generate({
      jumpWxa: {
        path: ' '.// The path of the applet page entered through the Scheme code must be the page of the published applet and cannot carry query. If path is empty, the applet home page is redirected.
        query: ' ' // Query when entering a small program through scheme code. A maximum of 128 characters, only numbers, upper and lower case English and some special characters are supported:! # $& '() *, + / :; =? @ - _ ~
      },
      isExpire: false.// The generated Scheme code type, expired: true, permanent: false
    })   
    return result.openlink
  } catch (err) {
    return err
  }
}
Copy the code

We can then call the cloud function by typing the following code in the developer tools console to print the Scheme code returned, such as weixin://dl/business/? T = zUHMETgiMak.

wx.cloud.callFunction({name:"urlscheme"}).then(res= >{console.log(res.result)})
Copy the code

Because isExpire is set to false, the generated scheme code is valid for a long time; Since path is empty, the scheme code will jump to the home page of the applet.

2. Use of Scheme code

If you are an iOS mobile phone or tablet, you can copy and paste this Scheme code into SMS, memos, browser address bar, email and almost any direct link to the App, you can directly jump to the small program.

We can also put this Scheme code in a web page and jump to it via location.href. For example, we can use VS Code to create a new schemesimple. HTML file, copy and save the following Code, and then upload schemesimple. HTML to static hosting or server.

<! DOCTYPEhtml>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="Width = device - width, initial - scale = 1.0">
  <title>Page jump small procedures</title>
  <script>
    window.location.href="weixin://dl/business/? t=zUHMETgiMak"
  </script>
</head>
<body>This only applies to wechat outside jump to small program</body>
</html>
Copy the code

Such as you can in other mobile phone applications outside the WeChat (such as browser, zhihu, SMS, E-mail, etc.) to open the link i.hackweek.org/public/sche… , you can jump to the small program home page.

It is worth emphasizing that the static web page with your Scheme code has no additional requirements on the server, such as not necessarily on the cloud development or Tencent cloud static hosting, but also on Tencent cloud, Ali cloud or other servers or other static hosting.

3. Short-term and long-term scheme codes

A Scheme whose validity duration is less than 31 days is a short-term Scheme. A small program generates a maximum of 500,000 short-term schemes every day. A Scheme whose validity duration is more than 31 days or a Scheme whose validity duration is permanent is a long-term Scheme. A single applet can generate a maximum of 100,000 long-term schemes.

Therefore, short-term scheme code is recommended for pages with activities, user OpenID, click time, address, source and other information, while long-term scheme code is recommended for fixed pages or pages that can directly access the home page of small programs.

The following is a cloud function case that records the user openID, the parameter productID passed in the applet side, and fails 2 hours after the generation. After entering the following code into the index.js of urlScheme, save the code, right-click the index.js, and select “Incremental upload of cloud function: Update file” in the popup window.

const cloud = require('wx-server-sdk')
cloud.init({
	env: cloud.DYNAMIC_CURRENT_ENV, 
})

exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const {OPENID} = wxContext
  const {productID} = event

  try {
    const result = await cloud.openapi.urlscheme.generate({
      jumpWxa: {
        path: '/pages/home/home'.query: `openid=${OPENID}&id=${productID}`
      },
      isExpire: true.expire_time: Math.round(new Date().getTime()/1000) + 3600*2
    })
    return result.openlink
  } catch (err) {
    return err
  }
}
Copy the code

To return the Scheme code, enter the following code on the console console and pass productID to the urlScheme cloud function.

wx.cloud.callFunction({name:"urlscheme".data: {productID:"JD1001"}}).then(res= >{console.log(res.result)})
Copy the code

Through the previous practice, we learned that by putting scheme code into static web pages, we can jump to small programs through web pages in mobile phones and other applications other than wechat. We also need to consider non-mobile or wechat compatibility, which we will cover in a later chapter.