context.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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.getLogger('fail').info(JSON.stringify({
  56. error: error,
  57. project: this.session.sessionProject,
  58. user: this.session.sessionUser,
  59. body: this.session.body,
  60. }));
  61. }
  62. },
  63. get hisOssPath() {
  64. return this.app.config.hisOssPath;
  65. },
  66. get hisOss() {
  67. return this.app.hisOss;
  68. },
  69. get stashOssPath() {
  70. return this.app.config.stashOssPath;
  71. },
  72. print(str) {
  73. this.getLogger('out').info(str);
  74. },
  75. saveTempFile(filename, text) {
  76. const filepath = path.join(this.app.config.logger.dir, this.app.config.version, filename);
  77. this.helper.saveBufferFile(text, filepath);
  78. }
  79. };