예제 #1
0
        /// <summary>
        /// 通过 openId 获取用户信息
        /// </summary>
        /// <param name="openId">唯一键</param>
        /// <returns>用户实体</returns>
        public static BaseUserEntity GetObjectByOpenIdByCache(string openId)
        {
            BaseUserEntity result = null;

            string key    = "OpenId";
            string userId = string.Empty;

            if (!string.IsNullOrWhiteSpace(openId))
            {
                key = key + openId;
# if Redis
                // 2015-12-14 吉日嘎拉 这里可以支持不走缓存的方式
                using (var redisClient = PooledRedisHelper.GetReadOnlyClient())
                {
                    userId = redisClient.Get <string>(key);

                    if (!string.IsNullOrWhiteSpace(userId))
                    {
                        result = GetObjectByCache(redisClient, userId);
                    }

                    if (result == null)
                    {
                        // 若没获取到用户?到数据库里查一次
                        BaseUserLogOnManager userLogOnManager = new BaseUserLogOnManager();
                        userId = userLogOnManager.GetIdByOpenId(openId);
                        if (!string.IsNullOrWhiteSpace(userId))
                        {
                            result = GetObjectByCache(redisClient, userId);
                        }
                    }
                }
#endif
            }
예제 #2
0
        /// <summary>
        /// 某一个网点是否另外一个网点的父级
        /// </summary>
        /// <param name="parentId">父级别主键</param>
        /// <param name="id">主键盘</param>
        /// <returns>是父亲节点</returns>
        public static bool IsParentByCache(string parentId, string id)
        {
            bool result = false;

            // 打开缓存、进行查找
            using (var redisClient = PooledRedisHelper.GetReadOnlyClient())
            {
                // 若已经在缓存里保存了,是上级网点、就不用计算了,提高判断的效率
                string key = "IsParent:" + parentId + ":" + id;
                result = redisClient.Get <bool>(key);
                if (!result)
                {
                    // 从缓存读取数据、提高读取的效率,这里需要防止死循环,有时候会有脏数据
                    BaseOrganizeEntity organizeEntity = BaseOrganizeManager.GetObjectByCache(redisClient, id);
                    while (organizeEntity != null &&
                           !string.IsNullOrEmpty(organizeEntity.ParentId) &&
                           !id.Equals(organizeEntity.ParentId)
                           )
                    {
                        // 若已经找到了,就退出循环,提高效率
                        if (organizeEntity.ParentId.Equals(parentId))
                        {
                            result = true;
                            break;
                        }
                        organizeEntity = BaseOrganizeManager.GetObjectByCache(redisClient, organizeEntity.ParentId);
                    }
                    // 设置一个过期时间,提高效率,10分钟内不需要重复判断,把关系写入缓存数据库里,是否成立都写入缓存服务器里。
                    redisClient.Set <bool>(key, result, DateTime.Now.AddMinutes(10));
                }
            }

            return(result);
        }
예제 #3
0
        public static BaseUserEntity GetObjectByCompanyIdByCodeByCache(string companyId, string userCode)
        {
            BaseUserEntity result = null;

            // 检查参数有效性
            if (string.IsNullOrWhiteSpace(companyId) || string.IsNullOrWhiteSpace(userCode))
            {
                return(result);
            }

            string key = "User:ByCompanyId:ByCode" + companyId + ":" + userCode;
            string id  = string.Empty;

            using (var redisClient = PooledRedisHelper.GetReadOnlyClient())
            {
                id = redisClient.Get <string>(key);

                if (!string.IsNullOrWhiteSpace(id))
                {
                    result = GetObjectByCache(redisClient, id);
                }
            }

            if (result == null)
            {
                BaseUserManager manager = new BaseUserManager();
                result = manager.GetObjectByCompanyIdByCode(companyId, userCode);

                SetCache(result);
            }

            return(result);
        }
예제 #4
0
        /// <summary>
        /// 用户是否在公司里
        /// </summary>
        /// <param name="companyCode">公司编号</param>
        /// <param name="userCode">用户编号</param>
        /// <returns></returns>
        public static bool IsInOrganizeByCode(string companyCode, string userCode)
        {
            // 返回值
            bool result = false;

            // 检查参数有效性
            if (string.IsNullOrWhiteSpace(companyCode) || string.IsNullOrWhiteSpace(userCode))
            {
                return(result);
            }

            // 先判断缓存,减少数据库查询
            string key = "User:ByCompanyCode:ByCode" + companyCode + ":" + userCode;

            using (var redisClient = PooledRedisHelper.GetReadOnlyClient())
            {
                result = redisClient.ContainsKey(key);
                if (result)
                {
                    return(result);
                }
            }

            if (!result)
            {
                BaseUserManager manager = new BaseUserManager();
                BaseUserEntity  entity  = manager.GetObjectByCompanyCodeByCode(companyCode, userCode);
                SetCache(entity);
                result = true;
            }

            return(result);
        }
예제 #5
0
        public static BaseOrganizeEntity GetObjectByNameByCache(string fullName)
        {
            BaseOrganizeEntity result = null;

            // string key = "OrganizeByName:" + fullName;
            string key = "OBN:" + fullName;
            string id  = string.Empty;

            using (var redisClient = PooledRedisHelper.GetReadOnlyClient())
            {
                id = redisClient.Get <string>(key);

                if (!string.IsNullOrWhiteSpace(id))
                {
                    result = GetObjectByCache(redisClient, id);
                }
                else
                {
                    // 从数据库读取数据
                    BaseOrganizeManager organizeManager = new BaseOrganizeManager();
                    result = organizeManager.GetObjectByName(fullName);
                    // 设置缓存
                    if (result != null)
                    {
                        SetCache(result);
                    }
                }
            }

            return(result);
        }
예제 #6
0
        public static BaseDepartmentEntity GetObjectByCodeByCache(string code)
        {
            BaseDepartmentEntity result = null;

            if (string.IsNullOrEmpty(code))
            {
                return(result);
            }

            // string key = "DepartmentByCode:" + code;
            string key = "DBC:" + code;
            string id  = string.Empty;

            using (var redisClient = PooledRedisHelper.GetReadOnlyClient())
            {
                id = redisClient.Get <string>(key);
            }
            if (!string.IsNullOrWhiteSpace(id))
            {
                result = GetObjectByCache(id);
            }
            else
            {
                // 从数据库读取数据
                BaseDepartmentManager departmentManager = new BaseDepartmentManager();
                result = departmentManager.GetObjectByCode(code);
                // 设置缓存,没必要来个空操作
                if (result != null)
                {
                    SetCache(result);
                }
            }

            return(result);
        }
예제 #7
0
        /// <summary>
        /// 从缓存获取获取实体
        /// </summary>
        /// <param name="companyId">公司主键</param>
        /// <param name="fullName">部门名称</param>
        /// <returns>实体</returns>
        public static BaseDepartmentEntity GetObjectByNameByCache(string companyId, string fullName)
        {
            BaseDepartmentEntity result = null;

            if (!string.IsNullOrEmpty(companyId) && !string.IsNullOrEmpty(fullName))
            {
                string id  = string.Empty;
                string key = "DBN:" + companyId + ":" + fullName;
                using (var redisClient = PooledRedisHelper.GetReadOnlyClient())
                {
                    id = redisClient.Get <string>(key);
                    if (!string.IsNullOrWhiteSpace(id))
                    {
                        result = GetObjectByCache(id);
                    }
                    if (result == null)
                    {
                        BaseDepartmentManager departmentManager = new BaseDepartmentManager();
                        result = departmentManager.GetObjectByName(companyId, fullName);
                        // 若是空的不用缓存,继续读取实体
                        if (result != null)
                        {
                            SetCache(result);
                        }
                    }
                }
            }

            return(result);
        }
예제 #8
0
        public static BaseDepartmentEntity GetObjectByNameByCache(BaseUserInfo userInfo, string companyId, string fullName)
        {
            BaseDepartmentEntity result = null;

            string key = "DBN:" + companyId + ":" + fullName;
            string id  = string.Empty;

            using (var redisClient = PooledRedisHelper.GetReadOnlyClient())
            {
                id = redisClient.Get <string>(key);
                if (!string.IsNullOrWhiteSpace(id))
                {
                    key    = "D:" + id;
                    result = redisClient.Get <BaseDepartmentEntity>(key);
                }
            }

            // 远程通过接口获取数据
            if (result == null)
            {
                result = GetObjectByName(userInfo, companyId, fullName);
            }

            return(result);
        }
예제 #9
0
        public static BaseOrganizeEntity GetObjectByNameByCache(BaseUserInfo userInfo, string fullName)
        {
            BaseOrganizeEntity result = null;

            string key = "OBN:" + fullName;
            string id  = string.Empty;

            using (var redisClient = PooledRedisHelper.GetReadOnlyClient())
            {
                id = redisClient.Get <string>(key);
            }
            if (!string.IsNullOrEmpty(id))
            {
                key    = "O:" + id;
                result = BaseOrganizeManager.GetCacheByKey(key);
            }

            // 远程通过接口获取数据
            if (result == null)
            {
                result = GetObjectByName(userInfo, fullName);
            }

            return(result);
        }
예제 #10
0
        public static BaseDepartmentEntity GetCacheByKey(string key)
        {
            BaseDepartmentEntity result = null;

            if (!string.IsNullOrWhiteSpace(key))
            {
                using (var redisClient = PooledRedisHelper.GetReadOnlyClient())
                {
                    result = redisClient.Get <BaseDepartmentEntity>(key);
                }
            }

            return(result);
        }
예제 #11
0
        public static string[] GetUserByOrganizeByCache(string companyId, string departmentId = null)
        {
            string[] result = null;

            if (string.IsNullOrEmpty(companyId))
            {
                return(result);
            }

            string key = string.Empty;

            string users = string.Empty;

            if (string.IsNullOrEmpty(departmentId))
            {
                departmentId = string.Empty;
                key          = "OU:" + companyId;
            }
            else
            {
                key = "OU:" + companyId + ":" + departmentId;
            }

            using (var redisClient = PooledRedisHelper.GetReadOnlyClient())
            {
                users = redisClient.Get <string>(key);
                if (!string.IsNullOrEmpty(users))
                {
                    result = users.Split(',');
                }
            }

            if (result == null)
            {
                BaseMessageManager manager = new BaseMessageManager();
                result = manager.GetUserByOrganize(companyId, departmentId);
                // 若是空的不用缓存,继续读取实体
                if (result != null)
                {
                    users = string.Join(",", result);
                    using (var redisClient = PooledRedisHelper.GetClient())
                    {
                        redisClient.Set <string>(key, users, DateTime.Now.AddDays(1));
                    }
                }
            }

            return(result);
        }
예제 #12
0
        public static BaseOrganizeEntity GetCacheByKey(string key)
        {
            BaseOrganizeEntity result = null;

            if (!string.IsNullOrWhiteSpace(key))
            {
                // 2016-01-08 吉日嘎拉 实现只读连接,读写分离
                using (var redisClient = PooledRedisHelper.GetReadOnlyClient())
                {
                    result = redisClient.Get <BaseOrganizeEntity>(key);
                }
            }

            return(result);
        }
예제 #13
0
        /// <summary>
        /// 获得公司列表
        /// 2015-11-25 吉日嘎拉 进行改进
        /// </summary>
        /// <param name="province">省</param>
        /// <param name="city">城市</param>
        /// <param name="district">县区</param>
        /// <returns>数据表</returns>
        public static string[] GetOrganizeByDistrictByCache(string province, string city, string district)
        {
            string[] result = null;

            string organize = string.Empty;

            if (string.IsNullOrWhiteSpace(province))
            {
                province = string.Empty;
            }
            if (string.IsNullOrWhiteSpace(city))
            {
                city = string.Empty;
            }
            if (string.IsNullOrWhiteSpace(district))
            {
                district = string.Empty;
            }
            // string key = "OrganizeByDistrict:" + province + ":" + city + ":" + district;
            string key = "OBD:" + province + ":" + city + ":" + district;

            using (var redisClient = PooledRedisHelper.GetReadOnlyClient())
            {
                organize = redisClient.Get <string>(key);
            }
            if (!string.IsNullOrWhiteSpace(organize))
            {
                result = organize.Split('.');
            }
            else
            {
                // 从数据库读取数据
                BaseOrganizeManager organizeManager = new BaseOrganizeManager();
                result = organizeManager.GetOrganizeByDistrict(province, city, district);
                // 设置缓存
                if (result != null && result.Length > 0)
                {
                    organize = string.Join(".", result);
                    using (var redisClient = PooledRedisHelper.GetClient())
                    {
                        redisClient.Set <string>(key, organize, DateTime.Now.AddHours(4));
                    }
                }
            }

            return(result);
        }
예제 #14
0
        /// <summary>
        /// 获取城市
        /// 2015-11-25 吉日嘎拉 采用缓存方式,效率应该会更高
        /// </summary>
        /// <param name="province">省份</param>
        /// <returns>城市数组</returns>
        public static string[] GetCityByCache(string province = null)
        {
            string[] result = null;

            string city = string.Empty;

            if (string.IsNullOrWhiteSpace(province))
            {
                province = string.Empty;
            }
            // string key = "OrganizeCity:" + province;
            string key = "OC:" + province;

            using (var redisClient = PooledRedisHelper.GetReadOnlyClient())
            {
                city = redisClient.Get <string>(key);
            }
            if (!string.IsNullOrWhiteSpace(city))
            {
                result = city.Split('.');
            }
            else
            {
                // 从数据库读取数据
                BaseOrganizeManager organizeManager = new BaseOrganizeManager();
                result = organizeManager.GetCity(province);
                // 设置缓存
                if (result != null && result.Length > 0)
                {
                    city = string.Join(".", result);
                    using (var redisClient = PooledRedisHelper.GetClient())
                    {
                        redisClient.Set <string>(key, city, DateTime.Now.AddHours(4));
                    }
                }
            }

            return(result);
        }
예제 #15
0
        /// <summary>
        /// 获取省份
        /// 2015-11-25 吉日嘎拉 采用缓存方式,效率应该会更高
        /// </summary>
        /// <param name="area">区域</param>
        /// <returns>省份数组</returns>
        public static string[] GetProvinceByCache(string area = null)
        {
            string[] result = null;

            if (string.IsNullOrWhiteSpace(area))
            {
                area = string.Empty;
            }
            // string key = "OrganizeProvince:" + area;
            string key      = "OP:" + area;
            string province = string.Empty;

            using (var redisClient = PooledRedisHelper.GetReadOnlyClient())
            {
                province = redisClient.Get <string>(key);
            }
            if (!string.IsNullOrWhiteSpace(province))
            {
                result = province.Split('.');
            }
            else
            {
                // 从数据库读取数据
                BaseOrganizeManager organizeManager = new BaseOrganizeManager();
                result = organizeManager.GetProvince(area);
                // 设置缓存
                if (result != null && result.Length > 0)
                {
                    province = string.Join(".", result);
                    using (var redisClient = PooledRedisHelper.GetClient())
                    {
                        redisClient.Set <string>(key, province, DateTime.Now.AddHours(4));
                    }
                }
            }


            return(result);
        }
예제 #16
0
        /// <summary>
        /// 通过唯一用户名从Reids中获取
        /// </summary>
        /// <param name="nickName"></param>
        /// <returns></returns>
        public static BaseUserEntity GetObjectByNickNameByCache(string nickName)
        {
            // 2016-01-25 黄斌 添加, 从缓存中 通过唯一用户名获取
            BaseUserEntity result = null;

            if (string.IsNullOrEmpty(nickName))
            {
                return(result);
            }

            //存取Redis中的U:BNN:
            string key = "User:ByNickName:" + nickName.ToLower();
            string id  = string.Empty;

            using (var redisClient = PooledRedisHelper.GetReadOnlyClient())
            {
                id = redisClient.Get <string>(key);
                if (!string.IsNullOrWhiteSpace(id))
                {
                    result = GetObjectByCache(redisClient, id);
                }
            }

            if (result == null)
            {
                BaseUserManager manager = new BaseUserManager();
                result = manager.GetObjectByNickName(nickName);
                if (result != null)
                {
                    result.NickName = result.NickName.ToLower();
                    SetCache(result);
                }
            }

            return(result);
        }
예제 #17
0
        /// <summary>
        /// 验证用户的令牌openId
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <param name="openId">用户的令牌</param>
        /// <param name="useCaching">采用缓存</param>
        /// <param name="useDataBase">采用数据库</param>
        /// <param name="useUserCenterHost">采用用户中心接口</param>
        /// <returns>验证通过</returns>
        public static bool ValidateOpenId(string userId, string openId, string cachingSystemCode = null, bool useCaching = true, bool useDataBase = false, bool useUserCenterHost = false)
        {
            bool result = false;

            if (string.IsNullOrEmpty(cachingSystemCode))
            {
                cachingSystemCode = string.Empty;
            }

            // 2016-03-14 吉日嘎拉、PDA系统的单独处理、其他的都认为是一样的。
            if (!cachingSystemCode.Equals("PDA"))
            {
                cachingSystemCode = string.Empty;
            }

            if (!string.IsNullOrWhiteSpace(openId))
            {
                // 使用缓存进行验证、效率高,减少数据库的I/O压力。
                if (useCaching)
                {
                    string key = string.Empty;
                    // 2015-11-20 吉日嘎拉 为了编译通过进行改进
                    using (var redisClient = PooledRedisHelper.GetReadOnlyClient())
                    {
                        if (string.IsNullOrEmpty(cachingSystemCode))
                        {
                            key = "openId:" + openId;
                        }
                        else
                        {
                            key = "openId:" + cachingSystemCode + ":" + openId;
                        }
                        result = redisClient.ContainsKey(key);
                    }
                }

                // 用数据库的方式进行验证
                if (!result && useDataBase)
                {
                    BaseUserLogOnManager userLogOnManager = new BaseUserLogOnManager();
                    result = userLogOnManager.ValidateOpenId(userId, openId, cachingSystemCode);
                    if (result)
                    {
                        // 提高缓存效率、若读取到了,写入到缓存里去
                        if (!string.IsNullOrWhiteSpace(userId) && useCaching)
                        {
                            SetUserOpenId(userId, openId, cachingSystemCode);
                        }
                        result = true;
                    }
                }

                // 不能访问数据库时、通过远程用户中心服务进行验证OpenId、通过服务方式进行验证
                if (!result && useUserCenterHost)
                {
                    string              url        = BaseSystemInfo.UserCenterHost + "/UserCenterV42/LogOnService.ashx";
                    WebClient           webClient  = new WebClient();
                    NameValueCollection postValues = new NameValueCollection();
                    if (!string.IsNullOrEmpty(cachingSystemCode))
                    {
                        postValues.Add("systemCode", cachingSystemCode);
                    }
                    postValues.Add("ipAddress", Utilities.GetIPAddress());
                    postValues.Add("securityKey", BaseSystemInfo.SecurityKey);
                    postValues.Add("function", "ValidateOpenId");
                    postValues.Add("userId", userId);
                    postValues.Add("openId", openId);
                    // 向服务器发送POST数据
                    byte[] responseArray = webClient.UploadValues(url, postValues);
                    string response      = Encoding.UTF8.GetString(responseArray);
                    if (!string.IsNullOrEmpty(response))
                    {
                        result = response.Equals(true.ToString(), StringComparison.InvariantCultureIgnoreCase);
                    }
                }
            }

            return(result);
        }