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 { /// /// JSON序列化时是否包括NULL值 /// protected virtual bool JsonIncludeNullValue { get; set; } #region ----HTTP响应---- /// /// 返回JSON错误信息 /// protected JsonResult JsonError(string errmsg) { return JsonResp(1, errmsg); } /// /// 返回JSON错误信息 /// protected JsonResult JsonError(string errmsg, bool locale) { if (locale) { return JsonResp(1, errmsg); } return Json(new ApiResponseModel(1, errmsg)); } /// /// 返回JSON错误信息 /// protected JsonResult JsonError(int errcode, string errmsg) { return JsonResp(errcode, errmsg); } /// /// 返回JSON成功信息 /// protected JsonResult JsonSuccess(object resultData, string message = null) { return JsonResp(0, message, resultData); } /// /// 返回JSON格式的成功消息 /// protected JsonResult JsonSuccess() { return Json(new ApiResponseModel(0)); } /// /// 返回JSON格式的成功消息 /// protected JsonResult JsonSuccessMessage(string message) { return JsonResp(0, message); } /// /// 返回JSON格式的模型验证失败 /// 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()); } /// /// 返回JSON格式的消息 /// /// 0-无错误,其他-有错误发生 /// 错误或成功消息 /// 结果数据 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); } } }