Exemplo n.º 1
0
        /// <summary>
        /// 当出现多人请求字典数据时,此方法可能会出现延迟现象(自行根据实际处理)
        /// </summary>
        /// <returns></returns>
        private static List <Sys_Dictionary> GetAllDictionary()
        {
            ICacheService cacheService = AutofacContainerModule.GetService <ICacheService>();

            //每次比较缓存是否更新过,如果更新则重新获取数据
            if (_dictionaries != null && _dicVersionn == cacheService.Get(Key))
            {
                return(_dictionaries);
            }

            lock (_dicObj)
            {
                if (_dicVersionn != "" && _dictionaries != null && _dicVersionn == cacheService.Get(Key))
                {
                    return(_dictionaries);
                }
                _dictionaries = DBServerProvider.DbContext
                                .Set <Sys_Dictionary>()
                                .Where(x => x.Enable == 1)
                                .Include(c => c.Sys_DictionaryList).ToList();

                string cacheVersion = cacheService.Get(Key);
                if (string.IsNullOrEmpty(cacheVersion))
                {
                    cacheVersion = DateTime.Now.ToString("yyyyMMddHHMMssfff");
                    cacheService.Add(Key, cacheVersion);
                }
                else
                {
                    _dicVersionn = cacheVersion;
                }
            }
            return(_dictionaries);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 拦截控制器方法
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var isDefined = false;
            // 获取请求进来的控制器与方法
            var controllerActionDescriptor = filterContext.ActionDescriptor as ControllerActionDescriptor;

            if (controllerActionDescriptor != null)
            {
                isDefined = controllerActionDescriptor.MethodInfo.GetCustomAttributes(inherit: true)
                            .Any(a => a.GetType().Equals(typeof(AllowAnonymousAttribute)));
            }

            if (isDefined || UserContext.Current.IsSuperAdmin)
            {
                return;
            }

            // 获取设置的操作码(如果没设置操作码,默认不验证权限)
            var actionCode = (PermissionAttribute)controllerActionDescriptor.MethodInfo
                             .GetCustomAttributes(inherit: true)
                             .FirstOrDefault(t => t.GetType().Equals(typeof(PermissionAttribute)));

            if (actionCode != null)
            {
                //tableNmae
                if (string.IsNullOrEmpty(actionCode.TableName))
                {
                    actionCode.TableName = controllerActionDescriptor.ControllerName;
                }

                var ResponseConten = new WebResponseContent();
                // 验证是否通过
                var permissions = AutofacContainerModule.GetService <Sys_RoleService>().GetUserPermissions();
                if (permissions == null || permissions.Count() == 0)
                {
                    filterContext.Result = new OkObjectResult(ResponseConten.Error(ResponseType.NoPermissions));
                    return;
                }
                var actionAuth = permissions.Where(x => x.TableName == actionCode.TableName.ToLower()).FirstOrDefault()?.UserAuthArr;
                if (actionAuth == null ||
                    actionAuth.Count() == 0 ||
                    !actionAuth.Contains(actionCode.Code.SafeString()))
                {
                    filterContext.Result = new OkObjectResult(ResponseConten.Error(ResponseType.NoPermissions));
                }
            }

            base.OnActionExecuting(filterContext);
        }
Exemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public WebResponseContent Login(string userName, string password)
        {
            WebResponseContent responseContent = new WebResponseContent();

            if (string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(password))
            {
                return(responseContent.Error("登录错误"));
            }
            // var encryptPwd = AESEncrypt.Encrypt(password, AESEncrypt.pwdKey);

            var user = repository.Sys_User.Where(q => q.Enable == (byte)DataStatus.Enable)
                       .Where(q => q.UserName == userName && q.UserPwd == password).First();

            if (user == null)
            {
                return(responseContent.Error("账户或密码错误"));
            }
            var adminToken = new AdminUser
            {
                User_Id  = user.UID,
                Email    = user.Email,
                Role_Id  = user.Role_Id,
                UserName = user.UserName,
            };
            //获取token配置
            var tokenManagement = AutofacContainerModule.GetService <IOptions <TokenManagement> >().Value;

            var token = TokenHelper.CreateAdminToken(tokenManagement, adminToken);

            //HttpContext.Current.Response.Headers.Add("Authorization", new StringValues(token));

            return(responseContent.OK("登录成功", new M_AdminUserRP
            {
                id = user.UID,
                UserName = user.UserName,
                RoleId = user.Role_Id,
                HeadImageUrl = user.HeadImageUrl,
                Moblie = user.Mobile,
                Email = user.Email,
                Token = token,
            }));
        }
Exemplo n.º 4
0
 /// <summary>
 /// 获取 <paramref name="name"/> 对应的本地化字符串。
 /// </summary>
 /// <param name="name">本地化资源的名称。</param>
 /// <returns>返回本地化字符串。</returns>
 public static string L(string name)
 {
     if (string.IsNullOrWhiteSpace(name))
     {
         return(name);
     }
     try
     {
         var Language = WM.Infrastructure.Utilities.HttpContext.Current.Request.Headers["Language"].ToString();
         LocalizationHelper.InitializeLanguage(Language);
         var value = LocalizationHelper.GetString(name);
         return(value);
     }
     catch (Exception ex)
     {
         var logger = AutofacContainerModule.GetService <ILogger <BaseSerivce <TRepository> > >();
         logger?.LogError(ex, $"多语言名称[{name}]未找到!");
         //写日志
     }
     return(name);
 }
Exemplo n.º 5
0
        public static List <RoleNodes> GetAllRoleId()
        {
            ICacheService cacheService = AutofacContainerModule.GetService <ICacheService>();

            //每次比较缓存是否更新过,如果更新则重新获取数据
            if (_roles != null && _RoleVersionn == cacheService.Get(Key))
            {
                return(_roles);
            }
            lock (_RoleObj)
            {
                if (_RoleVersionn != "" && _roles != null && _RoleVersionn == cacheService.Get(Key))
                {
                    return(_roles);
                }
                _roles = DBServerProvider.DbContext
                         .Set <Sys_Role>()
                         .Where(x => x.Enable == 1)
                         .Select(s => new RoleNodes()
                {
                    Id = s.Role_Id, ParentId = s.ParentId, RoleName = s.RoleName
                })
                         .ToList();

                string cacheVersion = cacheService.Get(Key);
                if (string.IsNullOrEmpty(cacheVersion))
                {
                    cacheVersion = DateTime.Now.ToString("yyyyMMddHHMMssfff");
                    cacheService.Add(Key, cacheVersion);
                }
                else
                {
                    _RoleVersionn = cacheVersion;
                }
            }
            return(_roles);
        }
Exemplo n.º 6
0
 private static T GetService <T>() where T : class
 {
     return(AutofacContainerModule.GetService <T>());
 }
Exemplo n.º 7
0
 public static void Refresh()
 {
     AutofacContainerModule.GetService <ICacheService>().Remove(Key);
 }
Exemplo n.º 8
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="path"></param>
 /// <param name="rootPath">获取wwwroot路径</param>
 /// <returns></returns>
 public static string MapPath(this string path, bool rootPath)
 {
     return(AutofacContainerModule.GetService <IPathProvider>().MapPath(path, rootPath));
 }