Exemple #1
0
 /// <summary>
 /// 注册号码
 /// </summary>
 /// <param name="member">注册用户信息</param>
 /// <returns>包含用户号码的用户信息</returns>
 public Member Register(Member member)
 {
     string id = Util.NewMemberId(7);
     member.Id = id;
     if (DALService.AddMember(member))
     {
         return member;
     }
     else
     {
        MyLogger.Logger.Warn("用户添加失败");
         return null;
     }
 }
Exemple #2
0
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="member">用户</param>
        /// <returns>添加成功返回true,添加失败返回false</returns>
        static public bool AddMember(Member member)
        {

            MySqlCommand cmd = null;
            try
            {

                string sql = String.Format("insert into tblMember(id,nickName,password,birthday,sex,status,information) values(?id,?nickName,?password,?birthday,?sex,?status,?information)");
                cmd = new MySqlCommand(sql, Conn);
                cmd.Parameters.AddWithValue("id", member.Id);
                cmd.Parameters.AddWithValue("password", member.Password);
                cmd.Parameters.AddWithValue("nickName", member.NickName);
                cmd.Parameters.AddWithValue("birthday", member.Birthday);
                cmd.Parameters.AddWithValue("sex", member.Sex);
                cmd.Parameters.AddWithValue("status", member.Status);
                cmd.Parameters.AddWithValue("information", member.Infomation);
                Prepare(cmd.Parameters);
                int i1 = cmd.ExecuteNonQuery();

                sql = String.Format("insert into tblFriend(id,groupId,groupName) values(?id,?groupId,?groupName)");
                cmd.Parameters.AddWithValue("groupId", "0");
                cmd.Parameters.AddWithValue("groupName", "我的好友");
                Prepare(cmd.Parameters);
                cmd.CommandText = sql;
                int i2 = cmd.ExecuteNonQuery();


                return i1 == 1 && i2 == 1;
            }
            catch (Exception e)
            {
                MyLogger.Logger.Error("添加用户出现错误\n", e);

                return false;
            }

            finally
            {

                if (cmd != null)
                    cmd.Dispose();


            }
        }
Exemple #3
0
        public void Logoff(Member member)
        {
            MyLogger.Logger.Info(String.Format("用户{0}退出", member.Id + " " + member.NickName));
            ChatEventArgs e = new ChatEventArgs();
            e.Id = member.Id;
            e.NickName = member.NickName;
            e.Type = MessageType.Logoff;
            if (Online.ContainsKey(member.Id))
                Online.Remove(member.Id);
            if (lastUpdateTable.ContainsKey(member.Id))
                lastUpdateTable.Remove(member.Id);
            BroadCatMessage(e);

            PrintOnLineNumber();

        }
