97 lines
2.6 KiB
C#
97 lines
2.6 KiB
C#
using System.Data;
|
|
using System.Text.Json;
|
|
|
|
namespace YLErp.Web.WebAPI
|
|
{
|
|
[ApiController, ApiAuthorize]
|
|
public abstract class BaseController : ControllerBase
|
|
{
|
|
OptUserInfo _userInfo;
|
|
|
|
protected OptUserInfo CurUser
|
|
{
|
|
get
|
|
{
|
|
if (_userInfo != null)
|
|
{
|
|
return _userInfo;
|
|
}
|
|
|
|
if (User.Identity is OptUserInfo userInfo)
|
|
{
|
|
return _userInfo = userInfo;
|
|
}
|
|
|
|
return OptUserInfo.SystemUser;
|
|
}
|
|
}
|
|
|
|
#region----JSON消息处理----
|
|
|
|
private static JsonResult JsonResult(int code, string msg, object data)
|
|
{
|
|
return new JsonResult(new ApiResult { code = code, msg = msg, data = data }
|
|
, new System.Text.Json.JsonSerializerOptions
|
|
{
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 处理成功后返回的json消息
|
|
/// </summary>
|
|
protected JsonResult JsonSuccessMsg(string msg)
|
|
{
|
|
return JsonResult(code: 0, msg: msg, data: null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 处理成功后返回的json数据
|
|
/// </summary>
|
|
protected JsonResult JsonSuccess(object data, string msg = null)
|
|
{
|
|
return JsonResult(code: 0, msg: msg, data: data);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 处理错误时返回的json消息
|
|
/// </summary>
|
|
protected JsonResult JsonError(string msg)
|
|
{
|
|
return JsonResult(code: 1, msg: msg, data: null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 处理错误时返回的json消息
|
|
/// </summary>
|
|
protected JsonResult JsonError(int code, string msg)
|
|
{
|
|
return JsonResult(code: code, msg: msg, data: null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 处理错误时返回的json消息
|
|
/// </summary>
|
|
protected JsonResult JsonError(Exception ex)
|
|
{
|
|
return JsonResult(code: 1, msg: "发生错误!" + ex.Message, data: null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 返回的分页json数据
|
|
/// </summary>
|
|
protected JsonResult JsonPagedList<T>(IPagedList<T> pagedList)
|
|
{
|
|
return JsonResult(code: 0, msg: null, data: new
|
|
{
|
|
page = pagedList.PageIndex,
|
|
total = pagedList.TotalCount,
|
|
rows = pagedList.Items,
|
|
totalPages = pagedList.TotalPages()
|
|
});
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|