Пример #1
0
        /// <summary>
        /// 获取准备推送数据的在线用户列表
        /// </summary>
        /// <param name="jobName"></param>
        /// <returns></returns>
        private List <string> GetUserConnList(string jobName)
        {
            List <string> res = new List <string>();
            Dictionary <string, string> dict = _cache.GetCache <Dictionary <string, string> >("__ConnectionUserCacheKey");

            if (dict.Count > 0)
            {
                List <string> userIdsList = new List <string>();
                foreach (KeyValuePair <string, string> pair in dict)
                {
                    if (!pair.Key.Contains("S_"))
                    {
                        userIdsList.Add(pair.Key);
                    }
                }

                List <string> temp = new List <string>();
                foreach (string userId in userIdsList)
                {
                    List <string> usedJobList = _cache.GetCache <List <string> >(userId + "_UsedJobListCacheKey");
                    if (usedJobList != null && usedJobList.Contains(jobName))
                    {
                        temp.Add(userId);
                    }
                }

                foreach (string s in temp)
                {
                    if (dict.ContainsKey(s))
                    {
                        res.Add(dict[s]);
                    }
                }
            }
            return(res);
        }
Пример #2
0
        /// <summary>
        /// 校验验证码
        /// </summary>
        /// <param name="mobile">手机号</param>
        /// <param name="vercode">验证码</param>
        /// <returns>返回 0.成功 1.过期 2.验证码错误</returns>
        public static int MobileMesCheck(string mobile, string vercode)
        {
            string SendKey = "mob_" + mobile;
            //string objMes = MemCached.GetCache<string>(SendKey);
            object objMes = WebCache.GetCache(SendKey);

            if (objMes == null)
            {
                return(1);//过期
            }
            if (objMes.ToString() != vercode)
            {
                return(2); //验证码错误
            }
            return(0);
        }
Пример #3
0
        /// <summary>
        /// 获取用户具有的权限
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public DataTable GetPermissionsNameByUseridModouleid(string userid, string moduleid)
        {
            string cacheStrphold = string.Format("CommonSearch_GetPermissionsNameByUseridModouleid_{0}_{1}", userid, moduleid);

            if ((WebCache.GetCache(cacheStrphold) == null))
            {
                lock (lockHelp)
                {
                    if ((WebCache.GetCache(cacheStrphold) == null))
                    {
                        DataTable dt = commonSearch.GetPermissionsNameByUseridModouleid(userid, moduleid);

                        WebCache.SetCache(cacheStrphold, dt, 60);
                    }
                }
            }
            return((DataTable)WebCache.GetCache(cacheStrphold));
        }
Пример #4
0
        /// <summary>
        /// 获取用户所具有的平台列表
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public DataTable GetPlatformTableByUserid(string userid)
        {
            string cacheStrphold = string.Format("CommonSearch_GetPlatformTableByUserid_{0}", userid);

            if ((WebCache.GetCache(cacheStrphold) == null))
            {
                lock (lockHelp)
                {
                    if ((WebCache.GetCache(cacheStrphold) == null))
                    {
                        DataTable dt = commonSearch.GetPlatformTableByUserid(userid);

                        WebCache.SetCache(cacheStrphold, dt, 60);
                    }
                }
            }
            return((DataTable)WebCache.GetCache(cacheStrphold));
        }
Пример #5
0
        /// <summary>
        /// 获取用户具有的权限
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public DataTable GetGameListByRoleId(string roleid, int channelid)
        {
            string cacheStrphold = string.Format("CommonSearch_GetGameListByRoleId_{0}_{1}", roleid, channelid);

            if ((WebCache.GetCache(cacheStrphold) == null))
            {
                lock (lockHelp)
                {
                    if ((WebCache.GetCache(cacheStrphold) == null))
                    {
                        DataTable dt = commonSearch.GetGameListByRoleId(roleid);

                        WebCache.SetCache(cacheStrphold, dt, 60);
                    }
                }
            }
            return((DataTable)WebCache.GetCache(cacheStrphold));
        }
