头图

Overview

WeChat has opened a series of front-end interfaces for obtaining WeChat's open data. Considering that the developer server also needs to obtain these open data, WeChat provides two ways to obtain it:

  • Traditional solution: developers verify and decrypt open data in the background
  • Cloud development: direct access to open data through cloud calls

This article will focus on how the cloud develops .

Solution comparison: traditional solution vs cloud development

On the whole, using cloud development to obtain WeChat open data is convenient and efficient.

Code example

The interface for obtaining open data, if sensitive data is involved (such as wx.getWeRunData(), the plaintext content of the interface will not contain these sensitive data, but the returned interface contains the cloudID field corresponding to the sensitive data, and the data can be passed through the cloud function Obtaining. The complete process is as follows:

Step 1: Get cloudID

Using the basic library of version 2.7.0 or above, if the applet has been opened for cloud development, the return value of the open data interface can be obtained through the cloudID field (same level as encryptedData), and the cloudID is valid for five minutes.

Take the example of obtaining the number of user's WeChat exercise steps:
Detailed documentation: https://developers.weixin.qq.com/miniprogram/dev/api/open-api/werun/wx.getWeRunData.html

wx.getWeRunData({
success(res) {
const cloudID = res.cloudID;
// ...
}
});

Step 2: Call the cloud function

When calling a cloud function, for the incoming data parameter, if the value of the top-level field is the cloudID constructed by wx.cloud.CloudID, when the cloud function is called, the value of these fields will be replaced with the open data corresponding to the cloudID, once Up to 5 cloudIDs can be replaced by a call.

example:

Initiate a call after the applet obtains the cloudID:

wx.getWeRunData({
success(res) {
const id = res.cloudID;
wx.cloud.callFunction({
name: "myFunction",
data: {
weRunData: wx.cloud.CloudID(id), // 这个 CloudID 值到云函数端会被替换,
obj: {
shareInfo: wx.cloud.CloudID("yyy") // 非顶层 CloudID,不会被替换
}
}
});
}
});

Examples of events received in cloud functions:

// event
{
// weRunData 的值已被替换为开放数据
"weRunData": {
"cloudID": "xxx",
"data": {
"stepInfoList": [
{
"step": 5000,
"timestamp": 1554814312
}
],
"watermark": {
"appid": "wx1111111111",
"timestamp": 1554815786
}
}
},
"obj": {
// 非顶层字段维持原样
"shareInfo": "yyy"
}
}

If the cloudID is illegal or expired, what you get in the event will be an object that contains the error code, error information, and the original cloudID. Example of the result of exchanging expired cloudID:

// event
{
"weRunData": {
"cloudID": "xxx",
"errCode": -601006,
"errMsg": "cloudID expired."
}
// ...
}

Related documents:

The server obtains WeChat open data: https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/signature.html

Mini Program Open Interface Document: https://developers.weixin.qq.com/miniprogram/dev/api/open-api/login/wx.login.html

Server access to open data: https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/signature.html

product description

Cloud Development (Tencent CloudBase, TCB) is a cloud-native integrated development environment and tool platform provided by Tencent Cloud. It provides developers with highly available, automatically and elastically scalable back-end cloud services, including serverless capabilities such as computing, storage, and hosting. , Can be used for cloud integration to develop a variety of end applications (small programs, official accounts, web applications, Flutter clients, etc.) to help developers build and manage back-end services and cloud resources in a unified manner, avoiding cumbersome servers in the application development process With construction and operation and maintenance, developers can focus on the realization of business logic, with lower development thresholds and higher efficiency.
Open cloud development: https://console.cloud.tencent.com/tcb?tdl_anchor=techsite
Product documentation: https://cloud.tencent.com/product/tcb?from=12763
Technical document: https://cloudbase.net?from=10004
Technical exchange group, latest information, follow WeChat public account [Developed by Tencent Cloud Cloud]


CloudBase云开发
425 声望438 粉丝

云开发(Tencent CloudBase,TCB)是云端一体化的后端云服务 ,采用 serverless 架构,免去了移动应用构建中繁琐的服务器搭建和运维。同时云开发提供的静态托管、命令行工具(CLI)、Flutter SDK 等能力极大的降...