BaseController.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System.Security.Claims;
  2. namespace OASystem.API.Controllers;
  3. /// <summary>
  4. /// 控制器基类
  5. /// </summary>
  6. //[Authorize]
  7. [ApiController]
  8. public class ControllerBase : Microsoft.AspNetCore.Mvc.ControllerBase
  9. {
  10. protected virtual string? CurrentUserId => HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
  11. protected virtual void Logs(string str)
  12. {
  13. Log.Error(str);
  14. }
  15. protected virtual JsonView JsonView(object obj)
  16. {
  17. return new JsonView { Code = StatusCodes.Status200OK, Msg = "操作成功", Data = obj };
  18. }
  19. protected virtual JsonView JsonView(object obj, int count)
  20. {
  21. return new JsonView { Code = StatusCodes.Status200OK, Msg = "操作成功", Data = obj, Count = count };
  22. }
  23. protected virtual JsonView JsonView(string msg)
  24. {
  25. return new JsonView { Code = StatusCodes.Status400BadRequest, Msg = msg };
  26. }
  27. protected virtual JsonView JsonView(bool s)
  28. {
  29. if (s)
  30. {
  31. return new JsonView { Code = StatusCodes.Status200OK, Msg = "操作成功" };
  32. }
  33. else
  34. {
  35. return new JsonView { Code = StatusCodes.Status400BadRequest, Msg = "操作失败" };
  36. }
  37. }
  38. protected virtual JsonView JsonView(bool s, string msg)
  39. {
  40. if (s)
  41. {
  42. return new JsonView { Code = StatusCodes.Status200OK, Msg = msg };
  43. }
  44. else
  45. {
  46. return new JsonView { Code = StatusCodes.Status400BadRequest, Msg = msg };
  47. }
  48. }
  49. protected virtual JsonView JsonView(bool s, string msg, object obj)
  50. {
  51. if (s)
  52. {
  53. return new JsonView { Code = StatusCodes.Status200OK, Msg = msg, Data = obj };
  54. }
  55. else
  56. {
  57. return new JsonView { Code = StatusCodes.Status400BadRequest, Msg = msg };
  58. }
  59. }
  60. protected virtual JsonView JsonView(bool s, string msg, object obj, int count)
  61. {
  62. if (s)
  63. {
  64. return new JsonView { Code = StatusCodes.Status200OK, Msg = msg, Data = obj, Count = count };
  65. }
  66. else
  67. {
  68. return new JsonView { Code = StatusCodes.Status400BadRequest, Msg = msg };
  69. }
  70. }
  71. protected virtual JsonView JsonView(int code, string msg, object obj)
  72. {
  73. return new JsonView { Code = code, Msg = msg, Data = obj };
  74. }
  75. }