检查过期的会员资格
Checking for expiring membership
在我的 nodejs 服务器中,我有两种类型的会员资格(免费、付费)。
付费会员资格自购买之日起有效 31 天。
当会员购买此类会员资格时,我(服务器端)UPDATE
我的数据库 (MySQL) 在字段 paid_expires
中接受 TIMESTAMP
输入当前日期加上 31 天。
如果我的服务器有 X 个付费会员在不同的日期购买了他们的会员资格,提前 3 小时通知每个人他们的会员资格到期的有效方法是什么?
有几个要素需要解决这个问题,所以我将逐一介绍它们
首先,您需要一些按时间间隔运行的函数。这可以通过 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 数据库驱动程序如何 returns 行,但我假设它是一个对象数组。您现在拥有所有付费用户及其到期日期的数组。
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
}
在我的 nodejs 服务器中,我有两种类型的会员资格(免费、付费)。 付费会员资格自购买之日起有效 31 天。
当会员购买此类会员资格时,我(服务器端)UPDATE
我的数据库 (MySQL) 在字段 paid_expires
中接受 TIMESTAMP
输入当前日期加上 31 天。
如果我的服务器有 X 个付费会员在不同的日期购买了他们的会员资格,提前 3 小时通知每个人他们的会员资格到期的有效方法是什么?
有几个要素需要解决这个问题,所以我将逐一介绍它们
首先,您需要一些按时间间隔运行的函数。这可以通过 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 数据库驱动程序如何 returns 行,但我假设它是一个对象数组。您现在拥有所有付费用户及其到期日期的数组。
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
}