BaseController.ts 1017 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. const Controller = require('egg').Controller;
  2. class BaseController extends Controller {
  3. /**
  4. * 功能:统一数据返回接口
  5. * @param data
  6. * @param message
  7. * @param code
  8. * @param httpCode
  9. * @private
  10. */
  11. #response(data: any, message: string, code: number, httpCode: number) {
  12. const { ctx } = this;
  13. ctx.body = {
  14. DATA: data,
  15. CODE: code,
  16. MESSAGE: message,
  17. };
  18. ctx.status = httpCode;
  19. return;
  20. }
  21. /**
  22. * 功能: 成功返回函数
  23. * @param data
  24. * @param message
  25. */
  26. success(data: any, message = '') {
  27. return this.#response(data, message, 0, 200);
  28. }
  29. /**
  30. * 功能:失败返回接口
  31. * @param message
  32. * @param code
  33. * @param httpCode
  34. */
  35. fail(message: string, code = 400, httpCode = 400) {
  36. return this.#response(undefined, message, code, httpCode);
  37. }
  38. }
  39. // module.exports = BaseController;
  40. export default BaseController;