intercept.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const { ObjectId } = require('mongoose').Types;
  5. const _ = require('lodash');
  6. const moment = require('moment');
  7. const assert = require('assert');
  8. // 拦截
  9. class InterceptService extends CrudService {
  10. constructor(ctx) {
  11. super(ctx, 'intercept');
  12. this.redis = this.app.redis;
  13. this.hits = this.ctx.model.Hits;
  14. this.client = this.ctx.model.Client;
  15. this.httpUtil = this.ctx.service.util.httpUtil;
  16. }
  17. async freeWebCount() {
  18. const record = {};
  19. record.ip = _.get(this.ctx.request, 'header.x-real-ip');
  20. record.route = 'http://free.waityou24.cn/';
  21. const today = moment().format('YYYY-MM-DD');
  22. const _tenant = 'freeWeb';
  23. const todayData = await this.hits.findOne({ date: today, _tenant });
  24. if (todayData) {
  25. todayData.record.push(record);
  26. await todayData.save();
  27. } else {
  28. const obj = { date: today, _tenant, record };
  29. await this.hits.create(obj);
  30. }
  31. let res = await this.hits.aggregate([
  32. { $match: { _tenant } },
  33. { $unwind: '$record' },
  34. { $group: {
  35. _id: '$_tenant',
  36. sum: { $sum: 1 },
  37. } },
  38. ]);
  39. if (res && _.isArray(res)) {
  40. res = _.get(_.head(res), 'sum', 0);
  41. }
  42. return res;
  43. }
  44. async getDeal() {
  45. await this.count();
  46. const url = this.ctx.request.url.replace('/site', '');
  47. const res = await this.httpUtil.cget(url);
  48. return res;
  49. }
  50. async postDeal() {
  51. const url = this.ctx.request.url.replace('/site', '');
  52. const body = this.ctx.request.body;
  53. const res = await this.httpUtil.cpost(url, body);
  54. return res;
  55. }
  56. async deleteDeal() {
  57. const url = this.ctx.request.url.replace('/site', '');
  58. const body = this.ctx.request.body;
  59. const res = await this.httpUtil.cdelete(url, body);
  60. return res;
  61. }
  62. /**
  63. * 添加页面访问量和客户访问量
  64. * TODO 定时任务,将redis的数据放进数据库
  65. */
  66. async count() {
  67. try {
  68. const referer = _.get(this.ctx.request, 'header.referer');
  69. const clientIp = _.get(this.ctx.request, 'header.x-real-ip');
  70. // if (clientIp.includes('192.168')) {
  71. // // 局域网随意
  72. // console.info('local area network');
  73. // return;
  74. // }
  75. const url = new URL(referer);
  76. const arr = url.pathname.split('/');
  77. if (arr.length <= 2) throw new BusinessError();
  78. const _tenant = arr[1];
  79. let project = await this.redis.get('project');
  80. if (!project) throw new BusinessError(ErrorCode.SERVICE_FAULT, '未设置中间层项目参数');
  81. project = JSON.parse(project);
  82. if (!project.includes(_tenant)) throw new BusinessError();
  83. const obj = { ip: clientIp, create_time: moment().format('YYYY-MM-DD HH:mm:ss') };
  84. // 访问量的添加
  85. let hitsList = await this.redis.get(`${_tenant}/hitsList`);
  86. if (!hitsList) {
  87. hitsList = [{ ...obj, route: url.pathname }];
  88. await this.redis.set(`${_tenant}/hitsList`, JSON.stringify(hitsList));
  89. } else {
  90. hitsList = JSON.parse(hitsList);
  91. const is_in = hitsList.find(f => f.ip === obj.ip && f.create_time === obj.create_time);
  92. if (!is_in) {
  93. hitsList.push({ ...obj, route: url.pathname });
  94. await this.redis.set(`${_tenant}/hitsList`, JSON.stringify(hitsList));
  95. }
  96. }
  97. // 用户(ip)访问添加
  98. let clientList = await this.redis.get(`${_tenant}/clientList`);
  99. if (!clientList) {
  100. clientList = [ obj ];
  101. await this.redis.set(`${_tenant}/clientList`, JSON.stringify(clientList));
  102. } else {
  103. clientList = JSON.parse(clientList);
  104. const t_s = moment().format('YYYY-MM-DD');
  105. const t_e = moment().add(1, 'd').format('YYYY-MM-DD');
  106. const is_in = clientList.find(f => f.ip === obj.ip && moment(f.create_time).isBetween(t_s, t_e, null, '[]'));
  107. if (!is_in) {
  108. clientList.push(obj);
  109. await this.redis.set(`${_tenant}/clientList`, JSON.stringify(clientList));
  110. }
  111. }
  112. } catch (error) {
  113. console.log(error);
  114. throw new BusinessError(ErrorCode.ACCESS_DENIED, '非法连接!');
  115. }
  116. }
  117. async check() {
  118. let project = await this.redis.get('project');
  119. if (!project) throw new BusinessError(ErrorCode.SERVICE_FAULT, '为设置中间层项目参数');
  120. project = JSON.parse(project);
  121. for (const _tenant of project) {
  122. let hitsList = await this.redis.get(`${_tenant}/hitsList`);
  123. if (hitsList) {
  124. hitsList = JSON.parse(hitsList);
  125. const today = await this.hits.findOne({ date: moment().format('YYYY-MM-DD'), _tenant });
  126. if (today) {
  127. today.record.push(...hitsList);
  128. await today.save();
  129. } else {
  130. await this.hits.create({ date: moment().format('YYYY-MM-DD'), record: hitsList || [], _tenant });
  131. }
  132. await this.redis.del(`${_tenant}/hitsList`);
  133. }
  134. let clientList = await this.redis.get(`${_tenant}/clientList`);
  135. if (clientList) {
  136. clientList = JSON.parse(clientList);
  137. const today = await this.client.findOne({ date: moment().format('YYYY-MM-DD') });
  138. if (today) {
  139. today.record.push(...clientList);
  140. today.record = _.uniqBy(today.record, 'ip');
  141. await today.save();
  142. } else {
  143. await this.client.create({ date: moment().format('YYYY-MM-DD'), record: clientList || [], _tenant });
  144. }
  145. await this.redis.del(`${_tenant}/clientList`);
  146. }
  147. }
  148. }
  149. }
  150. module.exports = InterceptService;