using Newtonsoft.Json.Serialization; using Newtonsoft.Json; namespace HealthMonitor.WebApi.Controllers.Api { public class ApiResponse { public string Timestamp { get; set; } = default!; public T Data { get; set; } = default!; public Result Result { get; set; } = default!; //public bool Succeeded { get; set; } // public string Message { get; set; } = String.Empty; public static ApiResponse Fail(int code, string errorMessage) => new() { //MsgType = msgType, //Signature = signature, Timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), Result = new() { Status = "failed", Code = code, Message = errorMessage, }, }; //public static ApiResponse Success(string msgType,string signature,T data) => new() //{ // MsgType= msgType, // Signature = signature, // Timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), // Data = data, // Result = new() // { // Status = "succeed", // Code = 200, // Message = "请求成功!", // }, //}; public static ApiResponse Success(T data) => new() { Timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), Data = data, Result = new() { Status = "succeed", Code = 200, Message = "请求成功!", }, }; public string ToJsonString() { var settings = new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss.fff", // 设置日期格式 ContractResolver = new DefaultContractResolver { NamingStrategy = new CamelCaseNamingStrategy() } }; return JsonConvert.SerializeObject(this, settings); } } public class Result { public string Status { get; set; } = default!; public int Code { get; set; } public string Message { get; set; } = default!; } }