在我的nodejs服务器中,我有两种类型的成员身份(免费,付费)。付费会员从购买之日起整整31天。
当成员购买这样的成员时,I(服务器端)在字段UPDATE my数据库(MySQL)中接受包含当前日期加上31天的TIMESTAMP输入。
如果我的服务器有X付费会员在不同的日期购买了他们的会员资格,有什么有效的方式提前3小时通知每个人他们的会员到期?
发布于 2018-04-12 12:05:50
有几个元素需要解决,所以我将逐一介绍它们。
首先,您需要一些运行在间隔上的函数。这可以由setInterval来处理。
setInterval(() => {
/**
* perform the magic here or call another function.
* Note that if you're working with classes you cannot reference "this"
* here without first setting it before the interval to a const
* for example with "const that = this;"
*/
}, 60000); // runs every minute接下来是由间隔执行的回调函数,这是所有魔术发生的地方。首先,要查询数据库中所有有付费成员资格的成员。也就是说,如果这是由paid_expires列是否是NOT NULL决定的,那么它将是SELECT memberId, paid_expires FROM member WHERE paid_expires IS NOT NULL之类的东西。下面的步骤取决于mysql数据库驱动程序如何返回行,但我假设它是一个对象数组。您现在有了一组所有付费用户及其终止日期。
假定SQL查询的输出
let members = [
{
memberId: "b6c4aeb1-6a23-477c-856a-d5f898153b62",
paid_expires: "2018-03-12T14:00:00"
},
{
memberId: "afc89eee-ef5e-4fbf-8451-aeac5620abe6",
paid_expires: "2018-03-12T16:30:00"
}
];最后一步是循环遍历这个对象数组,并计算它们是否在过期后3小时或更短的时间内。为此,您需要使用MomentJS的add、diff和duration函数。您可以使用add从数据库中为值添加31天,然后可以使用duration和diff的组合来确定是否需要发送通知。
最后一部分的例子是
const expiryDate = moment(sqlrow.paid_expires).add(31, 'd');
const timeout = moment.duration(expiryDate.diff());
if (timeout.asHours() <= 3) {
// send notification
}https://stackoverflow.com/questions/49794218
复制相似问题