log.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. console.log(error);
  21. throw new Error({ errcode: -2001, errmsg: '添加失败' });
  22. }
  23. }
  24. async del({ id }) {
  25. assert(id, 'id不存在');
  26. const { Log: model } = this.ctx.model;
  27. try {
  28. await model.findById(id).remove();
  29. return { errmsg: '', errcode: 0 };
  30. } catch (error) {
  31. throw new Error({ errcode: -2001, errmsg: '删除失败' });
  32. }
  33. }
  34. async query({ skip, limit, mondel, method, result }) {
  35. const { Log: model } = this.ctx.model;
  36. const filter = {};
  37. if (mondel) filter.mondel = mondel;
  38. if (method) filter.method = method;
  39. if (result) filter.result = result;
  40. try {
  41. const total = await model.find({ ...filter });
  42. let res;
  43. if (skip && limit) {
  44. res = await model.find({ ...filter }).skip(Number(skip) * Number(limit)).limit(Number(limit));
  45. } else {
  46. res = await model.find({ ...filter });
  47. }
  48. return { errmsg: '', errcode: 0, data: res, total: total.length };
  49. } catch (error) {
  50. console.log(error);
  51. throw new Error({ errcode: -2001, errmsg: '查询失败' });
  52. }
  53. }
  54. }
  55. module.exports = LogService;