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