async function notifyUsersOfTag(tag, title, date, link, content) {
const db = cloud.database();
const usersCollection = db.collection('users');
const usersWithSubscription = await usersCollection.where({
subscriptions: tag
}).get();
if (title.length > 5) {
title = title.substring(0, 5) + '...';
}
console.log(`Attempting to notify users about tag: ${tag}`);
for (let user of usersWithSubscription.data) {
console.log(`Sending message to user: ${user.openid}`);
try {
await cloud.openapi.subscribeMessage.send({
touser: user.openid,
page: `pages/newsdetail/newsdetail?title=${encodeURIComponent(title)}&content=${encodeURIComponent(content)}&link=${encodeURIComponent(link)}`,
data: {
thing4: { value: '您订阅的师大信息' },
thing3: { value: tag },
time8: { value: date },
thing12: { value: title }
},
templateId: 'CW7AjLIuEh1-KUh4ngj0_mCHXrXvLywgks5CMfIrxkg',
miniprogramState: 'formal'
});
console.log(`Successfully sent message to user: ${user.openid}`);
} catch (err) {
console.error("Error sending message to user:", user.openid, err);
}
}
}
有报错信息吗