Files
simapi-net/Communications/SimApiBaseResponse.cs
T

72 lines
1.6 KiB
C#
Raw Normal View History

2019-12-23 16:32:06 +08:00
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
2024-12-23 20:26:21 +08:00
using SimApi.Helpers;
2019-12-23 16:32:06 +08:00
2024-03-23 07:29:43 +08:00
namespace SimApi.Communications;
/// <summary>
/// 基础相应
/// </summary>
public class SimApiBaseResponse(int code = 200, string message = "成功")
2019-12-23 16:32:06 +08:00
{
public int Code { get; set; } = code;
public string Message { get; set; } = message;
2025-11-01 15:40:55 +08:00
public SimApiBaseResponse() : this(200, "成功")
{
}
/// <summary>
2024-03-23 07:29:43 +08:00
/// 默认错误代码对应提示信息
/// </summary>
2024-03-23 07:29:43 +08:00
private static readonly Dictionary<int, string> MsgBox = new()
2019-12-23 16:32:06 +08:00
{
2024-03-23 07:29:43 +08:00
{ 200, "成功" },
{ 204, "没有数据" },
{ 400, "参数错误" },
{ 401, "需要登录" },
{ 403, "无权访问" },
2026-05-05 03:53:02 +08:00
{ 404, "请求资源不存在" },
2024-03-23 07:29:43 +08:00
{ 500, "服务器错误" }
};
2019-12-23 16:32:06 +08:00
2026-05-05 03:53:02 +08:00
public SimApiBaseResponse(int code) : this(code, MsgBox.GetValueOrDefault(code, "未知错误代码"))
2024-03-23 07:29:43 +08:00
{
2019-12-23 16:32:06 +08:00
}
/// <summary>
2024-03-23 07:29:43 +08:00
/// 序列化为JSON字符串
2020-01-17 12:40:22 +08:00
/// </summary>
2024-03-23 07:29:43 +08:00
/// <returns></returns>
public override string ToString()
{
2024-12-23 20:26:21 +08:00
return SimApiUtil.Json(this);
2024-03-23 07:29:43 +08:00
}
}
2020-01-17 12:40:22 +08:00
2024-03-23 07:29:43 +08:00
/// <summary>
/// 分页内容返回
2024-03-23 07:29:43 +08:00
/// </summary>
/// <typeparam name="T"></typeparam>
public class PageResponse<T>
{
2026-06-23 17:47:42 +08:00
public T[] List { get; set; } = [];
public int Page { get; set; } = 1;
public int Count { get; set; } = 20;
public int Total { get; set; }
}
2024-03-23 07:29:43 +08:00
/// <summary>
/// 动态Data返回
/// </summary>
/// <typeparam name="T"></typeparam>
2024-04-16 06:57:56 +08:00
public class SimApiBaseResponse<T>() : SimApiBaseResponse
{
2024-08-03 19:19:42 +08:00
public T? Data { get; set; }
2024-04-16 06:57:56 +08:00
public SimApiBaseResponse(T data) : this()
{
Data = data;
}
}