1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- import { Provide, Scope, ScopeEnum } from '@midwayjs/decorator';
- import { InjectEntityModel } from '@midwayjs/typegoose';
- import { ReturnModelType } from '@typegoose/typegoose';
- import { BaseService } from 'free-midway-component';
- import { DataRecord } from '../../entityRecord/dataRecord.entity';
- import { get } from 'lodash';
- import dayjs = require('dayjs');
- type modelType = ReturnModelType<typeof DataRecord>;
- @Provide()
- @Scope(ScopeEnum.Request, { allowDowngrade: true })
- export class DataRecordService extends BaseService<modelType> {
- @InjectEntityModel(DataRecord)
- model: modelType;
- async makeLogs(controllerName: string, methodName: string) {
- const req = this.ctx.request;
- const user = this.ctx.user;
- const data = {
- operator: user,
- ip: get(req, 'header.x-forwarded-for', req.ip),
- device: get(req, 'header.user-agent'),
- time: dayjs().format('YYYY-MM-DD HH:mm:ss'),
- controller: controllerName,
- method: methodName,
- router: get(req, 'header.referer'),
- };
- return data;
- }
- /**
- * 为默认函数提供原数据
- * defaultMethod: create, update, delete
- * @param service 中间件提供的controller的默认service
- * @param method 当前执行函数名
- * @returns {object} 原数据
- */
- async getDefaultMethodOriginData(service: any, method: string) {
- let data = null;
- if (method === 'create') return data;
- const id = get(this.ctx.request, 'params.id');
- const modelName = get(service, 'model.modelName');
- data = await service.fetch(id);
- return { [modelName]: [data] };
- }
- /**
- * 为默认函数提供新数据
- * defaultMethod: create, update, delete
- * @param service 中间件提供的controller的默认service
- * @param method 当前执行函数名
- * @param origin_data 原数据
- * @returns {object} 新数据
- */
- async getDefaultMethodNewData(service: any, method: string, origin_data: object) {
- let data = null;
- if (method === 'delete') return data;
- const modelName = get(service, 'model.modelName');
- if (method === 'update' || method === 'status') {
- if (origin_data) {
- const list = get(origin_data, modelName);
- const arr = [];
- for (const i of list) {
- const d = await service.fetch(i._id);
- arr.push(d);
- }
- data = arr;
- }
- } else if (method === 'create') {
- data = [origin_data];
- }
- return { [modelName]: data };
- }
- }
|