context.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. // 所有附件都存储于fujianOss
  65. get fujianOss() {
  66. return this.app.fujianOss;
  67. },
  68. get fujianOssPath() {
  69. return this.app.config.fujianOssFolder;
  70. },
  71. // his和stash都存储于hisOss下,路径不同
  72. get hisOss() {
  73. return this.app.hisOss;
  74. },
  75. get hisOssPath() {
  76. return this.app.config.hisOssPath;
  77. },
  78. get stashOssPath() {
  79. return this.app.config.stashOssPath;
  80. },
  81. print(str) {
  82. this.getLogger('out').info(str);
  83. },
  84. saveTempFile(filename, text) {
  85. const filepath = path.join(this.app.config.logger.dir, this.app.config.version, filename);
  86. this.helper.saveBufferFile(text, filepath);
  87. }
  88. };