BaseController.cs 2.3 KB

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