|
@@ -0,0 +1,132 @@
|
|
|
+'use strict';
|
|
|
+const { CrudService } = require('naf-framework-mongoose/lib/service');
|
|
|
+const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
|
+const { ObjectId } = require('mongoose').Types;
|
|
|
+const _ = require('lodash');
|
|
|
+const moment = require('moment');
|
|
|
+const assert = require('assert');
|
|
|
+
|
|
|
+// 拦截
|
|
|
+class InterceptService extends CrudService {
|
|
|
+ constructor(ctx) {
|
|
|
+ super(ctx, 'intercept');
|
|
|
+ this.redis = this.app.redis;
|
|
|
+ this.hits = this.ctx.model.Hits;
|
|
|
+ this.client = this.ctx.model.Client;
|
|
|
+ this.httpUtil = this.ctx.service.util.httpUtil;
|
|
|
+ }
|
|
|
+
|
|
|
+ async getDeal() {
|
|
|
+ await this.count();
|
|
|
+ const url = this.ctx.request.url.replace('/site', '');
|
|
|
+ const res = await this.httpUtil.cget(url);
|
|
|
+ return res;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ async postDeal() {
|
|
|
+ const url = this.ctx.request.url.replace('/site', '');
|
|
|
+ const body = this.ctx.request.body;
|
|
|
+ const res = await this.httpUtil.cpost(url, body);
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+ async deleteDeal() {
|
|
|
+ const url = this.ctx.request.url.replace('/site', '');
|
|
|
+ const body = this.ctx.request.body;
|
|
|
+ const res = await this.httpUtil.cdelete(url, body);
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加页面访问量和客户访问量
|
|
|
+ * TODO 定时任务,将redis的数据放进数据库
|
|
|
+ */
|
|
|
+ async count() {
|
|
|
+ try {
|
|
|
+ const referer = _.get(this.ctx.request, 'header.referer');
|
|
|
+ const clientIp = _.get(this.ctx.request, 'header.x-real-ip');
|
|
|
+ // if (clientIp.includes('192.168')) {
|
|
|
+ // // 局域网随意
|
|
|
+ // console.info('local area network');
|
|
|
+ // return;
|
|
|
+ // }
|
|
|
+ const url = new URL(referer);
|
|
|
+ const arr = url.pathname.split('/');
|
|
|
+ if (arr.length <= 2) throw new BusinessError();
|
|
|
+ const _tenant = arr[1];
|
|
|
+ let project = await this.redis.get('project');
|
|
|
+ if (!project) throw new BusinessError(ErrorCode.SERVICE_FAULT, '为设置中间层项目参数');
|
|
|
+ project = JSON.parse(project);
|
|
|
+ if (!project.includes(_tenant)) throw new BusinessError();
|
|
|
+ const obj = { ip: clientIp, create_time: moment().format('YYYY-MM-DD HH:mm:ss') };
|
|
|
+ // 访问量的添加
|
|
|
+ let hitsList = await this.redis.get(`${_tenant}/hitsList`);
|
|
|
+ if (!hitsList) {
|
|
|
+ hitsList = [{ ...obj, route: url.pathname }];
|
|
|
+ await this.redis.set(`${_tenant}/hitsList`, JSON.stringify(hitsList));
|
|
|
+ } else {
|
|
|
+ hitsList = JSON.parse(hitsList);
|
|
|
+ const is_in = hitsList.find(f => f.ip === obj.ip && f.create_time === obj.create_time);
|
|
|
+ if (!is_in) {
|
|
|
+ hitsList.push({ ...obj, route: url.pathname });
|
|
|
+ await this.redis.set(`${_tenant}/hitsList`, JSON.stringify(hitsList));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 用户(ip)访问添加
|
|
|
+ let clientList = await this.redis.get(`${_tenant}/clientList`);
|
|
|
+ if (!clientList) {
|
|
|
+ clientList = [ obj ];
|
|
|
+ await this.redis.set(`${_tenant}/clientList`, JSON.stringify(clientList));
|
|
|
+ } else {
|
|
|
+ clientList = JSON.parse(clientList);
|
|
|
+ const t_s = moment().format('YYYY-MM-DD');
|
|
|
+ const t_e = moment().add(1, 'd').format('YYYY-MM-DD');
|
|
|
+ const is_in = clientList.find(f => f.ip === obj.ip && moment(f.create_time).isBetween(t_s, t_e, null, '[]'));
|
|
|
+ if (!is_in) {
|
|
|
+ clientList.push(obj);
|
|
|
+ await this.redis.set(`${_tenant}/clientList`, JSON.stringify(clientList));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ console.log(error);
|
|
|
+ throw new BusinessError(ErrorCode.ACCESS_DENIED, '非法连接!');
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ async check() {
|
|
|
+ let project = await this.redis.get('project');
|
|
|
+ if (!project) throw new BusinessError(ErrorCode.SERVICE_FAULT, '为设置中间层项目参数');
|
|
|
+ project = JSON.parse(project);
|
|
|
+ for (const _tenant of project) {
|
|
|
+ let hitsList = await this.redis.get(`${_tenant}/hitsList`);
|
|
|
+ if (hitsList) {
|
|
|
+ hitsList = JSON.parse(hitsList);
|
|
|
+ const today = await this.hits.findOne({ date: moment().format('YYYY-MM-DD'), _tenant });
|
|
|
+ if (today) {
|
|
|
+ today.record.push(...hitsList);
|
|
|
+ await today.save();
|
|
|
+ } else {
|
|
|
+ await this.hits.create({ date: moment().format('YYYY-MM-DD'), record: hitsList || [], _tenant });
|
|
|
+ }
|
|
|
+ await this.redis.del(`${_tenant}/hitsList`);
|
|
|
+ }
|
|
|
+ let clientList = await this.redis.get(`${_tenant}/clientList`);
|
|
|
+ if (clientList) {
|
|
|
+ clientList = JSON.parse(clientList);
|
|
|
+ const today = await this.client.findOne({ date: moment().format('YYYY-MM-DD') });
|
|
|
+ if (today) {
|
|
|
+ today.record.push(...clientList);
|
|
|
+ today.record = _.uniqBy(today.record, 'ip');
|
|
|
+ await today.save();
|
|
|
+ } else {
|
|
|
+ await this.client.create({ date: moment().format('YYYY-MM-DD'), record: clientList || [], _tenant });
|
|
|
+ }
|
|
|
+ await this.redis.del(`${_tenant}/clientList`);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+module.exports = InterceptService;
|