Implementing Simultaneous Publishing to Multiple Mini Programs in WeDa Applications
This article describes how to implement batch publishing of an application to multiple mini programs via the API of the WeDa low-code platform.
Background Introduction
In enterprise-level application scenarios, we may need to publish the same WeDa application to multiple different mini programs. Manually operating the publishing process for each mini program is both time-consuming and error-prone; implementing batch publishing through API interfaces can significantly improve efficiency.
Implementation Principles
The core process of batch publishing is shown in the figure below.
┌─────────────────────┐
│ Start Batch Publishing Process │
└──────────┬──────────┘
▼
┌─────────────────────┐
│ Bind the Nth Mini Program │◄─────┐
└──────────┬──────────┘ │
▼ │
┌─────────────────────┐ │
│ Publish Application │ │
└──────────┬──────────┘ │
▼ │
┌─────────────────────┐ │
│ Check Publishing Result │ │
└──────────┬──────────┘ │
▼ │
┌─────────────────────┐ │
│ Unbind Mini Program │ │
└──────────┬──────────┘ │
▼ │
┌─────────────────────┐ │
│ Whether there is another mini program ├── Yes ──┘
└──────────┬──────────┘
│ No
▼
┌─────────────────────┐
│ Batch Publishing Completed │
└─────────────────────┘
The entire process is a cyclic operation, performing the following steps sequentially for each mini program:
- Bind Mini Program: Bind the WeDa application to the target mini program.
- Publish Application: Trigger the application publishing process.
- Check Result: Poll the publishing status until success or failure
- Unbind: Unbind the current mini program from the application.
- Proceed to Next: If there are still mini programs in the list, continue processing the next one.
In this way, we can automatically publish an application to multiple Mini Programs without manual intervention.
Required API Interfaces
Implementing batch publishing requires calling the following API interfaces:
Interface Name | Function | API Documentation Link |
---|---|---|
DeployApp | Publish Application | View Documentation |
CheckDeployApp | Check Deployment Result | View Documentation |
DeleteAppBindWxApp | Delete Application and Mini Program Binding | View Documentation |
PutWxAppIdToWeApp | Bind Application and Mini Program | View Documentation |
Batch Publishing Process
1. Preparations
Before starting batch publishing, the following information needs to be prepared:
- Cloud Development Environment ID (EnvId) - Format: lowcode-5gzbnlvd93
- WeDa Application ID (AppId) - Can be viewed in the application list of the WeDa Low-Code Platform
- List of Mini Programs AppIDs to be published - Authorization must be completed in advance
- Cloud API Keys (SecretId and SecretKey) - How to Obtain Cloud API Keys
2. Sample Implementation Code
The authorization method for WeDa applications and mini programs currently only supports the fully managed mode. Before using the batch publishing feature, please ensure all target mini programs have completed authorization on the WeDa platform with the fully managed mode. Other authorization methods (e.g., scan code authorization) are not supported for batch publishing operations via API.
Below is a code sample for implementing batch publishing using Node.js:
If you need to implement in other programming languages such as Python, Java, PHP, etc., please refer to the Tencent Cloud API Documentation Center to obtain the SDKs and API invocation methods for the respective languages.
const tencentcloud = require("tencentcloud-sdk-nodejs");
const util = require("util");
const sleep = util.promisify(setTimeout);
// Import client models for the corresponding product module
const LowcodeClient = tencentcloud.lowcode.v20210108.Client;
// Configuration information
const secretId = "Your SecretId";
const secretKey = "Your SecretKey";
const region = "ap-shanghai"; // Region information
const appId = "App ID"; // WeDa App ID
const wxAppIdList = [
"wx1111111111111111",
"wx2222222222222222",
"wx3333333333333333",
]; // Mini Program AppID list
// Initialize client configuration
const clientConfig = {
credential: {
secretId,
secretKey,
},
region,
profile: {
httpProfile: {
endpoint: "lowcode.tencentcloudapi.com",
},
},
};
// Instantiate the client object for the requested product
const client = new LowcodeClient(clientConfig);
/**
* Publish Application
* @param {string} appId Application ID
* @param {string} wxAppId Mini Program AppID
* @returns {Promise<object|null>} Publish Result
*/
async function deployApp(appId, wxAppId) {
try {
const params = {
Action: "DeployApp", // The name of the API
Version: "2021-01-08", // API version number
EnvId: "lowcode-EnvironmentID", // Environment ID, example: lowcode-5gzbnlvd93
Id: appId, // Application ID, example: app-k82WCPM8
Mode: "upload", // Release to trial/production, example: upload
BuildType: "web", // Build type, example: web
SubAppIds: [], // Array of subpackages, optional parameter
};
const result = await client.DeployApp(params);
console.log(`Application publish request succeeded: ${JSON.stringify(result)}`);
return result;
} catch (error) {
console.error(`Application publish failed: ${error}`);
return null;
}
}
/**
* Check Publishing Result
* @param {string} appId Application ID
* @param {string} deployId Deployment ID
* @returns {Promise<boolean>} Whether the publish succeeded
*/
async function checkDeployStatus(appId, deployId) {
try {
const params = {
Action: "CheckDeployApp", // The name of the API
Version: "2021-01-08", // API version number
EnvId: "lowcode-EnvironmentID", // Environment ID, example: lowcode-5gzbnlvd93
Id: appId, // Application ID, example: app-k82WCPM8
BuildId: deployId, // Build ID, example: 4f12-b62b-c1c6f403d6e1
};
// Poll the publishing status, waiting for up to 10 minutes
for (let i = 0; i < 60; i++) {
const result = await client.CheckDeployApp(params);
const status = result.Status;
if (status === "success") {
console.log(`Application published successfully: ${JSON.stringify(result)}`);
return true;
} else if (status === "fail") {
console.log(`Application publish failed: ${JSON.stringify(result)}`);
return false;
}
console.log(`Application publishing in progress, current status: ${status}`);
await sleep(10000); // Query every 10 seconds
}
console.log("Publish timeout");
return false;
} catch (error) {
console.error(`Failed to query publish status: ${error}`);
return false;
}
}
/**
* Remove the binding between the application and the mini program
* @param {string} appId Application ID
* @returns {Promise<boolean>} Whether the unbinding succeeded
*/
async function deleteAppBind(appId) {
try {
const params = {
Action: "DeleteAppBindWxApp", // The name of the API
Version: "2021-01-08", // API version number
WeappId: appId, // Application ID, example: app-dfa
};
const result = await client.DeleteAppBindWxApp(params);
console.log(`Application unbound successfully: ${JSON.stringify(result)}`);
return true;
} catch (error) {
console.error(`Application unbind failed: ${error}`);
return false;
}
}
/**
* Bind application and Mini Program
* @param {string} appId Application ID
* @param {string} wxAppId Mini Program AppID
* @returns {Promise<boolean>} Whether the binding succeeded
*/
async function bindWxApp(appId, wxAppId) {
try {
const params = {
Action: "PutWxAppIdToWeApp", // The name of the API
Version: "2021-01-08", // API version number
WeAppId: appId, // Application ID, example: app-xxx
WxAppId: wxAppId, // WeChat AppId
};
const result = await client.PutWxAppIdToWeApp(params);
console.log(`Mini Program bound successfully: ${JSON.stringify(result)}`);
return true;
} catch (error) {
console.error(`Mini Program binding failed: ${error}`);
return false;
}
}
/**
* Batch publish applications to multiple Mini Programs
*/
async function batchDeploy() {
let successCount = 0;
let failCount = 0;
for (let index = 0; index < wxAppIdList.length; index++) {
const wxAppId = wxAppIdList[index];
console.log(
`\nProcessing the ${index + 1}/${wxAppIdList.length} Mini Program: ${wxAppId}`
);
// If it is not the first Mini Program, you need to unbind the previous binding first
if (index > 0) {
const unbindResult = await deleteAppBind(appId);
if (!unbindResult) {
console.log(`Unbinding failed, skipping Mini Program ${wxAppId}`);
failCount++;
continue;
}
}
// Bind new Mini Program
const bindResult = await bindWxApp(appId, wxAppId);
if (!bindResult) {
console.log(`Binding failed, skipping Mini Program ${wxAppId}`);
failCount++;
continue;
}
// Publish Application
const deployResult = await deployApp(appId, wxAppId);
if (!deployResult) {
console.log(`Publish Application failed, skipping Mini Program ${wxAppId}`);
failCount++;
continue;
}
const deployId = deployResult.DeployId;
if (!deployId) {
console.log("Failed to get deployment ID");
failCount++;
continue;
}
// Check Publishing Result
const deploySuccess = await checkDeployStatus(appId, deployId);
if (deploySuccess) {
successCount++;
console.log(`Mini Program ${wxAppId} published successfully`);
} else {
failCount++;
console.log(`Mini Program ${wxAppId} publish failed`);
}
}
// After the last Mini Program is published, you can choose whether to retain the binding relationship
console.log(`\nBatch publish completed, successes: ${successCount}, failures: ${failCount}`);
}
// Execute Batch Publishing
batchDeploy().catch((error) => {
console.error("An error occurred during batch publishing:", error);
});
3. Execution Process Description
- Initialization Configuration: Set API keys, region information, App ID, and Mini Program AppID list.
- Iterate over each mini program:
- If it is not the first Mini Program, unbind the previous binding first
- Bind the current Mini Program
- Publish Application
- Check publishing result, confirm success or failure
- Result Statistics: Record the number of successes and failures, then output the final result.
4. Mini Program Version Submission Review
After completing batch publishing, you also need to go to the WeChat Mini Program Admin Console to submit the version for review:
- Log in to WeChat Official Account Platform and access the corresponding Mini Program account.
- In the left menu bar, select "Development Management" -> "Development Version"
- Locate the version just published and click "Submit for Review"
- Fill in the necessary information such as version details and feature description
- Submit for review and await the official WeChat review result
For multiple mini programs published in batches, you need to log in to their respective WeChat Mini Program Admin Consoles and submit them for review one by one. Currently, the WeChat platform does not support batch review operations.
Notes
- API Call Frequency Limit: Please note that the WeDa platform imposes certain limits on API call frequency. It is recommended to add appropriate delays in your code.
- Error Handling: In actual production environments, more robust error handling and retry mechanisms should be implemented.
- Permission Verification: Ensure that the API key used has sufficient permissions to perform all operations.
- Release Time: The release process may take some time, especially for larger applications, requiring patient waiting.
- Logging: It is recommended to add detailed logging to facilitate troubleshooting.
Frequently Asked Questions
Q: How to get the WeDa application ID?
A: In the application list of the WeDa Low-Code Platform, select the target application, and the application ID will be displayed on the application details page.
Q: How to handle publish failures?
A: You can check the release logs to identify the specific reasons. Common failure reasons include Mini Program configuration errors, insufficient permissions, etc. It is recommended to add a retry mechanism in your code.
Q: Can applications be published to multiple Mini Programs simultaneously?
A: No. The WeDa platform requires that an application can only be bound to one mini program at a time, so they must be processed sequentially one by one.
Summary
Implementing batch publishing of WeDa applications through API interfaces can significantly improve operational efficiency, particularly suitable for enterprise scenarios requiring publishing the same application to multiple mini programs. The code sample provided in this article can serve as a foundational framework for customization and extension based on actual needs.