using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace YYApi.Communications
{
///
/// 基础相应
///
public class YYBaseResponse
{
///
/// 错误代码
///
public int Code { get; set; }
///
/// 错误描述
///
public string Message { get; set; }
///
/// 默认错误代码对应提示信息
///
private readonly Dictionary MsgBox = new Dictionary()
{
{200, "成功"},
{204, "没有数据"},
{400, "参数错误"},
{401, "需要登录"},
{403, "无权访问"},
{404, "接口不存在"},
{500, "服务器错误"}
};
///
/// 返回一个成功的空结果
///
public YYBaseResponse()
{
SetCode(200);
}
///
/// 返回指定代码的描述
///
/// 错误代码
public YYBaseResponse(int code)
{
SetCode(code);
}
///
/// 返回指定代码的结果,并自定义提示信息
///
/// 错误代码
/// 错误信息
public YYBaseResponse(int code, string message)
{
SetCodeMsg(code, message);
}
///
/// 设置响应结果的错误代码
///
/// 错误代码
public void SetCode(int code)
{
Code = code;
Message = MsgBox.ContainsKey(code) ? MsgBox[code] : "未知错误";
}
///
/// 设置响应结果的错误代码和错误描述
///
/// 错误代码
/// 错误描述
public void SetCodeMsg(int code, string message)
{
SetCode(code);
if (!string.IsNullOrEmpty(message))
{
Message = message;
}
}
///
/// 序列化为JSON字符串
///
///
public override string ToString()
{
return JsonSerializer.Serialize(this, new JsonSerializerOptions
{
IgnoreReadOnlyProperties = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters = { new JsonStringEnumConverter() }
});
}
}
///
/// 动态内容分页
///
///
public class YYBasePageResponse : YYBaseResponse
{
///
/// 动态内容列表
///
public T List { get; set; }
//当前页码
public int Page { get; set; }
//每页条数
public int Count { get; set; }
//总计条数
public int Total { get; set; }
}
///
/// 动态Data返回
///
///
public class YYBaseResponse : YYBaseResponse
{
///
/// 动态内容列表
///
public T Data { get; set; }
}
}