using Infrastructure.Constant; using Newtonsoft.Json; using System.Collections.Generic; namespace Infrastructure.Model { public class ApiResult : Dictionary { /** 状态码 */ public static readonly string CODE_TAG = "code"; /** 返回内容 */ public static readonly string MSG_TAG = "msg"; /** 数据对象 */ public static readonly string DATA_TAG = "data"; public int Code { get; set; } public string Msg { get; set; } /// /// 如果data值为null,则忽略序列化将不会返回data字段 /// [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public object Data { get; set; } /// /// 初始化一个新创建的APIResult对象,使其表示一个空消息 /// public ApiResult() { } /// /// 初始化一个新创建的 ApiResult 对象 /// /// /// public ApiResult(int code, string msg) { Add(CODE_TAG, code); Add(MSG_TAG, msg); } /// /// 初始化一个新创建的 ApiResult 对象 /// /// /// /// public ApiResult(int code, string msg, object data) { Add(CODE_TAG, code); Add(MSG_TAG, msg); if (data != null) { Add(DATA_TAG, data); } } /// /// 返回成功消息 /// /// < returns > 成功消息 public static ApiResult Success() { return new ApiResult(HttpStatus.SUCCESS, "success"); } /// /// 返回成功消息 /// /// /// 成功消息 public static ApiResult Success(object data) { return new ApiResult(HttpStatus.SUCCESS, "success", data); } /// /// 返回成功消息 /// /// 返回内容 /// 成功消息 public static ApiResult Success(string msg) { return new ApiResult(HttpStatus.SUCCESS, msg, null); } /// /// 返回成功消息 /// /// 返回内容 /// 数据对象 /// 成功消息 public static ApiResult Success(string msg, object data) { return new ApiResult(HttpStatus.SUCCESS, msg, data); } public static ApiResult Error(ResultCode code, string msg = "") { return Error((int)code, msg); } /// /// 返回失败消息 /// /// /// /// public static ApiResult Error(int code, string msg) { return new ApiResult(code, msg); } /// /// 返回失败消息 /// /// /// public static ApiResult Error(string msg) { return new ApiResult((int)ResultCode.CUSTOM_ERROR, msg); } public override string ToString() { return $"msg={Msg},data={Data}"; } } public class ApiResult : ApiResult { public T Result { get; set; } } }