May 20, 2021 WeChat Mini Program Development Document
Support side: Cloud function
Get the WeChat call context in the cloud function
wxContext
Property | Type | Description |
---|---|---|
Openid | string | Small program users openid, small programs call cloud functions |
APPID | string | Small program AppID, small program when calling cloud functions |
UNIONID | string | Small program user unionid, small program side calls the cloud function, and meets the unionid acquisition conditions |
Env | string | The ID of the environment in which the cloud function is located |
SOURCE | string | Call source (what triggered this run of the cloud function) |
CLIENTIP | string | Small program client IPv4 address |
CLIENTIPV6 | string | The applet client IPv6 address |
The SOURCE value follows the call chain pass, which indicates the call link situation (separated by an English comma), such as a small program calling the cloud function A, and then calling the cloud function B within the cloud function A, A gets the SOURCE as wx_client, and the SOURCE obtained within B is wx_client, scf (weChat small program calls, and then the cloud function calls).
The enumerus type of SOURCE:
SOURCE value | Meaning |
---|---|
wx_devtools | WeChat IDE call |
wx_client | WeChat small program call |
wx_http | WeChat HTTP API call |
wx_unknown | WeChat is called from an unknown source |
Scf | The cloud function calls the cloud function |
Other | Non-WeChat trigger |
If ENV is local and SOURCE is local in on-premises debugging of wx_client.
Do not use getWXContext outside of exports.main, this fashion does not call context and cannot get information.
const cloud = require('wx-server-sdk')
exports.main = async (event, context) => {
const {
OPENID,
APPID,
UNIONID,
ENV,
} = cloud.getWXContext()
return {
OPENID,
APPID,
UNIONID,
ENV,
}
}
Support side: Cloud function 1.5.0
Use advanced logging capabilities in cloud functions
logger
Property | Type | Description |
---|---|---|
log | function | The log of the default level |
info | function | The log of the normal level |
warn | function | The log of the warning level |
error | function | The log of the error level |
Used to use advanced logging capabilities.
The logger method returns a log object that contains the following method, generating one log record per call: log: log info: normal-grade log warn: warning-level log error: error-level log
All methods receive an object, and each of the object's key, value, and pairs is a retrievable pair of key values for a log record, where value is automatically converted to a string regardless of type
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV,
})
// 云函数入口函数
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext()
const log = cloud.logger()
log.info({
name: 'xx',
cost: 10,
attributes: {
width: 100,
height: 200,
},
colors: ['red', 'blue'],
})
// 输出到日志记录中会有这么一条记录:
// {
// "level": "info",
// "name": "xx",
// "cost": "10",
// "attributes": "{ width: 100, height: 200 }",
// "colors": "[ "red", "blue" ]"
// ..., // 其他系统字段
// }
return {
event,
openid: wxContext.OPENID,
appid: wxContext.APPID,
unionid: wxContext.UNIONID,
}
}
Support side: Small program 2.12.0
The small terminal tuning cloud function passes the temporary CDNs available for big data
Tags data that needs to be uploaded to a CDN file/large string and then converted to an HTTP URL must be used in callFunction.
When a cloud function is called by a small terminal, if you want to pass big data (recommended for more than 128k), you can use this CDN method to mark the data that needs to be passed, between a string and a temporary file path. After the tag, when the cloud function is called, the data is automatically uploaded to the temporary CDN, and the field received within the cloud function will eventually be a CDN address that can be requested within the cloud function.
This method avoids the transmission of big data within the cloud function link, improves the performance of big data calls, and avoids touching the size limit of the calling data.
The CDN method can receive three parameter types:
When you define an object using a file path, the file content corresponding to the file path is automatically uploaded to the CDN and converted to a CDN URL when the service API is called, and the object is defined as follows:
Receive an object with fields defined below:
The field name | Type | Required | Description |
---|---|---|---|
type | string | Is | Define the type of object, filePath is required |
filePath | string | Is | The file path |
wx.cloud.callFunction({
name: 'test',
data: {
strDemo: wx.cloud.CDN('some large string'),
filePathDemo: wx.cloud.CDN({
type: 'filePath',
filePath: 'xxxxxxxx',
})
},
})
.then(console.log)
.catch(console.error)