- /*
- * AtUser通知用户 C#
- *
- * MIT License
- * Date: 2012-03-20
- */
- public class AtUser {
- //Json数据List集合
- List<cJsonUser> JsonList = new List<cJsonUser>();
- /// <summary>
- /// 用户匹配
- /// </summary>
- /// <param name="Content"></param>
- /// <param name="JsonUser"></param>
- /// <param name="CallBackContent"></param>
- public void AtUserMatch(string UserID, string Content, string JsonUser, out string CallBackContent) {
- try {
- FriendModels friend = new FriendModels();
- if (JsonUser == "0") {
- CallBackContent = Content;
- return;
- } else {
- JsonUser = string.IsNullOrEmpty(JsonUser) ? friend.GetJsonUserList(UserID) : JsonUser;
- }
- //客户端Json@用户
- JavaScriptSerializer serializer = new JavaScriptSerializer();
- //Json数据映射到对象图
- Dictionary<string, object> JsonDictionary = (Dictionary<string, object>)serializer.DeserializeObject(JsonUser);
- object aValue;
- //尝试获从user属性取值输出到对象
- if (JsonDictionary != null && JsonDictionary.TryGetValue("user", out aValue)) {
- //输出的对象装箱到对象数组
- object[] aUser = (object[])aValue;
- //获取对象数组到Dictionary<string, object>
- for (int iUser = 0; iUser < aUser.Length; iUser++) {
- Dictionary<string, object> JsonArray = (Dictionary<string, object>)aUser[iUser];
- object oUserName;
- object oUserID;
- string GUserName = JsonArray.TryGetValue("username", out oUserName) ? oUserName.ToString() : null;
- string GUserID = JsonArray.TryGetValue("userid", out oUserID) ? oUserID.ToString() : null;
- if (!string.IsNullOrEmpty(GUserName) || !string.IsNullOrEmpty(GUserID)) {
- //正则匹配,在关键字前后插入封闭html标签,使用两次正则
- string FristPattern = string.Format(@"(?=@{0})", GUserName);
- string LastPattern = string.Format(@"(?<=@{0})", GUserName);
- string FristReplaceMent = string.Format("<a href=\\"/Space/Users/{0}\\">", GUserID);
- string LastReplaceMent = "</a>";
- Regex FristRegexContent = new Regex(FristPattern);
- int OldContentLength = Content.Length;
- Content = FristRegexContent.Replace(Content, FristReplaceMent);
- Regex LastRegexContent = new Regex(LastPattern);
- Content = LastRegexContent.Replace(Content, LastReplaceMent);
- if (Content.Length > OldContentLength) {
- //添加通知用户到List集合
- JsonList.Add(new cJsonUser { UserName = GUserName, UserID = GUserID });
- }
- }
- }
- }
- } catch (Exception ex) {
- LogHelperClass.WriteLog("用户匹配错误", ex);
- }
- CallBackContent = Content;
- }
- public void AtUserNotification(string UserID, string Href) {
- MessageModels notification = new MessageModels();
- if (JsonList.Count > 0) {
- for (int iListUser = 0; iListUser < JsonList.Count; iListUser++) {
- if (!UserID.Equals(JsonList[iListUser].UserID)) {
- notification.SendNotification(UserID, JsonList[iListUser].UserID, notification.msgtypeproperty.AtUser, Href, "AtUser");
- }
- }
- }
- }
- public void close() {
- JsonList.Clear();
- }
- }
- public class cJsonUser {
- public string UserName { get; set; }
- public string UserID { get; set; }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/2306201512903.html
来源: http://www.codesnippet.cn/detail/2306201512903.html