Exemple #4
0
        /// <summary>
        /// 处理用户登录
        /// </summary>
        /// <param name="member">包含用户名密码的member</param>
        /// <returns>返回登录结果,如果登录成功,Result.Member 包含用户的信息</returns>
        public Result Login(Member member)
        {


            MyLogger.Logger.Info(String.Format("用户{0}请求登录", member.Id + " " + member.NickName));


            try
            {
              

                ///判断用户名和密码是否正确
                if (!DALService.IsMember(member.Id, member.Password))
                {
                    MyLogger.Logger.Info(String.Format("用户{0}登录失败", member.Id + " " + member.NickName));
                    return new Result() { Status = MessageStatus.Failed };

                }

                member.Password = null;
                this.member = DALService.GetMember(member.Id);

                lastUpdateTable.Add(this.member.Id, DateTime.Now);
                GetEndPoint();
                ///获得回调句柄
                callback = OperationContext.Current.GetCallbackChannel<IChatterCallback>();
                ///获得好友们的id
                RefreshFriendsList();
                ///新建消息处理函数,处理登录、退出消息
                myEventHandler = new ChatEventHandler(HandleEvent);

                ///新建消息参数
                ChatEventArgs e = new ChatEventArgs();
                ///消息发出者的id
                e.Id = member.Id;
                ///消息发出者的昵称
                e.NickName = member.NickName;
                ///消息类型为登录
                e.Type = MessageType.Login;
                ///为每个在线好友订阅事件
                foreach (string friendId in friends.Keys)
                {
                    if (Online.ContainsKey(friendId))
                    {

                        ChatEvent += Online[friendId] as ChatEventHandler;
                    }
                }

                var groups = DALService.GetAllGroups(member.Id);
                foreach (var group in groups)
                {
                    foreach (Member m in group.GroupMember)
                    {
                        if (Online.ContainsKey(m.Id)&&!(friends.ContainsKey(m.Id)))
                        {

                            ChatEvent += Online[m.Id] as ChatEventHandler;
                        }
                    }
                }

                
                ///广播消息 将登录退出的消息发给好友
                BroadCatMessage(e);
                ///加锁,防止多线程同时访问
                lock (lockObj)
                {

                    ///将用户加到在线列表
                    Online.Add(member.Id, myEventHandler);

                }

                MyLogger.Logger.Info(String.Format("用户{0}登录成功", member.Id + " " + member.NickName));
                ///打印在线人数
                PrintOnLineNumber();
            }
            catch (Exception e)
            {
                MyLogger.Logger.Error(member.Id + "登录时候出现错误", e);
                return new Result() { Status = MessageStatus.Failed, Member = null };
            }

            ///返回登录结果
            return new Result() { Status = MessageStatus.OK, Member = DALService.GetMember(member.Id) };
        }
Exemple #5
0
        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="member">用户</param>
        /// <returns></returns>
        static public bool UpdateMemeber(Member member)
        {
            MySqlCommand cmd = null;
            try
            {
                string sql = String.Format("update tblMember set password=?password,sex=?sex,nickName=?nickName,status=?status,information=?information birthday=?birthday where id=?id");
                cmd = new MySqlCommand(sql, Conn);
                cmd.Parameters.AddWithValue("id", member.Id);
                cmd.Parameters.AddWithValue("password", member.Password);
                cmd.Parameters.AddWithValue("nickName", member.NickName);
                cmd.Parameters.AddWithValue("birthday", member.Birthday);
                cmd.Parameters.AddWithValue("sex", member.Sex);
                cmd.Parameters.AddWithValue("status", member.Status);
                cmd.Parameters.AddWithValue("information", member.Infomation);

                Prepare(cmd.Parameters);

                return 1 == cmd.ExecuteNonQuery(); ;

            }
            catch (Exception e)
            {
                MyLogger.Logger.Error("更新用户信息出现错误\n", e);
                return false;
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();

            }
        }
Exemple #6
0
        static public Member GetMember(string id,bool IncludePWD=false)
        {
            MySqlCommand cmd = null;
            Member member = null;
            try
            {

                string sql = String.Format("select * from tblMember where id=?id");
                cmd = new MySqlCommand(sql, Conn);
                cmd.Parameters.AddWithValue("id", id);
                Prepare(cmd.Parameters);

                using (var reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        member = new Member();
                        member.Id = id;
                        if (IncludePWD)
                        {
                            member.Password = reader["password"].ToString();
                        }
                        member.Sex = reader["sex"].ToString();
                        member.NickName = reader["nickName"].ToString();
                        member.Birthday = Convert.ToDateTime(reader["birthday"]);
                        member.Infomation = reader["information"].ToString();
                        if (reader["status"].ToString().Length != 0)
                        {
                            MemberStatus status;
                            // Enum.TryParse<MemberStatus>(reader["status"].ToString(),out status);
                            status = (MemberStatus)Enum.Parse(typeof(MemberStatus), reader["status"].ToString());
                            member.Status = status;
                        }

                    }
                }

                return member;

            }
            catch (Exception e)
            {
                MyLogger.Logger.Error("获得用户出现错误\n", e);

                return null;
            }

            finally
            {

                if (cmd != null)
                    cmd.Dispose();


            }
        }