BaseController.cs 3.74 KB
using Hh.Mes.Common;
using Hh.Mes.Common.Json;
using Hh.Mes.Common.log;
using Hh.Mes.POJO.EnumEntitys;
using Hh.Mes.POJO.Response;
using Hh.Mes.Service.SystemAuth;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Linq;
using System.Reflection;

namespace WebMvc
{
    /// <summary>
    /// 基础控制器
    /// <para>用于控制登录用户是否有权限访问指定的Action</para>
    /// </summary>
    public class BaseController : SSOController
    {
        public Response Result = new Response();
        protected string controllerName;   //当前控制器小写名称
        protected string actionName;        //当前Action小写名称

        /// <summary>
        /// 上下文
        /// </summary>
        public HttpContext context;

        public BaseController(IAuth authUtil) : base(authUtil)
        {
        }

        /// <summary>
        /// https://blog.csdn.net/mango_love/article/details/84992020
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            ActionBefore(filterContext);
            actionName = filterContext.RouteData.Values["action"].ToString();

            var function = ((TypeInfo)GetType()).DeclaredMethods.FirstOrDefault(u => u.Name == actionName);
            if (function == null)
            {
                Log4NetHelper.Instance.Info("请检查模块设置URL设置是否正确、请注意大小写! "+ actionName);
                throw new ArgumentNullException($"请检查模块设置URL设置是否正确、请注意大小写!"+ actionName);
            }

            //权限验证标识
            var authorize = function.GetCustomAttribute(typeof(AuthenticateAttribute));
            if (authorize == null) return;

            controllerName = filterContext.RouteData.Values["controller"].ToString();
            var currentModule = authStrategyContext?.Modules.FirstOrDefault(u => u.Url.Contains(controllerName));
            //当前登录用户没有Action记录&&Action有authenticate标识
            if (currentModule == null)
            {
                Log4NetHelper.Instance.Info("登入失效:BaseController【currentModule】为空 ");
                filterContext.Result = new RedirectResult("/Login/Index");
                return;
            }
        }


        public void ResponseEnumJosn()
        {
            ViewBag.EnumYesNoState = typeof(EnumtIsWorkClothes).GetJsonEnum();
            ViewBag.EnumAGVState = typeof(EnumAGVState).GetJsonEnum();
            ViewBag.EnumOrderHeadStatus = typeof(EnumOrderHeadStatus).GetJsonEnum();
            ViewBag.EnumOrderBodyStatus = typeof(EnumOrderBodyStatus).GetJsonEnum();
            ViewBag.EnumWorkReportStatus = typeof(EnumWorkReportStatus).GetJsonEnum();
            ViewBag.EnumAgvTaskType = typeof(EnumAgvTaskType).GetJsonEnum();
            ViewBag.EnumLocationStatus = typeof(EnumLocationStatus).GetJsonEnum();
            ViewBag.EnumLog = typeof(EnumLog).GetJsonEnum();
            ViewBag.EnumLoadData = typeof(EnumLoadData).GetJsonEnum();
            ViewBag.InventoryUseState = typeof(InventoryUseState).GetJsonEnum();
            ViewBag.EnumCutPlanHeadStatus = typeof(EnumCutPlanHeadStatus).GetJsonEnum();
            ViewBag.EnumCutPlanDetailStatus = typeof(EnumCutPlanDetailStatus).GetJsonEnum();

            ViewBag.EnumWeldTechnologyEquipmentStatus = typeof(EnumWeldTechnologyEquipmentStatus).GetJsonEnum();
            ViewBag.EnumPipeMaterial = typeof(EnumPipeMaterial).GetJsonEnum();
            


        }


        public string Serialize(dynamic response)
        {
            return JsonHelper.Instance.Serialize(response);
        }
    }
}