Monday, February 1, 2010

PowerTalk.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Caching;
namespace PowerTalkBox
{
///
/// PowerTalk 的摘要说明
///

public class PowerTalk
{
///
/// WWW地址
///

public static string FaceWwwPath = "";
///
/// PowerTalk
///

public PowerTalk()
{

}
#region 操作用户
///
/// 随机游客用户添加
///

public static string NewClientUserLogin(bool AutoInList)
{
string UserIDStr = "";
UserIDStr = RadomName();

while (ExistUser("游客" + UserIDStr))
{
UserIDStr = RadomName();
}
UserInfo UserItem = new UserInfo();
UserItem.UserID = "游客" + UserIDStr;
HttpRuntime.Cache.Add(UserItem.UserID, false, new AggregateCacheDependency(), Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(20), CacheItemPriority.Normal, null);
UserItem.UserPersonInfo = UserIDStr + " 登录时间" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
UserItem.Level = "0";
if (AutoInList)
{
ListClass.Engin_UserList.Add(UserItem);
}
return UserItem.UserID;
}
///
/// 用户添加
///

/// 用户ID
/// 用户信息
/// 级别
public static void NewUserLogin(string UserIDStr, string UserNameStr, string Level)
{
if (ExistUser(UserIDStr))
{
throw new Exception("UserID有重复!");
}
UserInfo UserItem = new UserInfo();
UserItem.UserID = UserIDStr;
UserItem.UserPersonInfo = UserNameStr;
UserItem.Level = Level;
ListClass.Engin_UserList.Add(UserItem);
HttpRuntime.Cache.Add(UserIDStr, false, new AggregateCacheDependency(), Cache.NoAbsoluteExpiration,TimeSpan.FromMinutes(20),CacheItemPriority.Normal, null);

}
///
/// 随机起名
///

///
public static string RadomName()
{
Random rdm = new Random();
string UserIDStr = "";
for (int i = 1; i <= 5; i++)
{
UserIDStr += rdm.Next(1, 9).ToString();
}
return UserIDStr;
}
///
/// 判断有无重名
///

/// 姓名
///
public static bool ExistUser(string ID)
{
FindClass Fc = new FindClass();
Fc.UserId = ID;
return ListClass.Engin_UserList.Exists(Fc.PredicateUser);
}
///
/// 用户List列表
///

///
public static List UserInfos(string ID)
{
FindClass Fc = new FindClass();
Fc.UserId = ID;
return ListClass.Engin_UserList.FindAll(Fc.PredicateUserList);
}
///
/// 查找用户名
///

/// 用户ID
///
public static UserInfo FindUserInfo(string ID)
{
FindClass Fc = new FindClass();
Fc.UserId = ID;
UserInfo Ui = ListClass.Engin_UserList.Find(Fc.PredicateUser);
return Ui;
}

///
/// 删除用户
///

/// 用户ID
///
public static bool DeleteUserInfo(string ID)
{
HttpRuntime.Cache.Remove(ID);
UserInfo Ui = FindUserInfo(ID);
DeleteChatInfo(ID);//删除记录
return ListClass.Engin_UserList.Remove(Ui);
}
#endregion
#region 记录操作
///
/// 添加新记录:群聊
///

/// 发送者
/// 内容
public static void AddChatInfo(string _Sender, string _SendContent)
{
foreach (UserInfo Uif in ListClass.Engin_UserList)
{
ChatInfo CiItem = new ChatInfo();
CiItem.Sender = _Sender;
CiItem.Reciver = Uif.UserID;
HttpRuntime.Cache[CiItem.Reciver] = true;
CiItem.SendContent = _SendContent;
CiItem.SendTime = DateTime.Now;
ListClass.Engin_ChatList.Add(CiItem);
}
}
///
/// 添加新记录:单聊
///

/// 发送者
/// 接收者
/// 内容
public static void AddChatInfo(string _Sender, string _Reciver, string _SendContent)
{
HttpRuntime.Cache[_Reciver] = true;
ChatInfo CiItem = new ChatInfo();
CiItem.Sender = _Sender;
CiItem.SendContent = _SendContent;
CiItem.Reciver = _Reciver;
CiItem.SendTime = DateTime.Now;
ListClass.Engin_ChatList.Add(CiItem);
}
/*三种模式 1群聊,多对多 2单对多 3单对单
*1聊天室效果时
*2管理员回答问题时
*3客户问问题咨询时
*/
///
/// 读取记录
///

/// 接收者ID
///
public static List ReadChatInfo(string ID,PowerTalkBoxEnum.Enum.SystemMode MsnMd)
{
List ChatInfoArray = new List();
bool IsHaveMsg = false;
if (HttpRuntime.Cache[ID] != null)
{
if (bool.TryParse(HttpRuntime.Cache[ID].ToString(), out IsHaveMsg))
{
if (IsHaveMsg)
{



FindClass Fc = new FindClass();
Fc.UserId = ID;
ChatInfoArray = ListClass.Engin_ChatList.FindAll(Fc.PredicateChat);

foreach (ChatInfo Cif in ChatInfoArray)
{
ListClass.Engin_ChatList.Remove(Cif);
}
HttpRuntime.Cache[ID] = false;
}
}
}

return ChatInfoArray;
}
///
/// 删除记录
///

/// 接收ID
///
public static bool DeleteChatInfo(string ID)
{
FindClass Fc = new FindClass();
Fc.UserId = ID;
List ChatInfoArray = ListClass.Engin_ChatList.FindAll(Fc.PredicateChat);
foreach (ChatInfo Cif in ChatInfoArray)
{
ListClass.Engin_ChatList.Remove(Cif);
}
return true;
}

#endregion
}
///
/// 列表
///

public class ListClass
{
public static PowerTalkBoxEnum.Enum.ChatMode _MsnChatMode = PowerTalkBoxEnum.Enum.ChatMode.OneToMore;
//在线人数
public static int OnlineMsnCount=0;
private static List _Engin_UserList = new List();
private static List _Engin_ChatList = new List();
private static List _Engin_MsnList = new List();
#region 定义
//用户列表
public static List Engin_UserList
{
get
{
return _Engin_UserList;
}
set
{
_Engin_UserList = value;
}

}
//聊天记录
public static List Engin_ChatList
{
get
{
return _Engin_ChatList;
}
set
{
_Engin_ChatList = value;
}
}
//MSN记录
public static List Engin_MsnList
{

get
{
return _Engin_MsnList;
}
set
{
_Engin_MsnList = value;
}
}

#endregion
}
//51aspx
///
/// 查找类
///

public class FindClass
{
public string UserId;
public bool PredicateUser(UserInfo s)
{
if (s.UserID == UserId)
{
return true;
}
else
{
return false;
}
}

public bool PredicateUserList(UserInfo s)
{
if (s.UserID != UserId)
{
return true;
}
else
{
return false;
}
}
///
/// MSN一对一时
///

///
///
public bool PredicateMsnUser(ChatInfo c)
{
if ((c.Reciver == UserId) && (c.Sender == UserId))
{
return true;
}
else
{
return false;
}
}
public bool PredicateChat(ChatInfo c)
{
if ((c.Reciver == UserId) && (c.Sender != UserId))
{
return true;
}
else
{
return false;
}
}

}
///
/// MSN用户信息
///

public class MsnUserInfo
{
private string _userID;

private DateTime _lasttime;
public string UserID
{
get { return _userID; }
set
{
if ((value.IndexOf('$') >= 0) (value.IndexOf('') >= 0))
{
throw new Exception("不能有'$',''等非法字符");
}
_userID = value;
}
}

public DateTime LastTime
{
get { return _lasttime; }
set { _lasttime = value; }
}
}
///
/// 用户信息
///

public class UserInfo
{
private string _userID;
private string _userInfo;
private string _level;
public string UserID
{
get { return _userID; }
set
{
if ((value.IndexOf('$') >= 0) (value.IndexOf('') >= 0))
{
throw new Exception("不能有'$',''等非法字符");
}
_userID = value;
}
}
public string UserPersonInfo
{
get { return _userInfo; }
set
{
if (value.IndexOf('$') >= 0 (value.IndexOf('') >= 0))
{
throw new Exception("不能有'$',''等非法字符");
}
_userInfo = value;
}
}
public string Level
{
get { return _level; }
set { _level = value; }
}

}
///
/// 聊天暂存记录
///

public class ChatInfo
{
private string _Sender;
private string _Reciver;
private string _SendContent;
private DateTime _SendTime;
///
/// 发送者
///

public string Sender
{
set { _Sender = value; }
get { return _Sender; }
}
///
/// 接收者
///

public string Reciver
{
set { _Reciver = value; }
get { return _Reciver; }
}
///
/// 内容
///

public string SendContent
{
set { _SendContent = value; }
get { return _SendContent; }
}
///
/// 发送时间
///

public DateTime SendTime
{
set { _SendTime = value; }
get { return _SendTime; }
}
}

}