/// <summary>
        /// 根据用户Id和路径查询按钮集合并且返回(使用redis实现)
        /// </summary>
        /// <returns></returns>
        public async Task <IViewComponentResult> InvokeAsync()
        {
            UserSession userSession = JsonNetHelper.DeserializeObject <UserSession>(HttpContext.Session.GetString(KeyUtil.user_info));

            List <MenuActionInfo> result = null;

            if (redisHelp._conn != null)
            {
                string key = string.Format(RedisKeyUtil.login_admin_menu, userSession.user_id);
                if (redisHelp.KeyExists(key))
                {
                    result = JsonNetHelper.DeserializeObject <List <MenuActionInfo> >(await redisHelp.StringGetAsync(key));
                }
                else
                {
                    result = await userService.GetMenuInfo(userSession.user_id);

                    await redisHelp.StringSetAsync(key, JsonNetHelper.SerializeObject(result), new TimeSpan(30, 12, 60));
                }
            }
            else
            {
                result = await userService.GetMenuInfo(userSession.user_id);
            }
            return(View("MenuInfo", result));
        }
示例#2
0
        public async Task <BaseResult <bool> > Login(string login_name_in, string user_pwd_in)
        {
            if (string.IsNullOrEmpty(login_name_in) || string.IsNullOrEmpty(user_pwd_in))
            {
                return(new BaseResult <bool>(808, false));
            }

            //这里可以用邮箱和手机号登陆,需要判断使用什么方式登录,查询用户信息之后验证是否可以访问
            Expression <Func <UserEntity, bool> > where = LinqUtil.True <UserEntity>();
            where = RegexUtil.Email(login_name_in) ? where.AndAlso(c => c.user_email == login_name_in) :
                    where.AndAlso(c => c.user_phone == login_name_in);
            where = where.AndAlso(c => c.user_pwd == CommonUtil.Md5(user_pwd_in));

            UserEntity userEntity = await userRepository.GetAsync(where);

            if (userEntity == null)
            {
                return(new BaseResult <bool>(1000, false));
            }
            if (userEntity.disable == (int)DisableStatus.disable_true)
            {
                return(new BaseResult <bool>(1004, false));
            }
            if (userEntity.user_activation == (int)DisableStatus.disable_true)
            {
                return(new BaseResult <bool>(1005, false));
            }
            if (userEntity.user_visit == (int)DisableStatus.disable_true)
            {
                return(new BaseResult <bool>(1006, false));
            }

            //用户登录正常,修改用户登录时间并且将登录的信息保存到Session中
            await userRepository.UpdateAsync(new UserEntity()
            {
                user_id = userEntity.user_id, last_time = DateTime.Now
            }, true, true, c => c.last_time);

            //处理信息,如果redis连接成功,则直接判断是否存在值,如果存在,则直接使用,否则直接查询并且保存   ,如果连接失败,则直接查询
            List <string> buttionActions = null;

            if (redisHelp._conn != null)
            {
                string key = string.Format(RedisKeyUtil.login_admin, userEntity.user_id);
                if (redisHelp.KeyExists(key))
                {
                    buttionActions = JsonNetHelper.DeserializeObject <List <string> >(await redisHelp.StringGetAsync(key));
                }
                else
                {
                    buttionActions = buttonActionRepository.GetMenuInfo(userEntity.user_id, c => c.action_type != (int)ActionType.front &&
                                                                        c.action_url != null && c.disable == (int)DisableStatus.disable_false).Select(c => c.action_url).ToList();
                    await redisHelp.StringSetAsync(key, JsonNetHelper.SerializeObject(buttionActions), new TimeSpan(30, 12, 60));
                }
            }
            else
            {
                buttionActions = buttonActionRepository.GetMenuInfo(userEntity.user_id,
                                                                    c => c.action_type != (int)ActionType.front && c.action_url != null && c.disable == (int)DisableStatus.disable_false).Select(c => c.action_url).ToList();
            }

            UserSession userSession = new UserSession
            {
                user_id    = userEntity.user_id,
                user_name  = userEntity.user_name + userEntity.user_code,
                user_image = userEntity.user_image,
                full_name  = userEntity.full_name,
                action_url = buttionActions == null ? null : buttionActions
            };

            httpContextUtil.setObjectAsJson(KeyUtil.user_info, userSession);
            return(new BaseResult <bool>(200, true));
        }