guhongwei 2 سال پیش
والد
کامیت
7241e8f08a
2فایلهای تغییر یافته به همراه27 افزوده شده و 14 حذف شده
  1. 13 0
      app/schedule/user.js
  2. 14 14
      app/service/user.js

+ 13 - 0
app/schedule/user.js

@@ -0,0 +1,13 @@
+'use strict';
+module.exports = {
+  schedule: {
+    interval: '30s', // 1 分钟间隔
+    // cron: '0 0 23 * * *', // 每天晚上十一点
+    // cron: '5 * * * * ?', // 30s
+    type: 'worker', // 指定所有的 worker 都需要执行
+  },
+  async task(ctx) {
+    console.log('11');
+    // ctx.service.user.watchvip();
+  },
+};

+ 14 - 14
app/service/user.js

@@ -45,20 +45,20 @@ class UserService extends CrudService {
     return token;
   }
   // 监听用户vip信息
-  // async watchvip() {
-  //   const list = [];
-  //   const data = await this.model.find();
-  //   for (const val of data) {
-  //     if (val.is_vip === '1') {
-  //       if (moment().isAfter(val.vip_end_time)) list.push(val);
-  //     }
-  //   }
-  //   if (list.length > 0) {
-  //     for (const val of list) {
-  //       await this.model.updateOne({ _id: ObjectId(val._id) }, { is_vip: '0' });
-  //     }
-  //   }
-  // }
+  async watchvip() {
+    const list = [];
+    const data = await this.model.find();
+    for (const val of data) {
+      if (val.is_vip === '1') {
+        if (moment().isAfter(val.vip_end_time)) list.push(val);
+      }
+    }
+    if (list.length > 0) {
+      for (const val of list) {
+        await this.model.updateOne({ _id: ObjectId(val._id) }, { is_vip: '0' });
+      }
+    }
+  }
 }
 
 module.exports = UserService;