global_log.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. async requestLog() {
  22. if (!this.ctx.session || !this.ctx.session.sessionProject || !this.ctx.session.sessionUser) return;
  23. await this.db.insert(this.tableName, {
  24. id: this.ctx.logId,
  25. project_id: this.ctx.session.sessionProject.id,
  26. project_code: this.ctx.session.sessionProject.code,
  27. user_id: this.ctx.session.sessionUser.accountId,
  28. user_name: this.ctx.session.sessionUser.name,
  29. url: this.ctx.url,
  30. method: this.ctx.method,
  31. });
  32. }
  33. async responseLog(runTime){
  34. if (!this.ctx.session || !this.ctx.session.sessionProject || !this.ctx.session.sessionUser) return;
  35. await this.defaultUpdate({ id: this.ctx.logId, is_response: 1, run_time: runTime });
  36. }
  37. }
  38. return GlobalLog;
  39. };