You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
4.3 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using EntrustSettle.Common.Const;
using EntrustSettle.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace EntrustSettle.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize]
[ApiExplorerSettings(GroupName = ApiGroupNameConst.Business)]
public class BaseApiController : Controller
{
#region 常用的 Http Route templete 后缀,影响所有接口,谨慎修改
protected const string ActionList = "list";
protected const string ActionDetail = "detail";
protected const string ActionAdd = "add";
protected const string ActionEdit = "edit";
protected const string ActionDelete = "delete";
#endregion
#region 构建返回数据
/// <summary>
/// 返回一个成功提示,但不包含数据
/// </summary>
[NonAction]
protected MessageModel SuccessMsg(string msg = "成功")
{
return new MessageModel()
{
code = 200,
success = true,
msg = msg
};
}
/// <summary>
/// 因某项原因导致流程没有完全进行下去,但是并没有到失败的消息级别
/// </summary>
[NonAction]
protected MessageModel<T> Success<T>(int code, string msg = "成功")
{
return new MessageModel<T>()
{
code = code,
success = true,
msg = msg
};
}
/// <summary>
/// 返回一个成功提示,包含数据
/// </summary>
[NonAction]
protected MessageModel<T> Success<T>(T data, string msg = "成功")
{
return new MessageModel<T>()
{
code = 200,
success = true,
msg = msg,
data = data,
};
}
/// <summary>
/// 返回一个错误提示,但不包含数据
/// </summary>
[NonAction]
protected MessageModel FailedMsg(string msg = "失败", int code = 500)
{
return new MessageModel()
{
code = code,
success = false,
msg = msg
};
}
/// <summary>
/// 返回一个错误提示可以自定义错误代码data为T的默认值
/// </summary>
[NonAction]
protected MessageModel<T> Failed<T>(int code, string msg = "失败")
{
return new MessageModel<T>()
{
code = code,
success = false,
msg = msg,
data = default,
};
}
/// <summary>
/// 返回一个错误提示code为500data为T的默认值
/// </summary>
[NonAction]
protected MessageModel<T> Failed<T>(string msg = "失败")
{
return new MessageModel<T>()
{
code = 500,
success = false,
msg = msg,
data = default,
};
}
[NonAction]
protected MessageModel<PageModel<T>> SuccessPage<T>(List<T> data, int dataCount, int page, int pageSize, string msg = "成功")
{
return new MessageModel<PageModel<T>>()
{
code = 200,
success = true,
msg = msg,
data = new PageModel<T>()
{
data = data,
dataCount = dataCount,
pageIndex = page,
pageSize = pageSize,
}
};
}
[NonAction]
protected MessageModel<PageModel<T>> SuccessPage<T>(PageModel<T> pageModel, string msg = "成功")
{
return new MessageModel<PageModel<T>>()
{
code = 200,
success = true,
msg = msg,
data = new PageModel<T>()
{
data = pageModel.data,
dataCount = pageModel.dataCount,
pageIndex = pageModel.pageIndex,
pageSize = pageModel.pageSize,
}
};
}
#endregion
}
}