intercept.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 getDeal() {
  18. await this.count();
  19. const url = this.ctx.request.url.replace('/site', '');
  20. const res = await this.httpUtil.cget(url);
  21. return res;
  22. }
  23. async postDeal() {
  24. const url = this.ctx.request.url.replace('/site', '');
  25. const body = this.ctx.request.body;
  26. const res = await this.httpUtil.cpost(url, body);
  27. return res;
  28. }
  29. async deleteDeal() {
  30. const url = this.ctx.request.url.replace('/site', '');
  31. const body = this.ctx.request.body;
  32. const res = await this.httpUtil.cdelete(url, body);
  33. return res;
  34. }
  35. /**
  36. * 添加页面访问量和客户访问量
  37. * TODO 定时任务,将redis的数据放进数据库
  38. */
  39. async count() {
  40. try {
  41. const referer = _.get(this.ctx.request, 'header.referer');
  42. const clientIp = _.get(this.ctx.request, 'header.x-real-ip');
  43. // if (clientIp.includes('192.168')) {
  44. // // 局域网随意
  45. // console.info('local area network');
  46. // return;
  47. // }
  48. const url = new URL(referer);
  49. const arr = url.pathname.split('/');
  50. if (arr.length <= 2) throw new BusinessError();
  51. const _tenant = arr[1];
  52. let project = await this.redis.get('project');
  53. if (!project) throw new BusinessError(ErrorCode.SERVICE_FAULT, '未设置中间层项目参数');
  54. project = JSON.parse(project);
  55. console.log(project);
  56. console.log(_tenant);
  57. if (!project.includes(_tenant)) throw new BusinessError();
  58. const obj = { ip: clientIp, create_time: moment().format('YYYY-MM-DD HH:mm:ss') };
  59. // 访问量的添加
  60. console.log('add visit');
  61. let hitsList = await this.redis.get(`${_tenant}/hitsList`);
  62. if (!hitsList) {
  63. hitsList = [{ ...obj, route: url.pathname }];
  64. await this.redis.set(`${_tenant}/hitsList`, JSON.stringify(hitsList));
  65. } else {
  66. hitsList = JSON.parse(hitsList);
  67. const is_in = hitsList.find(f => f.ip === obj.ip && f.create_time === obj.create_time);
  68. if (!is_in) {
  69. hitsList.push({ ...obj, route: url.pathname });
  70. await this.redis.set(`${_tenant}/hitsList`, JSON.stringify(hitsList));
  71. }
  72. }
  73. // 用户(ip)访问添加
  74. console.log('add ip');
  75. let clientList = await this.redis.get(`${_tenant}/clientList`);
  76. if (!clientList) {
  77. clientList = [ obj ];
  78. await this.redis.set(`${_tenant}/clientList`, JSON.stringify(clientList));
  79. } else {
  80. clientList = JSON.parse(clientList);
  81. const t_s = moment().format('YYYY-MM-DD');
  82. const t_e = moment().add(1, 'd').format('YYYY-MM-DD');
  83. const is_in = clientList.find(f => f.ip === obj.ip && moment(f.create_time).isBetween(t_s, t_e, null, '[]'));
  84. if (!is_in) {
  85. clientList.push(obj);
  86. await this.redis.set(`${_tenant}/clientList`, JSON.stringify(clientList));
  87. }
  88. }
  89. } catch (error) {
  90. console.log(error);
  91. throw new BusinessError(ErrorCode.ACCESS_DENIED, '非法连接!');
  92. }
  93. }
  94. async check() {
  95. let project = await this.redis.get('project');
  96. if (!project) throw new BusinessError(ErrorCode.SERVICE_FAULT, '为设置中间层项目参数');
  97. project = JSON.parse(project);
  98. for (const _tenant of project) {
  99. let hitsList = await this.redis.get(`${_tenant}/hitsList`);
  100. if (hitsList) {
  101. hitsList = JSON.parse(hitsList);
  102. const today = await this.hits.findOne({ date: moment().format('YYYY-MM-DD'), _tenant });
  103. if (today) {
  104. today.record.push(...hitsList);
  105. await today.save();
  106. } else {
  107. await this.hits.create({ date: moment().format('YYYY-MM-DD'), record: hitsList || [], _tenant });
  108. }
  109. await this.redis.del(`${_tenant}/hitsList`);
  110. }
  111. let clientList = await this.redis.get(`${_tenant}/clientList`);
  112. if (clientList) {
  113. clientList = JSON.parse(clientList);
  114. const today = await this.client.findOne({ date: moment().format('YYYY-MM-DD') });
  115. if (today) {
  116. today.record.push(...clientList);
  117. today.record = _.uniqBy(today.record, 'ip');
  118. await today.save();
  119. } else {
  120. await this.client.create({ date: moment().format('YYYY-MM-DD'), record: clientList || [], _tenant });
  121. }
  122. await this.redis.del(`${_tenant}/clientList`);
  123. }
  124. }
  125. }
  126. }
  127. module.exports = InterceptService;