152 lines
4.4 KiB
C#
152 lines
4.4 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc.Filters;
|
|
using System.Text;
|
|
using YieldChain.Models;
|
|
|
|
namespace YLErp.Web.Areas.Admin.Controllers
|
|
{
|
|
[Authorize(policy: "yc_identity"), Area("Admin")]
|
|
public abstract class AdminBaseController : Controller
|
|
{
|
|
/// <summary>
|
|
/// JSON序列化时是否包括NULL值
|
|
/// </summary>
|
|
protected virtual bool JsonIncludeNullValue { get; set; }
|
|
|
|
#region ----HTTP响应----
|
|
|
|
/// <summary>
|
|
/// 返回JSON错误信息
|
|
/// </summary>
|
|
protected JsonResult JsonError(string errmsg)
|
|
{
|
|
return JsonResp(1, errmsg);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 返回JSON错误信息
|
|
/// </summary>
|
|
protected JsonResult JsonError(string errmsg, bool locale)
|
|
{
|
|
if (locale)
|
|
{
|
|
return JsonResp(1, errmsg);
|
|
}
|
|
return Json(new ApiResponseModel(1, errmsg));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 返回JSON错误信息
|
|
/// </summary>
|
|
protected JsonResult JsonError(int errcode, string errmsg)
|
|
{
|
|
return JsonResp(errcode, errmsg);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 返回JSON成功信息
|
|
/// </summary>
|
|
protected JsonResult JsonSuccess(object resultData, string message = null)
|
|
{
|
|
return JsonResp(0, message, resultData);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 返回JSON格式的成功消息
|
|
/// </summary>
|
|
protected JsonResult JsonSuccess()
|
|
{
|
|
return Json(new ApiResponseModel(0));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 返回JSON格式的成功消息
|
|
/// </summary>
|
|
protected JsonResult JsonSuccessMessage(string message)
|
|
{
|
|
return JsonResp(0, message);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 返回JSON格式的模型验证失败
|
|
/// </summary>
|
|
protected JsonResult JsonModelError(string separator = "\\n\\n")
|
|
{
|
|
var sb = new StringBuilder();
|
|
|
|
foreach (var item in ModelState.Values)
|
|
{
|
|
if (item.Errors.Count > 0)
|
|
{
|
|
for (var i = item.Errors.Count - 1; i >= 0; i--)
|
|
{
|
|
if (sb.Length > 0)
|
|
{
|
|
sb.Append(separator);
|
|
}
|
|
|
|
sb.Append(item.Errors[i].ErrorMessage);
|
|
}
|
|
}
|
|
}
|
|
|
|
return JsonResp(2, sb.ToString());
|
|
}
|
|
|
|
/// <summary>
|
|
/// 返回JSON格式的消息
|
|
/// </summary>
|
|
/// <param name="errcode">0-无错误,其他-有错误发生</param>
|
|
/// <param name="message">错误或成功消息</param>
|
|
/// <param name="resultData">结果数据</param>
|
|
protected JsonResult JsonResp(int errcode, string message, object resultData = null)
|
|
{
|
|
return Json(new ApiResponseModel(errcode, message, resultData));
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
UserInfo _curUser;
|
|
|
|
protected UserInfo CurUser
|
|
{
|
|
get
|
|
{
|
|
if (User.Identity.IsAuthenticated)
|
|
{
|
|
if (_curUser != null)
|
|
{
|
|
return _curUser;
|
|
}
|
|
|
|
_curUser = Server.CacheProvider.Get("loginUser^" + HttpContext.User.GetUserId()) as UserInfo;
|
|
|
|
if (_curUser != null)
|
|
{
|
|
_curUser.UserFrom = OptUserFrom.WebUI;
|
|
return _curUser;
|
|
}
|
|
}
|
|
return new UserInfo { UserName = "系统", UserFrom = OptUserFrom.WebUI };
|
|
}
|
|
}
|
|
|
|
public override void OnActionExecuting(ActionExecutingContext context)
|
|
{
|
|
if (!CurUser.HasRight("系统管理-系统配置管理"))
|
|
{
|
|
if (Request.Headers["X-Requested-With"] == "XMLHttpRequest")
|
|
{
|
|
context.Result = JsonError("没有权限");
|
|
}
|
|
else
|
|
{
|
|
context.Result = new ContentResult { Content = "您无权访问此页面。", ContentType = "text/plain; charset=utf-8" };
|
|
}
|
|
}
|
|
|
|
base.OnActionExecuting(context);
|
|
}
|
|
}
|
|
} |