Requirement: Make a subscription message alert similar to notification of lottery results
Implementation process:
Each user needs to first authorize subscription message receiving, after the authorization is successful, the data is stored in the cloud development of the data set, and then write a timer, traverse all the data of the data set, traverse after getting the subscription message, delete the data after sending success.
Let’s look at the implementation and code:
1. Create the cloud development environment, create the data set MSG, change the data permission to custom, all can be modified
2. Code for authorization and submission of user information by small program (data is the field of subscription message, user Openid, and page to which the subscription message jumps);
wx.requestSubscribeMessage({ tmplIds: ['DYvZoya_RukdkznIQchLWsD296UujyU_pCFGRXa9nnw'], success (res) { wx.cloud.callFunction({ name: "sendMsg", data: { thing5: '111', thing1: '22', name2: '33', thing6: '44', openid: 'oESJb5LTR0lu7rUn9mVpAt-xEu2k', page: 'pages/index/index,}, success (res) {the console. The log (' personal information -- -- -- -- -- -- -- -- -- -- -- -- --', res)}, Fail (err) {the console. The log (' personal information -- -- -- -- -- -- -- -- -- -- -- -- -- ', err)}})}})Copy the code
3. Create a cloud function (sendMsg) to store data.
const cloud = require('wx-server-sdk') cloud.init({ env: 'shijihyj-5g7mfb4z4011210a ' }) const db = cloud.database() exports.main = async (event, Context) => {db.collection(' MSG ').add({// data) data: {thing5: event. Thing5, thing1: event.thing1, name2: event.name2, thing6: event.thing6, openid: event.openid, page: event.page, } }) .then(res => { console.log(res) return res }) .catch(console.error) }Copy the code
4. Create the timer myTrigger
{
"permissions": {
"openapi": [
"subscribeMessage.send"
]
},
"triggers": [
{
"name": "myTrigger",
"type": "timer",
"config": "10 * * * * * *"
}
]
}
Copy the code
js
const cloud = require('wx-server-sdk') cloud.init({ env: 'shijihyj-5g7mfb4z4011210a ' }) const db = cloud.database() var sendMsg = async (item) =>{ const result = await cloud.openapi.subscribeMessage.send({ touser: item.openid, page: item.page, lang: 'zh_CN', data: { thing5: { value: item.thing5 }, thing1: { value: item.thing1 }, name2: { value: item.name2 }, thing6: { value: item.thing6 } }, templateId: 'DYvZoya_RukdkznIQchLWsD296UujyU_pCFGRXa9nnw', }) const remove = await db.collection('msg').where({ openid: item.openid }).remove() } const MAX_LIMIT = 100 exports.main = async (event, Const countResult = await db.collection(' MSG ').count() const total = countresult.total // Const batchTimes = math.ceil (total / 100) // Array of promises for all read operations const tasks = [] for (let I = 0; i < batchTimes; I ++) {const promise = db.collection(' MSG ').skip(I * MAX_LIMIT).limit(MAX_LIMIT).get() tasks.push(promise) arr = (await Promise.all(tasks)).reduce((acc, cur) => { return { data: acc.data.concat(cur.data), errMsg: acc.errMsg, } }) for(var i =0; i<arr.data.length; i++){ sendMsg(arr.data[i]) } }Copy the code
The submission is complete