context.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const moment = require('moment');
  10. const messageType = require('../const/message_type');
  11. module.exports = {
  12. moment,
  13. setMessage(message, type) {
  14. let icon = '';
  15. switch (type) {
  16. case messageType.SUCCESS:
  17. icon = 'check';
  18. break;
  19. case messageType.ERROR:
  20. icon = 'exclamation-circle';
  21. break;
  22. case messageType.INFO:
  23. icon = 'info-circle';
  24. break;
  25. case messageType.WARNING:
  26. icon = 'warning';
  27. break;
  28. default:
  29. break;
  30. }
  31. this.session.message = {
  32. type,
  33. icon,
  34. message,
  35. };
  36. },
  37. postError(error, msg) {
  38. if (error.stack) {
  39. this.session.postError = msg;
  40. } else {
  41. this.session.postError = error.toString();
  42. }
  43. },
  44. ajaxErrorBody(error, msg) {
  45. if (error.stack) {
  46. this.body = {err: 4, msg: msg, data: null};
  47. } else {
  48. this.body = {err: 3, msg: error.toString(), data: null};
  49. }
  50. },
  51. log(error) {
  52. if (error.stack) {
  53. this.logger.error(error);
  54. } else {
  55. this.setMessage(error, messageType.ERROR);
  56. this.getLogger('fail').info(JSON.stringify({
  57. error: error,
  58. project: this.session.sessionProject,
  59. user: this.session.sessionUser,
  60. body: this.session.body,
  61. }));
  62. }
  63. },
  64. get hisOssPath() {
  65. return this.app.config.hisOssPath;
  66. },
  67. get hisOss() {
  68. return this.app.hisOss;
  69. },
  70. get stashOssPath() {
  71. return this.app.config.stashOssPath;
  72. },
  73. print(str) {
  74. this.getLogger('out').info(str);
  75. },
  76. saveTempFile(filename, text) {
  77. const filepath = path.join(this.app.config.logger.dir, this.app.config.version, filename);
  78. this.helper.saveBufferFile(text, filepath);
  79. }
  80. };