跳到主要内容
版本:v1.5.5-en

WeChat Mini Program SDK Integration Guide

1. Preparation

  1. Register an Account on River Cloud

    Visit the River Cloud Management Interface and register an account using your email address.

    💡   Tip:

    Refer to How to Obtain a River Cloud Account for steps on account registration and product purchase.

  2. Choose an Integration Method

    Read How to Choose a River Cloud Integration Method and select a suitable integration approach.

  3. Configure According to the Integration Guide

    Visit the Integration Guide page, find the guide corresponding to your chosen integration method, and complete the integration configuration as described.

  4. Download the WeChat Mini-Program SDK

    Log in to the River Cloud Management Interface, go to the Site Management page, and click WeChat Mini-Program SDK at the top. Download the file named wx-sdk.js.

2. Start Integrating the WeChat Mini-Program SDK

2.1 Integration Steps

  1. Copy the wx-sdk.js file to the root directory of the mini-program (or any other path, but ensure the correct path is used during import; here, the root directory is used as an example).

  2. In the root directory of the mini-program, find the app.js file and import wx-sdk.js at the first line. You can use either import or require for importing.

    💡   Tip:

    Refer to Appendix 1 for examples.

  3. The imported wx-sdk.js returns an init method, used to initialize the SDK.

    The init method has 4 parameters: tenant_id, access_key, hosts, and an optional options object.

    💡   Attention!!!

    Import the mini-program SDK at the first line of app.js and call the init method as soon as possible, otherwise, some or all requests may not include the token.

  4. Call the init method and pass the correct parameters.

2.2 init Method Parameters and Return Value Explanation

The parameters for the init method are as follows:

ParameterTypeRequiredDefaultDescription
tenant_idstringYesEnterprise Management -> Enterprise ID
access_keystringYesSite Management -> Global Configuration -> Access Key
hostsArray<string>YesSite Management -> Edit Site -> Domain
optionsObjectNoOptional, with async and timeout properties, detailed in the table below

The init method returns Promise<boolean>. If the SDK is successfully initialized, the Promise callback returns true; otherwise, it returns false.

The properties of the options parameter object are as follows:

ParameterTypeRequiredDefaultDescription
asyncbooleanNofalsefalse: Synchronous. The mini-program will not make any requests before init is completed. If requests are made before init completion, they will be stored and sent out together after init, ensuring that each request carries a token.
true: Asynchronous. The mini-program can make requests before init completion, but it cannot guarantee that these requests will carry a token.
timeoutnumberNo5000Request timeout duration in milliseconds.

3. Verifying Integration

  1. Open the SDK-integrated WeChat mini-program on a mobile device and visit different links.

  2. Log in to the River Cloud Management Interface, go to the Log Management > Search page, and in the Select Log dropdown menu at the top left of the page, choose WeChat Log (this log name will include the site name you created in the integration guide).

  3. If the page displays access records generated by the WeChat mini-program, it indicates successful SDK integration, and the mini-program has successfully integrated with River Cloud.

Appendix 1 Import Examples

  1. Import Example using import

    import init from './wx-sdk'

    const tenant_id = 'revol79602666'
    const access_key = '1u9r4iko4sq8g8h8'
    const hosts = ['demo.revolcloud.com']

    init(tenant_id, access_key, hosts, {
    async: false,
    timeout: 3000,
    }).then(res => {
    console.log(res) // true or false
    })
  2. Import Example using require

    There are two ways to import using the require statement. The first way is as follows:

    const { init } = require('./wx-sdk')

    const tenant_id = 'revol79602666'
    const access_key = '1u9r4iko4sq8g8h8'
    const hosts = ['demo.revolcloud.com']

    init(tenant_id, access_key, hosts, {
    async: false,
    timeout: 3000,
    })

    The second way is similar to the first but condensed into a single line, as shown below:

    require('./wx-sdk').init('revol79602666', '1u9r4iko4sq8g8h8', ['demo.revolcloud.com'])