Skip to main content

WeChat Mini Program

WeChat Mini Program (hereinafter referred to as Mini Program) is an application method supported by WeDa. Applications developed in WeDa can be directly published as WeChat Mini Programs. Most of WeDa's capabilities can be used in Mini Programs, and most of the native APIs of Mini Programs can also be used in WeDa.

Extending Use of WeChat Native Capabilities

WeDa provides many encapsulated capabilities for Mini Programs. For example, most components and APIs can be used normally in both Mini Programs and WEB. For Mini Program capabilities not encapsulated by WeDa, they can still be used normally in WeDa. For instance, when we want to directly obtain the user's shipping address in a Mini Program, we can simply call wx.chooseAddress in the event handler.

index/handler/chooseAddress
export default async function ({ event, data }) {
const res = await wx.chooseAddress();
console.log(res);
// Use shipping address logic
}

We can also directly utilize the capabilities of WeChat Cloud Development, for example, by directly invoking Mini Program Cloud Development cloud functions in front-end code:

index/handler/function1
export default async function ({ event, data }) {
const r = await wx.cloud.callFunction({
name: "myFunction",
});

console.log(res);
}

When using WeChat Cloud Development capabilities, it is important to note the differences between WeChat Cloud Development and Tencent Cloud CloudBase underlying WeDa. If the Mini Program account and Tencent Cloud account are not bound and associated, the resources in WeChat Cloud Development will be completely independent from those in WeDa, and resources in WeDa cannot be utilized through WeChat Cloud Development methods.

Limitations:

  • It should be noted that Mini Program APIs are only effective in the Mini Program runtime environment and cannot be properly executed in the WEB environment (including WeDa's editing area and preview mode).
  • Currently, WeChat native components cannot be directly used in WeDa and must be utilized via custom components.

OpenID / UnionID

In Mini Program scenarios, obtaining openid and unionid is a common use case. Here we introduce the methods to obtain openid/unionid in WeDa:

openid:

The application frontend can directly obtain user information via $w.auth.currentUser. If logged in via Mini Program, it will include the openid.

unionid:

WeDa does not currently provide encapsulated methods for obtaining unionid, but developers can still acquire unionid using the original Mini Program methods. For example, the simplest approach is to create a cloud function in the WeChat Cloud Development environment and call cloud.getWXContext() within the cloud function to retrieve unionid and other information.

For example, first create a cloud function getMPUserInfo in the Mini Program Cloud Development environment:

functions/getMPUserInfo
const cloud = require("wx-server-sdk");

cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV,
});

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

return {
openid: wxContext.OPENID,
appid: wxContext.APPID,
unionid: wxContext.UNIONID,
};
};

Then, call this cloud function in the WeDa application frontend:

index/handler/function1
export default async function ({ event, data }) {
const r = await wx.cloud.callFunction({
name: "getMPUserInfo",
});

console.log(res);
}

If the Mini Program cloud function environment and WeDa environment are not the same, it is necessary to independently initialize the Mini Program Cloud Development.

wx.cloud.init({
env: "xxxx", // initialization before invoking Mini Program Cloud Development
});

Page path

In native Mini Programs, APIs such as sharing, forwarding, and navigation require providing the absolute path of the page. Within the WeDa system, the absolute path can be obtained by following the rules below:

The main package page path is: /pages/${page.id}/index

The subpackage page path is: /packages/${packageName}/${page.id}/index, where packageName is the subpackage identifier, which can be obtained from the subpackage list and details.

Mini Program Package Size

WeChat Mini Program currently has limitations on the main package size. Exceeding 2M will prevent the Mini Program from being uploaded and published normally. When the package size exceeds the limit and cannot be published, consider using subpackages or reducing code. In WeDa, there are several approaches to resolve the issue where applications cannot be published due to the main package size exceeding the limit: