예제 #1
0
        public bool 检查用户登陆是否合法(int userId, DeviceType deviceType, string userToken, bool isAdmin, bool isSingleLogin, out string errorMsg)
        {
            errorMsg = string.Empty;

            //只能一个用户登录
            if (isAdmin || isSingleLogin)
            {
                string key = CommonCacheKeys.Build_UserToken_Key(userId, deviceType, isAdmin);

                string tokenInCache = this._easyCache.Get <string>(key);

                //只有token相同才是合法的
                if (!string.IsNullOrWhiteSpace(tokenInCache) && tokenInCache == userToken)
                {
                    return(true);
                }
                else
                {
                    errorMsg = "您的帐号已经在其他地方登录,如不是您本人的操作,请及时修改账户密码。";

                    return(false);
                }
            }

            return(true);
        }
예제 #2
0
        /// <summary>
        /// 添加【用户树节点】缓存
        /// </summary>
        public bool SetUserTreeNode(UserTreeNodeCache cacheModel)
        {
            if (GetUserFunc == null)
            {
                throw new FException("您未指定查询用户信息的方法");
            }
            if (cacheModel.UserId <= 0)
            {
                throw new FException("您必须要传入【用户Id】");
            }
            if (cacheModel.ParentId < 0)
            {
                throw new FException("您的推荐人【用户Id】不能为空");
            }
            if (string.IsNullOrWhiteSpace(cacheModel.UserName))
            {
                throw new FException("您必须要传入【用户名】");
            }
            if (cacheModel.ParentId > 0 && string.IsNullOrWhiteSpace(cacheModel.ParentName))
            {
                throw new FException("您必须要传入推荐人【用户名】");
            }

            //把该用户加到他父亲下面的孩子集合
            this.AddToPartOfParentCache(cacheModel.ParentId, cacheModel.UserId);

            //把自己的信息添加到【用户树节点缓存】(如果修改了用户信息,那么需要重置该缓存,以保证数据正确)
            string selfKey = CommonCacheKeys.Build_UserTree_Key(cacheModel.UserId);

            return(this._easyCache.Set(selfKey, cacheModel, _expiresTimeSpan, db: redisdbIndex));
        }
예제 #3
0
        /// <summary>
        /// 清除指定用户所有token
        /// </summary>
        public void ClearUserAllToken(int userId)
        {
            foreach (DeviceType deviceType in Enum.GetValues(typeof(DeviceType)))
            {
                string key = CommonCacheKeys.Build_UserToken_Key(userId, deviceType, false);

                this._easyCache.Remove(key);
            }
        }
예제 #4
0
        /// <summary>
        /// 检查指定孩子用户Id是否存在于他的父节点缓存集合中
        /// </summary>
        /// <param name="parentId">父亲用户Id</param>
        /// <param name="childUserId">孩子用户Id</param>
        public bool IsInPartOfParentCache(int parentId, int childUserId)
        {
            if (GetUserFunc == null)
            {
                throw new FException("您未指定查询用户信息的方法");
            }

            string partOfKey = CommonCacheKeys.Build_UserTree_PartOf_Puid_Key(parentId);

            return(this._easyCache.IsInSet(partOfKey, childUserId.ToString(), db: redisdbIndex));
        }
예제 #5
0
        /// <summary>
        /// 移除指定UserId的孩子集合缓存
        /// </summary>
        public void RemovePartOfParentCache(int userId)
        {
            if (GetUserFunc == null)
            {
                throw new FException("您未指定查询用户信息的方法");
            }

            string partOfKey = CommonCacheKeys.Build_UserTree_PartOf_Puid_Key(userId);

            this._easyCache.Remove(partOfKey, db: redisdbIndex);
        }
예제 #6
0
        /// <summary>
        /// 把指定的孩子UserId从指定父亲的孩子集合缓存里移除
        /// </summary>
        /// <param name="parentId">父亲用户Id</param>
        /// <param name="childUserId">孩子用户Id</param>
        public void RemoveMemberFromPartOfParentCache(int parentId, int childUserId)
        {
            if (GetUserFunc == null)
            {
                throw new FException("您未指定查询用户信息的方法");
            }

            string partOfKey = CommonCacheKeys.Build_UserTree_PartOf_Puid_Key(parentId);

            this._easyCache.SetRemove(partOfKey, childUserId.ToString(), db: redisdbIndex);
        }
예제 #7
0
        /// <summary>
        /// 移除用户树节点缓存
        /// </summary>
        public void RemoveUserTreeNode(int userId)
        {
            if (GetUserFunc == null)
            {
                throw new FException("您未指定查询用户信息的方法");
            }
            if (userId <= 0)
            {
                return;
            }

            string selfKey = CommonCacheKeys.Build_UserTree_Key(userId);

            this._easyCache.Remove(selfKey, db: redisdbIndex);
        }
