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.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace EntrustSettle.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize]
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 = 2000,
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 = 2000,
success = true,
msg = msg,
data = data,
};
}
/// <summary>
/// 返回一个错误提示,但不包含数据
/// </summary>
[NonAction]
protected MessageModel FailedMsg(int code, string msg = "失败")
{
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为5000data为T的默认值
/// </summary>
[NonAction]
protected MessageModel<T> Failed<T>(string msg = "失败")
{
return new MessageModel<T>()
{
code = 5000,
success = false,
msg = msg,
data = default,
};
}
[NonAction]
protected MessageModel<PageModel<T>> SuccessPage<T>(List<T> data, int dataCount, int page, int pageSize, int pageCount, string msg = "成功")
{
return new MessageModel<PageModel<T>>()
{
code = 2000,
success = true,
msg = msg,
data = new PageModel<T>()
{
data = data,
dataCount = dataCount,
page = page,
pageSize = pageSize,
pageCount = pageCount
}
};
}
[NonAction]
protected MessageModel<PageModel<T>> SuccessPage<T>(PageModel<T> pageModel, string msg = "成功")
{
return new MessageModel<PageModel<T>>()
{
code = 2000,
success = true,
msg = msg,
data = new PageModel<T>()
{
data = pageModel.data,
dataCount = pageModel.dataCount,
page = pageModel.page,
pageSize = pageModel.pageSize,
pageCount = pageModel.pageCount
}
};
}
#endregion
}
}