context.js 2.0 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. const path = require('path');
  12. module.exports = {
  13. moment,
  14. setMessage(message, type) {
  15. let icon = '';
  16. switch (type) {
  17. case messageType.SUCCESS:
  18. icon = 'check';
  19. break;
  20. case messageType.ERROR:
  21. icon = 'exclamation-circle';
  22. break;
  23. case messageType.INFO:
  24. icon = 'info-circle';
  25. break;
  26. case messageType.WARNING:
  27. icon = 'warning';
  28. break;
  29. default:
  30. break;
  31. }
  32. this.session.message = {
  33. type,
  34. icon,
  35. message,
  36. };
  37. },
  38. postError(error, msg) {
  39. if (error.stack) {
  40. this.session.postError = msg;
  41. } else {
  42. this.session.postError = error.toString();
  43. }
  44. },
  45. ajaxErrorBody(error, msg) {
  46. if (error.stack) {
  47. this.body = {err: 4, msg: msg, data: null};
  48. } else {
  49. this.body = {err: 3, msg: error.toString(), data: null};
  50. }
  51. },
  52. log(error) {
  53. if (error.stack) {
  54. this.logger.error(error);
  55. } else {
  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. };