mirror of
https://github.com/Megghy/MegghysAPI.git
synced 2025-12-06 14:16:56 +08:00
92 lines
3.1 KiB
C#
92 lines
3.1 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Minio.DataModel.Result;
|
|
|
|
namespace MegghysAPI.Controllers
|
|
{
|
|
public interface IResponseResult
|
|
{
|
|
public string Message { get; set; }
|
|
public int Code { get; set; }
|
|
public object? Data { get; set; }
|
|
}
|
|
public class ResponseResult : IResponseResult
|
|
{
|
|
public ResponseResult()
|
|
{
|
|
|
|
}
|
|
public ResponseResult(object? data, int code = 200, string msg = "成功")
|
|
{
|
|
Message = msg;
|
|
Code = code;
|
|
Data = data;
|
|
}
|
|
public string Message { get; set; } = "";
|
|
public int Code { get; set; } = 0;
|
|
public object? Data { get; set; } = new();
|
|
}
|
|
public class ResponseResult<T>
|
|
{
|
|
public ResponseResult()
|
|
{
|
|
|
|
}
|
|
public ResponseResult(T? data, int code = 200, string msg = "成功")
|
|
{
|
|
Message = msg;
|
|
Code = code;
|
|
Data = data;
|
|
}
|
|
public string Message { get; set; } = "";
|
|
public int Code { get; set; } = 0;
|
|
public T? Data { get; set; } = default;
|
|
public static implicit operator ResponseResult<T>(T value)
|
|
{
|
|
return new(value);
|
|
}
|
|
public static implicit operator ResponseResult(ResponseResult<T> value)
|
|
{
|
|
return new(value.Data, value.Code, value.Message);
|
|
}
|
|
public static implicit operator ResponseResult<T>(ResponseResult result)
|
|
=> new(result.Data is null ? default : (T)result.Data, result.Code, result.Message);
|
|
}
|
|
public class PaginationResponse<T> : ResponseResult<T>
|
|
{
|
|
public PaginationResponse(T data, int pn, int ps, long total)
|
|
{
|
|
Code = 200;
|
|
Message = "成功";
|
|
Data = data;
|
|
Ps = ps;
|
|
Pn = pn;
|
|
Total = total;
|
|
More = ps * pn < total;
|
|
}
|
|
public int Ps { get; set; }
|
|
public int Pn { get; set; }
|
|
public long Total { get; set; }
|
|
public bool More { get; set; }
|
|
public static implicit operator PaginationResponse<T>(ResponseResult result)
|
|
=> new(result.Data is null ? default : (T)result.Data, -1, -1, -1);
|
|
}
|
|
public class MControllerBase : Controller
|
|
{
|
|
protected HttpContext context => ControllerContext.HttpContext;
|
|
protected ResponseResult<T> ResponseOK<T>(T? data, string msg = "成功")
|
|
=> new(data, 200, msg);
|
|
protected ResponseResult ResponseOKWithoutData(string msg = "成功")
|
|
=> new(null, 200, msg);
|
|
protected ResponseResult ResponseUnauthorized(string msg = "未认证")
|
|
=> new(null, 401, msg);
|
|
protected ResponseResult ResponseForbidden(string msg = "无权限")
|
|
=> new(null, 403, msg);
|
|
protected ResponseResult ResponseNotFound(string msg = "未找到")
|
|
=> new(null, 404, msg);
|
|
protected ResponseResult ResponseInternalError(string msg = "内部错误")
|
|
=> new(null, 500, msg);
|
|
protected ResponseResult ResponseBadRequest(string msg = "无效操作")
|
|
=> new(null, 400, msg);
|
|
}
|
|
}
|