using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace SimApi.Communications;
///
/// 基础相应
///
public class SimApiBaseResponse(int code = 200, string message = "成功")
{
public int Code { get; set; } = code;
public string Message { get; set; } = message;
///
/// 默认错误代码对应提示信息
///
private static readonly Dictionary MsgBox = new()
{
{ 200, "成功" },
{ 204, "没有数据" },
{ 400, "参数错误" },
{ 401, "需要登录" },
{ 403, "无权访问" },
{ 404, "接口不存在" },
{ 500, "服务器错误" }
};
public SimApiBaseResponse(int code) : this(code, MsgBox.GetValueOrDefault(code, "未知错误"))
{
}
///
/// 序列化为JSON字符串
///
///
public override string ToString()
{
return JsonSerializer.Serialize(this, new JsonSerializerOptions
{
IgnoreReadOnlyProperties = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters =
{
new JsonStringEnumConverter()
}
});
}
}
///
/// 动态内容分页
///
///
public class SimApiBasePageResponse : SimApiBaseResponse
{
public T List { get; set; }
public int Page { get; set; } = 1;
public int Count { get; set; } = 20;
public int Total { get; set; }
}
///
/// 动态Data返回
///
///
public class SimApiBaseResponse : SimApiBaseResponse
{
public T Data { get; set; }
}