global_log.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. module.exports = app => {
  10. class GlobalLog extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'global_log';
  20. }
  21. getRptTplInfo(data) {
  22. if (!data || !data.params) return '';
  23. try {
  24. const params = JSON.parse(data.params);
  25. return JSON.stringify({ rpt_tpl_id: params.rpt_tpl_id, project_id: params.project_id, tender_id: params.tender_id, stage_id: params.stage_id });
  26. } catch(err) {
  27. return '';
  28. }
  29. }
  30. getMemo(url, data) {
  31. if (url.indexOf('report_api/getReport') === -1) return '';
  32. return this.getRptTplInfo(data);
  33. }
  34. async requestLog() {
  35. if (!this.ctx.session || !this.ctx.session.sessionProject || !this.ctx.session.sessionUser) return;
  36. await this.db.insert(this.tableName, {
  37. id: this.ctx.logId,
  38. project_id: this.ctx.session.sessionProject.id,
  39. project_code: this.ctx.session.sessionProject.code,
  40. user_id: this.ctx.session.sessionUser.accountId,
  41. user_name: this.ctx.session.sessionUser.name,
  42. url: this.ctx.url,
  43. method: this.ctx.method,
  44. memo: this.getMemo(this.ctx.url, this.ctx.request.body),
  45. });
  46. }
  47. async responseLog(runTime){
  48. if (!this.ctx.session || !this.ctx.session.sessionProject || !this.ctx.session.sessionUser) return;
  49. await this.defaultUpdate({ id: this.ctx.logId, is_response: 1, run_time: runTime });
  50. }
  51. }
  52. return GlobalLog;
  53. };