/// <summary> /// 生成OnlineUser用户,并且加入在线列表 /// </summary> /// <param name="uname">用户名,无则默认生成</param> /// <param name="UserID">如果输入该值,无则新建</param> /// <returns></returns> public M_OnlineUser GetUser(string uname = "", string UserID = "") { HttpRequest request = HttpContext.Current.Request; HttpResponse response = HttpContext.Current.Response; M_OnlineUser model = GetLogin(); //如果输入了用户ID,则强制刷新,避免缓存,用于用户行为追踪 if (model != null && !string.IsNullOrEmpty(UserID) && !model.UserID.Equals(UserID)) { model = null; } if (model == null) { model = new M_OnlineUser(); model.UserID = string.IsNullOrEmpty(UserID) ? function.GetRandomString(4).ToLower() : UserID; model.UserName = string.IsNullOrEmpty(uname) ? "游客" : uname; model.UserFace = ""; model.IsVisitor = true; model.LastTime = DateTime.Now; response.Cookies["chat"]["chatuid"] = model.UserID; response.Cookies["chat"]["chatuname"] = HttpUtility.UrlEncode(model.UserName); response.Cookies["chat"].Expires = DateTime.Now.AddMonths(1); } AddOnLineUser(model); return(model); }
//获取已登录的用户,无则返回null public M_OnlineUser GetLogin() { HttpRequest request = HttpContext.Current.Request; HttpResponse response = HttpContext.Current.Response; M_UserInfo mu = buser.GetLogin(); M_OnlineUser model = null; if (mu == null || mu.UserID < 1) { if (request.Cookies["chat"] != null) { model = new M_OnlineUser(); model.UserID = request.Cookies["chat"]["chatuid"]; model.UserName = HttpUtility.UrlDecode(request.Cookies["chat"]["chatuname"]); model.UserFace = ""; model.IsVisitor = true; model.LastTime = DateTime.Now; } } else//已登录用户 { model = new M_OnlineUser(); model.UserID = mu.UserID.ToString(); model.UserName = mu.HoneyName; model.UserFace = mu.UserFace; model.LastTime = DateTime.Now; } if (model != null) { AddOnLineUser(model); } return(model); }
//----OnlineUser //移除超时的用户(getmsg中一并处理) public void AddOnLineUser(M_OnlineUser model) { //检测用户是否存在,存在则更新时间,不存在则移除 bool isexist = false; foreach (M_OnlineUser user in OnlineUserList) { if (user.UserID.Equals(model.UserID)) { user.LastTime = DateTime.Now; isexist = true; break; } } if (!isexist) { OnlineUserList.Add(model); } }
public void MyBind() { //后期将其分离 M_OnlineUser user = chatBll.GetLogin(); if (user != null) { UserName_L.Text = user.UserName; UserID_Hid.Value = user.UserID; } if (!string.IsNullOrEmpty(Login)) { function.Script(this, "VisitorToLogin('用户" + DateTime.Now.ToString("yyyyMMddHHmmss") + "','" + Request["idflag"] + "');"); } else if (user == null) { function.Script(this, "ShowModal();"); } if (!string.IsNullOrEmpty(UserFlag)) { M_OnlineUser mu = chatBll.GetModelByUid(UserFlag); //function.WriteErrMsg(mu.UserID+":"+mu.UserName); function.Script(this, "ChangeTalker('" + mu.UserID + "','" + mu.UserName + "','" + mu.UserFace + "');"); } else if (UserID > 0) { M_UserInfo mu = buser.GetSelect(UserID); function.Script(this, "ChangeTalker('" + mu.UserID + "','" + mu.UserName + "','" + mu.UserFace + "');"); } if (buser.CheckLogin()) { DataTable dt = friendBll.SelMyFriend(Convert.ToInt32(user.UserID)); Friend_RPT.DataSource = dt; Friend_RPT.DataBind(); if (dt == null) { Friend_Num.Text = "0"; } else { Friend_Num.Text = dt.Rows.Count.ToString(); function.Script(this, "InitUserList(" + JsonHelper.JsonSerialDataTable(dt) + ");"); } } //用于显示欢迎信息 if (CodeID > 0) { M_Temp tempMod = tempBll.SelReturnModel(CodeID); if (tempMod != null && tempMod.UseType == 12) { Wel_Hid.Value = tempMod.Str2; function.Script(this, "ShowWel('" + tempMod.Str1 + "');"); } } //----------------------------------- //if (badmin.CheckLogin()) //{ // visitor_ul.Visible = true; // List<M_OnlineUser> list = chatBll.GetOnlineVisitor(); // Y_RPT.DataSource = list; // Y_RPT.DataBind(); // Unum_L.Text = list.Count.ToString(); // M_AdminInfo adminmod=badmin.GetAdminLogin(); // DataTable dt = SeatBll.SelBySUid(adminmod.AdminId); // function.Script(this, "SetMyInfo('" + adminmod.AdminId + "','" + adminmod.AdminName + "','" + dt.Rows[0]["S_ImgUrl"] + "');BeginInit();"); //} //else { //DataTable dt = SeatBll.Select_All(); //DataTable dt = UserDT; //dt.DefaultView.RowFilter = "Status=0"; //dt = dt.DefaultView.ToTable(); B_User_Plat upBll = new B_User_Plat(); DataTable dt = upBll.SelByCompany(B_User_Plat.GetLogin().CompID); Customs_RPT.DataSource = dt; Customs_RPT.DataBind(); } if (user != null) { function.Script(this, "SetMyInfo('" + user.UserID + "','" + user.UserName + "','" + user.UserFace + "');BeginInit();"); } }
public void ProcessRequest(HttpContext context) { HttpRequest request = context.Request; HttpResponse response = context.Response; string action = request["action"]; string result = ""; //传两个uid,rece目标uid switch (action) { case "sendmsg": { string uid = request.Form["uid"]; M_OnlineUser mu = chatBll.GetModelByUid(uid); M_ChatMsg model = new M_ChatMsg(); model.UserID = mu.UserID; model.UserName = mu.UserName; model.UserFace = mu.UserFace; model.Content = request.Form["content"]; model.ReceUser = StrHelper.IdsFormat(request.Form["rece"]); chatBll.Insert(model); B_ChatMsg.MsgList.Add(model); result = "99"; //更新在线用户信息(一个计数,每过十秒更新一次状态) response.Write(result); } break; case "getmsg": //同时更新在线状态 { string uid = request.Form["uid"]; //我要接收谁的信息 string rece = request.Form["rece"]; //在线用户在进页面的时候加入这里只更新,用户自已提交ID上来 M_OnlineUser mu = chatBll.GetModelByUid(uid); List <M_ChatMsg> msglist = msgBll.GetMsgByUid(mu.UserID.ToString(), rece); string contents = ""; chatBll.UpdateTime(mu.UserID.ToString()); string onlinelist = chatBll.GetOnlineStr(); //在线列表IDS格式 string unread = chatBll.GetMsgCount(mu.UserID); foreach (M_ChatMsg model in msglist) { //model.ReceUser = model.ReceUser.Replace("," + mu.UserID + ",", ",");//移除用户指示 result += "{\"UserName\":\"" + model.UserName + "\",\"UserFace\":\"" + model.UserFace + "\",\"CDate\":\"" + model.CDate.ToString("HH:mm:ss") + "\"},"; contents += boundary + model.Content; } result = result.TrimEnd(','); result = "[" + result + "]"; result += boundary + onlinelist; result += boundary + unread; result += contents; response.Write(result); } break; case "getonline": //初次登录时检测一次 { string uid = context.Request.Form["uid"]; chatBll.UpdateTime(uid); result = chatBll.GetOnlineStr(); //在线列表IDS格式 response.Write(result); } break; case "userlogin": //用户与游客登录 { string uname = request.Form["name"]; string userid = request.Form["userid"]; M_OnlineUser model = chatBll.GetUser(uname, userid); result = "{\"UserID\":\"" + model.UserID + "\",\"UserName\":\"" + model.UserName + "\",\"UserFace\":\"" + model.UserFace + "\"}"; response.Write(result); } break; case "getonlinelist": result = chatBll.GetOnlineJson(true); //在线列表Json格式 response.Write(result); break; case "gethistorymsg": //获取聊天记录: { string uid = request.Form["uid"]; //我要接收谁的信息 string rece = request.Form["rece"]; M_OnlineUser mu = chatBll.GetModelByUid(uid); DataTable msglist = msgBll.SelHistoryMsg(mu.UserID.ToString(), rece); chatBll.UpdateTime(mu.UserID.ToString()); string contents = ""; foreach (DataRow dr in msglist.Rows) { result += "{\"UserName\":\"" + dr["UserName"] + "\",\"UserFace\":\"" + dr["UserFace"] + "\",\"CDate\":\"" + Convert.ToDateTime(dr["CDate"]).ToString("HH:mm:ss") + "\",\"Type\":\"" + (mu.UserName.Equals(dr["UserName"]) ? "my" : "other") + "\"},"; contents += boundary + dr["Content"]; } result = result.TrimEnd(','); result = "[" + result + "]"; result += contents; response.Write(result); } break; case "getunreadmsg": //获取未读消息 { string uid = request.Form["uid"]; M_OnlineUser mu = chatBll.GetModelByUid(uid); List <M_ChatMsg> msglist = msgBll.GetUnreadMsgByUid(mu.UserID.ToString()); string contents = ""; chatBll.UpdateTime(mu.UserID.ToString()); foreach (M_ChatMsg model in msglist) { result += "{\"UserID\":\"" + model.UserID + "\",\"UserName\":\"" + model.UserName + "\",\"UserFace\":\"" + model.UserFace + "\",\"CDate\":\"" + model.CDate.ToString("HH:mm:ss") + "\"},"; contents += boundary + model.Content; } result = result.TrimEnd(','); result = "[" + result + "]"; result += contents; response.Write(result); } break; } response.Flush(); response.End(); }