예제 #8
0
        /// <summary>
        /// 查找指定用户的【直接】孩子个数(不包括自己)
        /// </summary>
        public long FindDirectChildCount(int parentId)
        {
            if (GetUserFunc == null)
            {
                throw new FException("您未指定查询用户信息的方法");
            }

            //再找出直接孩子的树节点
            string partOfParentKey = CommonCacheKeys.Build_UserTree_PartOf_Puid_Key(parentId);

            //【模式匹配】找出直接孩子的树节点Key
            long count = this._easyCache.GetSetLength(partOfParentKey, db: redisdbIndex);

            return(count);
        }
예제 #9
0
        /// <summary>
        /// 查找指定用户的【直接】孩子UserId集合(不包括自己)
        /// </summary>
        public List <int> FindDirectChildUserId(int parentId)
        {
            if (GetUserFunc == null)
            {
                throw new FException("您未指定查询用户信息的方法");
            }

            //再找出直接孩子的树节点
            string partOfParentKey = CommonCacheKeys.Build_UserTree_PartOf_Puid_Key(parentId);

            //【模式匹配】找出直接孩子的树节点Key
            var childUserIdList = this._easyCache.GetSetList(partOfParentKey, db: redisdbIndex) ?? new List <string>();

            return(childUserIdList.Select(x => int.Parse(x)).ToList());
        }
예제 #10
0
        /// <summary>
        /// 更新用户Token
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="userName">用户名</param>
        /// <param name="deviceType">平台</param>
        /// <param name="isAdmin">是否是管理员</param>
        /// <param name="tokenExpireTime">token过期时间</param>
        /// <returns>新的用户token</returns>
        public string 更新用户Token(int userId, string userName, DeviceType deviceType, bool isAdmin, TimeSpan?tokenExpireTime = null)
        {
            var expire = tokenExpireTime ?? this._tokenExpiresTimeSpan;

            string newToken = CreateNewToken(new TokenModel
            {
                UserId          = userId,
                UserName        = userName,
                TokenExpireTime = DateTime.Now.Add(expire),
            });

            string tokenCacheKey = CommonCacheKeys.Build_UserToken_Key(userId, deviceType, isAdmin);

            this._easyCache.Set(tokenCacheKey, newToken, expire);

            return(newToken);
        }
예제 #11
0
        /// <summary>
        /// 把指定的孩子UserId添加到指定父亲的孩子集合缓存里
        /// </summary>
        /// <param name="parentId">父亲用户Id</param>
        /// <param name="childUserId">孩子用户Id</param>
        public void AddToPartOfParentCache(int parentId, int childUserId)
        {
            if (GetUserFunc == null)
            {
                throw new FException("您未指定查询用户信息的方法");
            }

            string partOfKey = CommonCacheKeys.Build_UserTree_PartOf_Puid_Key(parentId);

            this._easyCache.SetAdd(partOfKey, childUserId.ToString(), db: redisdbIndex);

            //为了防止数据没有添加到redis,这里做一次检查,并且再补偿一次,记下记录以便跟踪。
            if (!IsInPartOfParentCache(parentId, childUserId))
            {
                logger.Fatal($"PartOfParentCache:出现添加异常。parentId={parentId},childUserId={childUserId}");

                this._easyCache.SetAdd(partOfKey, childUserId.ToString(), db: redisdbIndex);
            }
        }
예제 #12
0
        /// <summary>
        /// 获取【用户树节点】缓存
        /// </summary>
        public UserTreeNodeCache GetUserTreeNode(int userId)
        {
            if (GetUserFunc == null)
            {
                throw new FException("您未指定查询用户信息的方法");
            }
            if (userId <= 0)
            {
                return(null);
            }

            string key = CommonCacheKeys.Build_UserTree_Key(userId);

            return(this._easyCache.Get(key, () =>
            {
                //读不到缓存从数据库读
                UserTreeNodeCache cacheModel = this.GetUserFormDB(userId);
                return cacheModel;
            }, _expiresTimeSpan, db: redisdbIndex));
        }
예제 #13
0
        /// <summary>
        /// 清除指定用户token
        /// </summary>
        public void ClearUserToken(int userId, DeviceType deviceType, bool isAdmin)
        {
            string userTokenKey = CommonCacheKeys.Build_UserToken_Key(userId, deviceType, isAdmin);

            this._easyCache.Remove(userTokenKey);
        }