YD_XinWei.Api/Server/YD_XinWei.Commons/Dto/WebResponseContent.cs

80 lines
1.8 KiB
C#
Raw Normal View History

2026-04-22 13:41:49 +08:00
using YD_XinWei.Commons.Enum;
2025-01-14 14:51:07 +08:00
namespace YD_XinWei.Commons.Dto
{
public class WebResponseContent
{
2026-04-22 13:41:49 +08:00
private string _errorMsg = "";
public bool Success { get; set; }
public string ErrorCode { get; set; } = "";
public string ErrorMsg
2025-01-14 14:51:07 +08:00
{
2026-04-22 13:41:49 +08:00
get => _errorMsg;
set => _errorMsg = value ?? "";
2025-01-14 14:51:07 +08:00
}
2026-04-22 13:41:49 +08:00
2025-01-14 14:51:07 +08:00
public object Data { get; set; }
2026-04-22 13:41:49 +08:00
public WebResponseContent()
2025-01-14 14:51:07 +08:00
{
2026-04-22 13:41:49 +08:00
ErrorMsg = "";
2025-01-14 14:51:07 +08:00
}
2026-04-22 13:41:49 +08:00
public WebResponseContent(bool success)
2025-01-14 14:51:07 +08:00
{
2026-04-22 13:41:49 +08:00
Success = success;
ErrorMsg = "";
2025-01-14 14:51:07 +08:00
}
2026-04-22 13:41:49 +08:00
public static WebResponseContent Instance => new WebResponseContent();
public WebResponseContent OK(object data = null, string msg = "")
2025-01-14 14:51:07 +08:00
{
2026-04-22 13:41:49 +08:00
Success = true;
Data = data;
ErrorMsg = msg ?? "";
2025-01-14 14:51:07 +08:00
return this;
}
2026-04-22 13:41:49 +08:00
public WebResponseContent OK(ResponseType type)
2025-01-14 14:51:07 +08:00
{
2026-04-22 13:41:49 +08:00
return Set(type, null, true);
2025-01-14 14:51:07 +08:00
}
2026-04-22 13:41:49 +08:00
public WebResponseContent Error(string msg = "")
2025-01-14 14:51:07 +08:00
{
2026-04-22 13:41:49 +08:00
Success = false;
ErrorMsg = msg ?? "";
2025-01-14 14:51:07 +08:00
return this;
}
2026-04-22 13:41:49 +08:00
public WebResponseContent Error(ResponseType type)
2025-01-14 14:51:07 +08:00
{
2026-04-22 13:41:49 +08:00
return Set(type, null, false);
2025-01-14 14:51:07 +08:00
}
2026-04-22 13:41:49 +08:00
public WebResponseContent Set(ResponseType type, string msg = null, bool? success = null)
2025-01-14 14:51:07 +08:00
{
2026-04-22 13:41:49 +08:00
if (success.HasValue)
2025-01-14 14:51:07 +08:00
{
2026-04-22 13:41:49 +08:00
Success = success.Value;
2025-01-14 14:51:07 +08:00
}
2026-04-22 13:41:49 +08:00
ErrorCode = ((int)type).ToString();
2025-01-14 14:51:07 +08:00
if (!string.IsNullOrEmpty(msg))
{
ErrorMsg = msg;
}
2026-04-22 13:41:49 +08:00
else
{
ErrorMsg = type.GetMsg() ?? "";
}
2025-01-14 14:51:07 +08:00
return this;
}
}
2026-04-22 13:41:49 +08:00
}