Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

75 lines
2.2KB

  1. using Newtonsoft.Json.Serialization;
  2. using Newtonsoft.Json;
  3. namespace HealthMonitor.WebApi.Controllers.Api
  4. {
  5. public class ApiResponse<T>
  6. {
  7. public string Timestamp { get; set; } = default!;
  8. public T Data { get; set; } = default!;
  9. public Result Result { get; set; } = default!;
  10. //public bool Succeeded { get; set; }
  11. // public string Message { get; set; } = String.Empty;
  12. public static ApiResponse<T> Fail(int code, string errorMessage) => new()
  13. {
  14. //MsgType = msgType,
  15. //Signature = signature,
  16. Timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"),
  17. Result = new()
  18. {
  19. Status = "failed",
  20. Code = code,
  21. Message = errorMessage,
  22. },
  23. };
  24. //public static ApiResponse<T> Success(string msgType,string signature,T data) => new()
  25. //{
  26. // MsgType= msgType,
  27. // Signature = signature,
  28. // Timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"),
  29. // Data = data,
  30. // Result = new()
  31. // {
  32. // Status = "succeed",
  33. // Code = 200,
  34. // Message = "请求成功!",
  35. // },
  36. //};
  37. public static ApiResponse<T> Success(T data) => new()
  38. {
  39. Timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"),
  40. Data = data,
  41. Result = new()
  42. {
  43. Status = "succeed",
  44. Code = 200,
  45. Message = "请求成功!",
  46. },
  47. };
  48. public string ToJsonString()
  49. {
  50. var settings = new JsonSerializerSettings
  51. {
  52. DateFormatString = "yyyy-MM-dd HH:mm:ss.fff", // 设置日期格式
  53. ContractResolver = new DefaultContractResolver { NamingStrategy = new CamelCaseNamingStrategy() }
  54. };
  55. return JsonConvert.SerializeObject(this, settings);
  56. }
  57. }
  58. public class Result
  59. {
  60. public string Status { get; set; } = default!;
  61. public int Code { get; set; }
  62. public string Message { get; set; } = default!;
  63. }
  64. }