lrf402788946 4 years ago
parent
commit
2238f6d75f
4 changed files with 35 additions and 1 deletions
  1. 4 0
      app/controller/intercept.js
  2. 1 0
      app/router.js
  3. 29 0
      app/service/intercept.js
  4. 1 1
      config/config.prod.js

+ 4 - 0
app/controller/intercept.js

@@ -21,5 +21,9 @@ class InterceptController extends Controller {
     const res = await this.service.deleteDeal();
     this.ctx.body = res;
   }
+  async freeWeb() {
+    const res = await this.service.freeWebCount();
+    this.ctx.body = res;
+  }
 }
 module.exports = CrudController(InterceptController, {});

+ 1 - 0
app/router.js

@@ -6,6 +6,7 @@
 module.exports = app => {
   const { router, controller } = app;
   router.get('/', controller.home.index);
+  router.get('/site/freeWeb', controller.intercept.freeWeb);
   router.get(/^\/site*/, controller.intercept.get);
   router.post(/^\/site*/, controller.intercept.post);
   router.delete(/^\/site*/, controller.intercept.delete);

+ 29 - 0
app/service/intercept.js

@@ -16,6 +16,35 @@ class InterceptService extends CrudService {
     this.httpUtil = this.ctx.service.util.httpUtil;
   }
 
+  async freeWebCount() {
+    const record = {};
+    record.ip = _.get(this.ctx.request, 'header.x-real-ip');
+    record.route = 'http://free.waityou24.cn/';
+    const today = moment().format('YYYY-MM-DD');
+    const _tenant = 'freeWeb';
+    const todayData = await this.hits.findOne({ date: today, _tenant });
+    if (todayData) {
+      todayData.record.push(record);
+      await todayData.save();
+    } else {
+      const obj = { date: today, _tenant, record };
+      await this.hits.create(obj);
+    }
+    let res = await this.hits.aggregate([
+      { $match: { _tenant } },
+      { $unwind: '$record' },
+      { $group: {
+        _id: '$_tenant',
+        sum: { $sum: 1 },
+      } },
+    ]);
+    if (res && _.isArray(res)) {
+      res = _.get(_.head(res), 'sum', 0);
+    }
+    return res;
+  }
+
+
   async getDeal() {
     await this.count();
     const url = this.ctx.request.url.replace('/site', '');

+ 1 - 1
config/config.prod.js

@@ -7,7 +7,7 @@ module.exports = () => {
     level: 'INFO',
     consoleLevel: 'INFO',
   };
-  config.dbName = 'new-platform';
+  config.dbName = 'statistics';
   config.mongoose = {
     url: `mongodb://localhost:27017/${config.dbName}`,
     options: {