/** * @ClassName: ApiResult * @Description: 返回结果集封装 * @Author: wu * @Date: 2020/06/12 * @Version 1.0 **/ public class ApiResult { public string Status { get; set; } public string Message { get; set; } public object Data { get; set; } private ApiResult(ResultCode resultCode) { Status = resultCode.GetValue<string>(); Message = resultCode.GetAttributeValue(); } public ApiResult(ResultCode resultCode, object rtnData = null) { Status = resultCode.GetValue<int>().ToString(); Message = resultCode.GetAttributeValue(); Data = rtnData ?? string.Empty; } public ApiResult(string responseCode, string responseMessage, object rtnData = null) { Status = responseCode; Message = responseMessage ?? string.Empty; Data = rtnData ?? string.Empty; } }
定义响应枚举状态码类
/** * @Author wu * @Description 响应枚举状态码 * @Date 2020/06/12 **/ public enum ResultCode { /// <summary> /// 成功 /// </summary> [Desc("成功")] SUCCESS = 200, /// <summary> /// 失败 /// </summary> [Desc("失败")] FALL = 9999, //缺少token或者会话信息 /// <summary> /// 未认证(签名错误) /// </summary> [Desc("未认证(签名错误)")] UNAUTHORIZED = 9001, /// <summary> /// 查询无记录 /// </summary> [Desc("查询无记录")] NO_RECORD = 9002, /// <summary> /// 缺少必须的请求参数 /// </summary> [Desc("缺少必须的请求参数")] REQUEST_PARAM_ERROR = 9003, /// <summary> /// 找不到相关请求 /// </summary> [Desc("找不到相关请求")] NOT_FOUND = 404, /// <summary> /// 服务器内部错误 /// </summary> [Desc("服务器内部错误")] INTERNAL_SERVER_ERROR = 500, /// <summary> /// 权限不足 /// </summary> [Desc("权限不足")] ACCESS_DENIED = 4301, /// <summary> /// 用户名不存在 /// </summary> [Desc("用户名不存在")] LOGIN_NAME_ERROR = 4302, /// <summary> /// 密码错误 /// </summary> [Desc("密码错误")] LOGIN_PASSWORD_ERROR = 4304, /// <summary> /// 不合法的token验证 /// </summary> [Desc("不合法的token验证")] AUTH_HEADER_ERROR = 4305, /// <summary> /// 无token信息,访问当前资源请先登录 /// </summary> [Desc("无token信息,访问当前资源请先登录")] TOKEN_NEEDED = 4306, /// <summary> /// token已过期,请重新登录 /// </summary> [Desc("token已过期,请重新登录")] TOKEN_EXPIRED = 4307, /// <summary> /// token已被刷新,用户在其他地方登录 /// </summary> [Desc("token已被刷新,用户在其他地方登录")] TOKEN_REFRESHED = 4308, /// <summary> /// 用户已在其他设备登录 /// </summary> [Desc("用户已在其他设备登录")] ALREADY_LOGIN = 4309, }
扩展枚举操作:
/// <summary> /// ** 描述:枚举扩展函数 /// ** 创始时间:2020-9-18 13:16:05 /// ** 修改时间:- /// ** 作者:child /// ** 使用说明: /// </summary> public static class CustomEnumExtenions { /// <summary> /// 获得枚举字段的特性(Attribute),该属性不允许多次定义。 /// </summary> public static string GetAttributeValue(this Enum thisValue) { FieldInfo field = thisValue.GetType().GetField(thisValue.ToString()); var attr = (Attribute.GetCustomAttribute(field, typeof(Desc)) as Desc); if (attr == null) return string.Empty; return (Attribute.GetCustomAttribute(field, typeof(Desc)) as Desc).Value; } /// <summary> /// 获得枚举字段的特性(Attribute),该属性不允许多次定义。 /// </summary> public static T GetAttribute<T>(this Enum thisValue) where T : class { FieldInfo field = thisValue.GetType().GetField(thisValue.ToString()); var attr = (Attribute.GetCustomAttribute(field, typeof(T)) as T); return attr; } /// <summary> /// 获得枚举字段的名称。 /// </summary> /// <returns></returns> public static string GetName(this Enum thisValue) { return Enum.GetName(thisValue.GetType(), thisValue); } /// <summary> /// 获得枚举字段的值。 /// </summary> /// <returns></returns> public static T GetValue<T>(this Enum thisValue) { return (T)Enum.Parse(thisValue.GetType(), thisValue.ToString()); } } /// <summary> /// 字段或属性的中文解释属性 /// </summary> [Serializable] [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)] public class Desc : Attribute { /// <summary> /// 获得字段或属性的中文解释. /// </summary> /// <value>字段或属性的中文解释.</value> public string Value { get; private set; } /// <summary> /// 初始化创建一个 <see cref="Desc"/> 类的实例, 用于指示字段或属性的解释说明. /// </summary> /// <param name="value">字段或属性的解释说明.</param> public Desc(string value) { Value = value; } }
用法:
public ApiResult GetTest() { try { return new ApiResult(ResultCode.SUCCESS, usersService.GetList()); } catch (Exception ex) { _logger.LogError(ex.ToString()); return null; } }
评论区