/// <summary> /// 发送微信消息给其他服务 /// </summary> /// <param name="msg"></param> public void notifyMessage(WXMsg msg) { foreach (IWXMessageObserver observer in observers) { observer.ReceiveMessage(msg); } }
/// <summary> /// 接收来自该用户的消息 /// </summary> /// <param name="msg"></param> public void ReceiveMsg(WXMsg msg) { _recvedMsg.Add(msg.Time, msg); if (MsgRecved != null) { MsgRecved(msg); } }
/// <summary> /// 接收来自该用户的消息 /// </summary> /// <param name="msg"></param> public void ReceiveMsg(WXMsg msg) { if (!_recvedMsg.Keys.Contains(msg.Time)) { _recvedMsg.Add(msg.Time, msg); } if (MsgRecved != null) { MsgRecved(msg); } }
/// <summary> /// 向该用户发送消息 /// </summary> /// <param name="msg"></param> public void SendMsg(WXMsg msg, bool showOnly) { //发送 if (!showOnly) { WXService wxs = new WXService(); wxs.SendMsg(msg.Msg, msg.From, msg.To, msg.Type); } _sentMsg.Add(msg.Time, msg); if (MsgSent != null) { MsgSent(msg); } }
/// <summary> /// 获取最近的一条消息 /// </summary> /// <returns></returns> public WXMsg GetLatestMsg() { WXMsg msg = null; if (_sentMsg.Count > 0 && _recvedMsg.Count > 0) { msg = _sentMsg.Last().Value.Time > _recvedMsg.Last().Value.Time ? _sentMsg.Last().Value : _recvedMsg.Last().Value; } else if (_sentMsg.Count > 0) { msg = _sentMsg.Last().Value; } else if (_recvedMsg.Count > 0) { msg = _recvedMsg.Last().Value; } else { msg = null; } return(msg); }
/// <summary> /// UI界面显示接收消息 /// </summary> private void ShowReceiveMsg(WXMsg msg) { if (_meUser == null || _friendUser == null) { return; } string str = @"<script type=""text/javascript"">window.location.hash = ""#ok"";</script> <div class=""chat_content_group buddy""> <img class=""chat_content_avatar"" src=""" + _friend_base64 + @""" width=""40px"" height=""40px""> <p class=""chat_nick"">" + _friendUser.ShowName + @"</p> <p class=""chat_content"">" + msg.Msg + @"</p> </div><a id='ok'></a>"; if (_totalHtml == "") { _totalHtml = _baseHtml; } msg.Readed = true; webKitBrowser1.DocumentText = _totalHtml = _totalHtml.Replace("<a id='ok'></a>", "") + str; }
/// <summary> /// 点击发送 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { if (textBox1.Text != null && _friendUser != null && _meUser != null) { WXMsg msg = new WXMsg(); msg.From = _meUser.UserName; msg.Msg = textBox1.Text; msg.Readed = false; msg.To = _friendUser.UserName; msg.Type = 1; msg.Time = DateTime.Now; _friendUser.SendMsg(msg, false); } }
/// <summary> /// 收到新消息 /// </summary> /// <param name="msg"></param> void _friendUser_MsgRecved(WXMsg msg) { ShowReceiveMsg(msg); }
/// <summary> /// 发送消息完成 /// </summary> /// <param name="msg"></param> void _friendUser_MsgSent(WXMsg msg) { ShowSendMsg(msg); }
/// <summary> /// 接收来自该用户的消息 /// </summary> /// <param name="msg"></param> public void ReceiveMsg(WXMsg msg) { if (!_recvedMsg.Keys.Contains(msg.Time)) _recvedMsg.Add(msg.Time, msg); if (MsgRecved != null) { MsgRecved(msg); } }
/// <summary> /// /// </summary> private void DoMainLogic() { _lblWait.BringToFront(); ((Action)(delegate() { WXService wxs = new WXService(); JObject init_result = wxs.WxInit(); //初始化 List<object> contact_all = new List<object>(); if (init_result != null) { _me = new WXUser(); _me.UserName = init_result["User"]["UserName"].ToString(); _me.City = ""; _me.HeadImgUrl = init_result["User"]["HeadImgUrl"].ToString(); _me.NickName = init_result["User"]["NickName"].ToString(); _me.Province = ""; _me.PYQuanPin = init_result["User"]["PYQuanPin"].ToString(); _me.RemarkName = init_result["User"]["RemarkName"].ToString(); _me.RemarkPYQuanPin = init_result["User"]["RemarkPYQuanPin"].ToString(); _me.Sex = init_result["User"]["Sex"].ToString(); _me.Signature = init_result["User"]["Signature"].ToString(); foreach (JObject contact in init_result["ContactList"]) //部分好友名单 { WXUser user = new WXUser(); user.UserName = contact["UserName"].ToString(); user.City = contact["City"].ToString(); user.HeadImgUrl = contact["HeadImgUrl"].ToString(); user.NickName = contact["NickName"].ToString(); user.Province = contact["Province"].ToString(); user.PYQuanPin = contact["PYQuanPin"].ToString(); user.RemarkName = contact["RemarkName"].ToString(); user.RemarkPYQuanPin = contact["RemarkPYQuanPin"].ToString(); user.Sex = contact["Sex"].ToString(); user.Signature = contact["Signature"].ToString(); _contact_latest.Add(user); } } JObject contact_result = wxs.GetContact(); //通讯录 if (contact_result != null) { foreach (JObject contact in contact_result["MemberList"]) //完整好友名单 { WXUser user = new WXUser(); user.UserName = contact["UserName"].ToString(); user.City = contact["City"].ToString(); user.HeadImgUrl = contact["HeadImgUrl"].ToString(); user.NickName = contact["NickName"].ToString(); user.Province = contact["Province"].ToString(); user.PYQuanPin = contact["PYQuanPin"].ToString(); user.RemarkName = contact["RemarkName"].ToString(); user.RemarkPYQuanPin = contact["RemarkPYQuanPin"].ToString(); user.Sex = contact["Sex"].ToString(); user.Signature = contact["Signature"].ToString(); contact_all.Add(user); } } IOrderedEnumerable<object> list_all = contact_all.OrderBy(e => (e as WXUser).ShowPinYin); WXUser wx; string start_char; foreach (object o in list_all) { wx = o as WXUser; start_char = wx.ShowPinYin == "" ? "" : wx.ShowPinYin.Substring(0, 1); if (!_contact_all.Contains(start_char.ToUpper())) { _contact_all.Add(start_char.ToUpper()); } _contact_all.Add(o); } this.BeginInvoke((Action)(delegate() //等待结束 { _lblWait.Visible = false; wChatList1.Items.AddRange(_contact_latest.ToArray()); //近期联系人 wFriendsList1.Items.AddRange(_contact_all.ToArray()); //通讯录 wpersonalinfo.FriendUser = _me; })); string sync_flag = ""; JObject sync_result; while (true) { sync_flag = wxs.WxSyncCheck(); //同步检查 if (sync_flag == null) { continue; } //这里应该判断 sync_flag中selector的值 else //有消息 { sync_result = wxs.WxSync(); //进行同步 if (sync_result != null) { if (sync_result["AddMsgCount"] != null && sync_result["AddMsgCount"].ToString() != "0") { foreach (JObject m in sync_result["AddMsgList"]) { string from = m["FromUserName"].ToString(); string to = m["ToUserName"].ToString(); string content = m["Content"].ToString(); string type = m["MsgType"].ToString(); WXMsg msg = new WXMsg(); msg.From = from; msg.Msg = type == "1" ? content : "请在其他设备上查看消息"; //只接受文本消息 msg.Readed = false; msg.Time = DateTime.Now; msg.To = to; msg.Type = int.Parse(type); if (msg.Type == 51) //屏蔽一些系统数据 { continue; } this.BeginInvoke((Action)delegate() { WXUser user; bool exist_latest_contact = false; foreach (Object u in wChatList1.Items) { user = u as WXUser; if (user != null) { if (user.UserName == msg.From && msg.To == _me.UserName) //接收别人消息 { wChatList1.Items.Remove(user); wChatList1.Items.Insert(0, user); exist_latest_contact = true; user.ReceiveMsg(msg); break; } else if (user.UserName == msg.To && msg.From == _me.UserName) //同步自己在其他设备上发送的消息 { wChatList1.Items.Remove(user); wChatList1.Items.Insert(0, user); exist_latest_contact = true; user.SendMsg(msg,true); break; } } } if (!exist_latest_contact) { foreach (object o in wFriendsList1.Items) { WXUser friend = o as WXUser; if (friend != null && friend.UserName == msg.From && msg.To == _me.UserName) { wChatList1.Items.Insert(0, friend); friend.ReceiveMsg(msg); break; } if (friend != null && friend.UserName == msg.To && msg.From == _me.UserName) { wChatList1.Items.Insert(0, friend); friend.SendMsg(msg,true); break; } } } wChatList1.Invalidate(); }); } } } } System.Threading.Thread.Sleep(10); } })).BeginInvoke(null, null); }
/// <summary> /// /// </summary> private void DoMainLogic() { _lblWait.BringToFront(); ((Action)(delegate() { WXService wxs = new WXService(); #region MyRegion JObject init_result = wxs.WxInit(); //初始化 List<object> contact_all = new List<object>(); if (init_result != null) { _me = new WXUser(); _me.UserName = init_result["User"]["UserName"].ToString(); _me.City = ""; _me.HeadImgUrl = init_result["User"]["HeadImgUrl"].ToString(); _me.NickName = init_result["User"]["NickName"].ToString(); _me.Province = ""; _me.PYQuanPin = init_result["User"]["PYQuanPin"].ToString(); _me.RemarkName = init_result["User"]["RemarkName"].ToString(); _me.RemarkPYQuanPin = init_result["User"]["RemarkPYQuanPin"].ToString(); _me.Sex = init_result["User"]["Sex"].ToString(); _me.Signature = init_result["User"]["Signature"].ToString(); foreach (JObject contact in init_result["ContactList"]) //部分好友名单 { WXUser user = new WXUser(); user.UserName = contact["UserName"].ToString(); user.City = contact["City"].ToString(); user.HeadImgUrl = contact["HeadImgUrl"].ToString(); user.NickName = contact["NickName"].ToString(); user.Province = contact["Province"].ToString(); user.PYQuanPin = contact["PYQuanPin"].ToString(); user.RemarkName = contact["RemarkName"].ToString(); user.RemarkPYQuanPin = contact["RemarkPYQuanPin"].ToString(); user.Sex = contact["Sex"].ToString(); user.Signature = contact["Signature"].ToString(); _contact_latest.Add(user); } } JObject contact_result = wxs.GetContact(); //通讯录 if (contact_result != null) { foreach (JObject contact in contact_result["MemberList"]) //完整好友名单 { WXUser user = new WXUser(); user.UserName = contact["UserName"].ToString(); user.City = contact["City"].ToString(); user.HeadImgUrl = contact["HeadImgUrl"].ToString(); user.NickName = contact["NickName"].ToString(); user.Province = contact["Province"].ToString(); user.PYQuanPin = contact["PYQuanPin"].ToString(); user.RemarkName = contact["RemarkName"].ToString(); user.RemarkPYQuanPin = contact["RemarkPYQuanPin"].ToString(); user.Sex = contact["Sex"].ToString(); user.Signature = contact["Signature"].ToString(); user.Alias = contact["Alias"].ToString(); contact_all.Add(user); } } IOrderedEnumerable<object> list_all = contact_all.OrderBy(e => (e as WXUser).ShowPinYin); WXUser wx; string start_char; foreach (object o in list_all) { wx = o as WXUser; start_char = wx.ShowPinYin == "" ? "" : wx.ShowPinYin.Substring(0, 1); if (!_contact_all.Contains(start_char.ToUpper())) { _contact_all.Add(start_char.ToUpper()); } _contact_all.Add(o); } this.BeginInvoke((Action)(delegate() //等待结束 { _lblWait.Visible = false; wChatList1.Items.AddRange(_contact_latest.ToArray()); //近期联系人 wFriendsList1.Items.AddRange(_contact_all.ToArray()); //通讯录 wpersonalinfo.FriendUser = _me; })); #endregion string sync_flag = ""; JObject sync_result; List<WXUser> userlist = new List<WXUser>(); foreach (var item in contact_all) { userlist.Add((WXUser)item); } #region MyRegion while (true) { System.Threading.Thread.Sleep(3000); sync_flag = wxs.WxSyncCheck(); //同步检查 if (sync_flag == null) { continue; } //手机登出 if (sync_flag.Contains("retcode:\"1100\"")) { Application.Exit(); } //你在其他地方登录了 WEB 版微信 if (sync_flag.Contains("retcode:\"1101\"")) { Application.Exit(); } if (!sync_flag.Contains("retcode:\"0\"")) { Application.Exit(); } try { sync_result = wxs.WxSync(); //进行同步 } catch (Exception) { sync_result = null; } if (sync_result == null) { continue; } if (sync_result["AddMsgCount"] == null || sync_result["AddMsgCount"].ToString() == "0") continue; foreach (JObject m in sync_result["AddMsgList"]) { string from = m["FromUserName"].ToString(); string to = m["ToUserName"].ToString(); string content = m["Content"].ToString(); string type = m["MsgType"].ToString(); WXMsg msg = new WXMsg(); if (content.IndexOf("http://mp.weixin.qq.com/") == -1 || msg.Type == 51) //屏蔽一些系统数据 { if (content.IndexOf("http://mp.weixin.qq.com/") ==0) { byte[] bytes = BaseService.SendGetRequest(content); if (bytes != null) { var oldhtml = Encoding.UTF8.GetString(bytes); } } continue; } msg.From = from; msg.Msg = type == "1" ? content : "请在其他设备上查看消息"; //只接受文本消息 msg.Readed = false; msg.Time = DateTime.Now; msg.To = to; msg.Type = int.Parse(type); var createTime = m["CreateTime"].ToString(); var username = string.Empty; var userEname = string.Empty; var Signature = string.Empty; var wxuser = userlist.FirstOrDefault(a => a.UserName == msg.From); if (wxuser == null) continue; if (string.IsNullOrEmpty(wxuser.Alias)) continue; userEname = wxuser.Alias; username = wxuser.NickName; Signature = wxuser.Signature; var imgpath = "E:/Github/nodeclub/public/img/" + userEname + ".jpg"; if (!File.Exists(imgpath)) { Image image = wxs.GetIcon(wxuser.UserName); image.Save(imgpath); imgpath = "E:/Github/nodeclub/public/qr/" + userEname + ".jpg"; var bytes = BaseService.SendGetRequest("http://open.weixin.qq.com/qr/code/?username=" + userEname); image = Image.FromStream(new MemoryStream(bytes)); image.Save(imgpath); } new DBService.MongoHelper().AddTopic(createTime, content, userEname, username, Signature); } } #endregion })).BeginInvoke(null, null); }