Пример #6
0
        /// <summary>
        /// 获得天气的信息(json格式)
        /// </summary>
        /// <returns></returns>
        private string GetResponseStr()
        {
            string cacheTodayDay = string.Format("TodayDay_{0}", DateTime.Now.ToString("yyyy-MM-dd hh:00:00"));

            if ((WebCache.GetCache(cacheTodayDay) == null))
            {
                lock (lockHelp)
                {
                    if ((WebCache.GetCache(cacheTodayDay) == null))
                    {
                        string          str      = "http://m.weather.com.cn/data/101010100.html";
                        HttpWebRequest  request  = (HttpWebRequest)WebRequest.Create(str);
                        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                        Stream          stream   = response.GetResponseStream();//获得回应的数据流
                        //将数据流转成 String
                        string result = new StreamReader(stream, System.Text.Encoding.UTF8).ReadToEnd();

                        WebCache.SetCache(cacheTodayDay, result, 3600);
                    }
                }
            }
            return((string)WebCache.GetCache(cacheTodayDay));
        }
        ///// <summary>
        ///// 开启一个或多个任务
        ///// </summary>
        ///// <param name="userId">用户ID</param>
        ///// <param name="jobCode">任务编码</param>
        //public void OpenJob(string userId, string[] jobCode)
        //{
        //    lock (_jobStateLock)
        //    {
        //        Dictionary<string, Dictionary<string, JobState>> jobStateDict =
        //            _cache.GetCache<Dictionary<string, Dictionary<string, JobState>>>("");

        //        //已有任务
        //        Dictionary<string, JobState> historyJobStatedict = new Dictionary<string, JobState>();
        //        //新加任务
        //        Dictionary<string, JobState> newJobStatedict = new Dictionary<string, JobState>();

        //        _jobStateDict.TryGetValue(userId, out historyJobStatedict);
        //        if (historyJobStatedict == null || historyJobStatedict.Count == 0)
        //        {
        //            historyJobStatedict = new Dictionary<string, JobState>();
        //            //添加任务状态到字典
        //            foreach (string code in jobCode)
        //            {
        //                historyJobStatedict.Add(code, JobState.Open);
        //            }

        //            newJobStatedict = historyJobStatedict;

        //            _jobStateDict.TryAdd(userId, historyJobStatedict);
        //        }
        //        else
        //        {
        //            //添加未标识的任务
        //            foreach (string code in jobCode)
        //            {
        //                //如果不包含该任务,则直接添加
        //                if (!historyJobStatedict.ContainsKey(code))
        //                {
        //                    historyJobStatedict.Add(code, JobState.Open);
        //                    newJobStatedict.Add(code, JobState.Open);
        //                }
        //                else
        //                {
        //                    //如果任务是关闭状态,则开启它
        //                    if (historyJobStatedict[code] == JobState.Closed)
        //                    {
        //                        historyJobStatedict[code] = JobState.Open;
        //                        newJobStatedict.Add(code, JobState.Open);
        //                    }
        //                }
        //            }

        //            _jobStateDict[userId] = historyJobStatedict;
        //        }

        //        //TODO 调用开启、关闭任务方法
        //        this.OpenOrCloseJob(userId, newJobStatedict);
        //    }
        //}

        ///// <summary>
        ///// 关闭一个或多个任务
        ///// </summary>
        ///// <param name="userId">用户ID</param>
        ///// <param name="jobCode">任务编码</param>
        //public void CloseJob(string userId, string[] jobCode)
        //{
        //    lock (_jobStateLock)
        //    {
        //        //已有任务
        //        Dictionary<string, JobState> historyJobStatedict = new Dictionary<string, JobState>();

        //        if (_jobStateDict.Count > 0 && _jobStateDict != null)
        //        {
        //            _jobStateDict.TryGetValue(userId, out historyJobStatedict);
        //            if (historyJobStatedict != null && historyJobStatedict.Count > 0)
        //            {
        //                foreach (string code in jobCode)
        //                {
        //                    if (historyJobStatedict.ContainsKey(code))
        //                    {
        //                        if (historyJobStatedict[code] == JobState.Open)
        //                        {
        //                            historyJobStatedict[code] = JobState.Closed;
        //                        }
        //                    }
        //                    else
        //                    {
        //                        historyJobStatedict.Add(code, JobState.Closed);
        //                    }
        //                }
        //            }
        //        }
        //        _jobStateDict[userId] = historyJobStatedict;

        //        this.OpenOrCloseJob(userId, historyJobStatedict);
        //    }
        //}

        ///// <summary>
        ///// 重置一个或多个任务
        ///// </summary>
        ///// <param name="userId">用户ID</param>
        ///// <param name="jobCode">任务编码</param>
        //public void ResetJob(string userId, string[] jobCode)
        //{
        //    throw new NotImplementedException();
        //}

        ///// <summary>
        ///// 开启或者关闭任务
        ///// </summary>
        ///// <param name="userId"></param>
        ///// <param name="newJobStatedict"></param>
        //private void OpenOrCloseJob(string userId, Dictionary<string, JobState> newJobStatedict)
        //{
        //    foreach (KeyValuePair<string, JobState> pair in newJobStatedict)
        //    {
        //        string jobClde = pair.Key;
        //        switch (pair.Value)
        //        {
        //            case JobState.Open:
        //                //TODO 开启任务

        //                //向客户端广播消息
        //                Clients.Client(userId).BroadcastJobOpened(jobClde);
        //                break;
        //            case JobState.Closed:
        //                //TODO 关闭任务

        //                //向客户端广播消息
        //                Clients.Client(userId).BroadcastJobClosed(jobClde);
        //                break;
        //        }
        //    }
        //}

        #endregion

        #region 任务控制

        /// <summary>
        /// 开启一个或多个任务
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="connId">用户ID-连接ID</param>
        /// <param name="jobCode">任务编码</param>
        public void OpenJob(string userId, Dictionary <string, string> connId, string[] jobCode)
        {
            lock (_jobStateLock)
            {
                #region 1-获取用户列表(放在客户端连接时操作)
                ////1-获取用户列表
                List <string> jobUserList = _cache.GetCache <List <string> >("__JobUserCacheKey");
                if (jobUserList != null && jobUserList.Count != 0)
                {
                    //判断当前用户是否在集合里面
                    if (!jobUserList.Contains(userId))
                    {
                        jobUserList.AddRange(new List <string> {
                            userId
                        });
                    }
                    _cache.WriteCache(jobUserList, "__JobUserCacheKey");
                }
                else
                {
                    jobUserList = new List <string> {
                        userId
                    };
                    _cache.WriteCache(jobUserList, "__JobUserCacheKey");
                }
                #endregion

                #region 2-获取此用户已经在使用的任务列表
                //2-获取此用户已经在使用的任务列表
                List <string> usedJobList = _cache.GetCache <List <string> >(userId + "_UsedJobListCacheKey");
                if (usedJobList == null || usedJobList.Count == 0)
                {
                    usedJobList = new List <string>();
                    //如果还没使用任何任务,则直接全部加入
                    usedJobList.AddRange(jobCode);
                    //加入缓存
                    _cache.WriteCache(usedJobList, userId + "_UsedJobListCacheKey");
                }
                else
                {
                    //检测是否重复开启
                    List <string> temp = jobCode.ToList();
                    //筛选出差集
                    //List<string> except = usedJobList.Except(temp).ToList();
                    //取并集并去重
                    List <string> union = usedJobList.Union(temp).Distinct().ToList();
                    if (union.Count > 0)
                    {
                        usedJobList = union;
                        //加入缓存
                        _cache.WriteCache(usedJobList, userId + "_UsedJobListCacheKey");
                    }
                }
                #endregion

                #region 3-检测每个任务是否开启
                //防止重复开启任务
                Dictionary <string, JobState> doJob = new Dictionary <string, JobState>();
                //3-检测每个任务是否开启
                Dictionary <string, JobState> jobStateDict = _cache.GetCache <Dictionary <string, JobState> >("__JobStateCacheKey");
                if (jobStateDict == null || jobStateDict.Count == 0)
                {
                    jobStateDict = new Dictionary <string, JobState>();
                    //检测状态
                    foreach (string code in jobCode)
                    {
                        jobStateDict.Add(code, JobState.Open);
                        doJob.Add(code, JobState.Open);
                    }
                }
                else
                {
                    foreach (string code in jobCode)
                    {
                        if (!jobStateDict.ContainsKey(code))
                        {
                            jobStateDict.Add(code, JobState.Open);
                            doJob.Add(code, JobState.Open);
                        }
                        else
                        {
                            if (jobStateDict[code] == JobState.Closed)
                            {
                                jobStateDict[code] = JobState.Open;
                                doJob.Add(code, JobState.Open);
                            }
                        }
                    }
                }
                _cache.WriteCache(jobStateDict, "__JobStateCacheKey");
                #endregion

                #region 4-开启任务并推送消息
                ////4-开启任务并推送消息
                ////准备广播消息的用户ID
                //List<string> connIds = new List<string>();
                //foreach (string id in userList)
                //{
                //    connIds.Add(connId[id]);
                //}
                this.OpenJob(userId, doJob);

                #endregion

                #region 弃用代码
                ////已有任务
                //Dictionary<string, JobState> historyJobStatedict = new Dictionary<string, JobState>();
                ////新加任务
                //Dictionary<string, JobState> newJobStatedict = new Dictionary<string, JobState>();

                //_jobStateDict.TryGetValue(userId, out historyJobStatedict);
                //if (historyJobStatedict == null || historyJobStatedict.Count == 0)
                //{
                //    historyJobStatedict = new Dictionary<string, JobState>();
                //    //添加任务状态到字典
                //    foreach (string code in jobCode)
                //    {
                //        historyJobStatedict.Add(code, JobState.Open);
                //    }

                //    newJobStatedict = historyJobStatedict;

                //    _jobStateDict.TryAdd(userId, historyJobStatedict);
                //}
                //else
                //{
                //    //添加未标识的任务
                //    foreach (string code in jobCode)
                //    {
                //        //如果不包含该任务,则直接添加
                //        if (!historyJobStatedict.ContainsKey(code))
                //        {
                //            historyJobStatedict.Add(code, JobState.Open);
                //            newJobStatedict.Add(code, JobState.Open);
                //        }
                //        else
                //        {
                //            //如果任务是关闭状态,则开启它
                //            if (historyJobStatedict[code] == JobState.Closed)
                //            {
                //                historyJobStatedict[code] = JobState.Open;
                //                newJobStatedict.Add(code, JobState.Open);
                //            }
                //        }
                //    }

                //    _jobStateDict[userId] = historyJobStatedict;
                //}
                #endregion
            }
        }
Пример #8
0
        /// <summary>
        /// 获取缓存的值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>

        public static object GetCache(string key)
        {
            return(WebCache.GetCache(key));
        }