using Newtonsoft.Json; using System.ComponentModel; namespace ZRModel { /// /// 通用json格式实体返回类 /// Author by zhaorui /// public class ApiResult { public int Code { get; set; } public string Msg { get; set; } /// /// 如果data值为null,则忽略序列化将不会返回data字段 /// [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public object Data { get; set; } public ApiResult() { Code = 0; Msg = "fail"; } public ApiResult OnSuccess() { Code = 100; Msg = "success"; return this; } public ApiResult(int code, string msg, object data = null) { Code = code; Msg = msg; Data = data; } public ApiResult OnSuccess(object data = null) { Code = (int)ResultCode.SUCCESS; Msg = "SUCCESS"; Data = data; return this; } /// /// 返回成功消息 /// /// 数据对象 /// 成功消息 public static ApiResult Success(object data) { return new ApiResult(100, "success", data); } /// /// 返回成功消息 /// /// 返回内容 /// 成功消息 public static ApiResult Success(string msg) { return new ApiResult(100, msg, null); } /// /// 返回成功消息 /// /// 返回内容 /// 数据对象 /// 成功消息 public static ApiResult Success(string msg, object data) { return new ApiResult(100, msg, data); } /// /// 返回失败消息 /// /// /// /// public static ApiResult Error(int code, string msg) { return new ApiResult(code, msg); } /// /// 返回失败消息 /// /// /// public static ApiResult Error(string msg) { return new ApiResult(200, msg); } } public enum ResultCode { [Description("success")] SUCCESS = 100, [Description("参数错误")] PARAM_ERROR = 101, [Description("自定义错误")] CUSTOM_ERROR = 200, FAIL = 0, [Description("程序出错啦")] GLOBAL_ERROR = 104, [Description("请先绑定手机号")] NOBIND_PHONENUM = 102, [Description("授权失败")] OAUTH_FAIL = 201, [Description("未授权")] DENY = 401 } }