WePY and Cloud Development: Practical Guide and Best Practices
The guide explains how to combine WePY’s Vue‑style component model with Tencent Cloud Development—showing project initialization, cloud‑function setup, common pitfalls, and a token‑caching example—so developers can build and launch a full mini‑program in roughly 24 hours while competing for best‑practice prizes.
This article introduces a collaborative activity between Tencent Cloud Development and the WePY team, inviting developers to submit best‑practice projects for a prize.
Why choose WePY? The author compares several mini‑program frameworks (WePY, MPVue, Taro, MinUI) and selects WePY for its Vue‑like component model, ES6/ES7 support, rich Vue ecosystem, native API optimizations, and convenient data binding syntax (e.g., this.xxx = xxx ).
Enabling Cloud Development in a WePY project – Since WePY does not provide a cloud‑development template, the author demonstrates how to initialize a project with wepy init cloudkits/tcb-demo and manually add cloud functions. Cloud APIs are accessed via the wx.cloud.xxx namespace, and the cloud function directory must be declared in project.config.json as cloudfunctionRoot pointing to a cloudfunctions folder.
Common pitfalls
When using this.xxx to modify data, the variable must be declared in the page's data object; otherwise the change will not be reflected.
Cloud functions should be placed outside the mini‑program src directory to avoid compilation errors.
Token management example – The article provides a complete code snippet that caches a WeChat access token in a cloud database, checks its expiration, and refreshes it when necessary. The code uses the got library for HTTP requests and demonstrates cache operations (get, add, update) with proper time handling.
const result = await cache.get();
const now = new Date().valueOf();
const nextTime = now + 5400000;
let accessToken = '';
if (!result.data.length) {
console.log("进入初次获取的流程");
const result = await got(accessTokenUrl);
accessToken = JSON.parse(result.body).access_token;
await cache.add({ data: { token: accessToken, time: nextTime } });
} else {
if (result.data[0].time > now) {
console.log("已有 token 有效");
accessToken = result.data[0].token;
} else {
console.log("已有 token 无效");
const tokenResult = await got(accessTokenUrl);
accessToken = JSON.parse(tokenResult.body).access_token;
await cache.doc(result.data[0]._id).update({ data: { token: accessToken, time: nextTime } });
}
}The author concludes that using WePY together with Tencent Cloud Development dramatically speeds up mini‑program development, allowing a full product to be launched within 24 hours.
Tencent Cloud Developer
Official Tencent Cloud community account that brings together developers, shares practical tech insights, and fosters an influential tech exchange community.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.