WeChat Mini Program - Built-in Cloud Support
WeChat Mini Program now supports built-in Cloud Database, Storage, Function support for repid development.
Cloud Database
JSON Database
Table: Collection(/Array of JSON)
ROW: Record/Doc(/JSON)
Column: Field
API: Mini-program Side & Server Side
Mini-program side(access control):
- for less sensitive data
Server side:
- via Cloud function
- CRUD
- for sensitive data
Accessing
- retrieve a database
wx.cloud.database() - construct the query
collection(): retrieve a collection
where(): form a query - initiate the request
get()
db.collection('books').where({
publishInfo: {
country: 'United States'
}
}).get({
success: function(res) {
// 输出 [{ "title": "The Catcher in the Rye", ... }]
console.log(res)
}
})
Advanced Query
//todo
Cloud Storage
API: Mini-program & Cloud function
wx.cloud.uploadFile()
wx.cloud.downloadFile()
wx.cloud.uploadFile({
cloudPath: 'xy.png',
filePath: chooseResult.temFilePath[0],
success: res => {}
})
Cloud Function
Cloud function
- entry: index.js
- default: exports.main
- Context: require('wx-server-sdk').getWXContext() provide default login status of appid and openid
const cloud = require('ex-server-sdk')
exports.main = (event, context) => {
let { userInfo, a, b } = event
let { OPENID, APPID } = cloud.getWXContent()
return {
OPENID,
APPID,
x,
y
}
}
Invoke
wx.cloud.callFunction({
name: 'add',
data: {
a: 12,
b: 19
}
}).then(console.log)
Cloud Invoke
utilise Mini Program API in the server side (e.g. send templated message, acquire mini-program code)
Send Templated Message(模板消息)
Invoking from a mini program, but messages are sent through server
Cloud Function
async function sendTemplateMessage(event) {
// get openid of a user
const { OPENID } = cloud.getWXContext()
// constructe a function of adding message template
const addResult = await cloud.openapi.templateMessage.addTemplate({
id: 'AT0002',
keywordIdList: [3, 4, 5]
})
// template ID
const templateId = addResult.templateId
// constructe a function of sending a message with a message template
// formId: formId of the data to be sent (such as event.formId)
// page: redirect url if the message card is clicked
const sendResult = await cloud.openapi.templateMessage.send({
touser: OPENID,
templateId,
formId: event.formId,
page: 'pages/openapi/openapi',
data: {
keyword1: {
value: '未名咖啡屋',
},
keyword2: {
value: '2019 年 1 月 1 日',
},
keyword3: {
value: '拿铁',
},
}
})
// delete message template
await cloud.openapi.templateMessage.deleteTemplate({
templateId,
})
return sendResult
}
Invoke from Mini-program
wx.cloud.callFunction({
// Cloud function and data to pass
name: 'openapi',
data: {
action: 'sendTemplateMessage',
formId: e.detail.formId,
},
// if the request successes
success: res => {
console.warn('[云函数] [openapi] templateMessage.send 调用成功:', res)
wx.showModal({
title: '发送成功',
content: '请返回微信主界面查看',
showCancel: false,
})
wx.showToast({
title: '发送成功,请返回微信主界面查看',
})
this.setData({
templateMessageResult: JSON.stringify(res.result)
})
},
// if the request fails
fail: err => {
wx.showToast({
icon: 'none',
title: '调用失败',
})
console.error('[云函数] [openapi] templateMessage.send 调用失败:', err)
}
})
HTTP API
All cloud services are accessable as HTTP API
Open Data
Step 1 CloudID
wx.cloud.callFunction({
name: 'myFunction',
data: {
weRunData: wx.cloud.CloudID('xxx'), // 这个 CloudID 值到云函数端会被替换
obj: {
shareInfo: wx.cloud.CloudID('yyy'), // 非顶层字段的 CloudID 不会被替换,会原样字符串展示
}
}
})
Step 2 Cloud Invoke
// event
{
// weRunData 的值已被替换为开放数据
"weRunData": {
"cloudID": "xxx",
"data": {
"stepInfoList": [
{
"step": 5000,
"timestamp": 1554814312,
}
],
"watermark": {
"appid": "wx1111111111",
"timestamp": 1554815786
}
}
},
"obj": {
// 非顶层字段维持原样
"shareInfo": "yyy",
}
}
Customer Service
Standard
The simplest way of handling customer message is by assigning a wechat user as Customer service responder in the mini program dashboard.
Cloud Function
Once set up, customer message will be push directly to the Cloud, therefore bypass the customer service responders
Configure via the Development Tool
add a message-push record