log.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. const assert = require('assert');
  4. const moment = require('moment');
  5. class LogService extends Service {
  6. async create({ mondel, method, data, result, userName, acct, date }) {
  7. assert(mondel, '模块不存在');
  8. assert(method, '类型不存在');
  9. assert(data, '数据不存在');
  10. assert(result, '结果不存在');
  11. assert(userName, '操作人不存在');
  12. assert(acct, '操作帐号不存在');
  13. assert(date, '时间不存在');
  14. const { Log: model } = this.ctx.model;
  15. const createAt = moment().format('x');
  16. try {
  17. await model.create({ data, method, createAt, mondel, result, userName, acct, date });
  18. return { errmsg: '', errcode: 0 };
  19. } catch (error) {
  20. throw new Error({ errcode: -2001, errmsg: '添加失败' });
  21. }
  22. }
  23. async del({ id }) {
  24. assert(id, 'id不存在');
  25. const { Log: model } = this.ctx.model;
  26. try {
  27. await model.findById(id).remove();
  28. return { errmsg: '', errcode: 0 };
  29. } catch (error) {
  30. throw new Error({ errcode: -2001, errmsg: '删除失败' });
  31. }
  32. }
  33. async query({ skip, limit, mondel, method, result }) {
  34. const { Log: model } = this.ctx.model;
  35. const filter = {};
  36. if (mondel) filter.mondel = mondel;
  37. if (method) filter.method = method;
  38. if (result) filter.result = result;
  39. try {
  40. const total = await model.find({ ...filter });
  41. let res;
  42. if (skip && limit) {
  43. res = await model.find({ ...filter }).skip(Number(skip) * Number(limit)).limit(Number(limit));
  44. } else {
  45. res = await model.find({ ...filter });
  46. }
  47. return { errmsg: '', errcode: 0, data: res, total: total.length };
  48. } catch (error) {
  49. console.log(error);
  50. throw new Error({ errcode: -2001, errmsg: '查询失败' });
  51. }
  52. }
  53. }
  54. module.exports = LogService;