/// <summary> /// 微信同步检测(检查是否有新消息),返回非空为有新消息 /// </summary> /// <returns></returns> public string WxSyncCheck() { try { return(MessageSync.WxSyncCheck()); //同步检查 } catch (Exception ex) { return(null); } }
/// <summary> /// 最近联系人、消息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void pbMessage_Click(object sender, EventArgs e) { ((Action)(delegate() { bool _start = false; pbFriends.BackgroundImage = Resources.haoyou; pbMessage.BackgroundImage = Resources.message2_1_; List <object> data = MessageSync.Message(); this.BeginInvoke((Action) delegate() { wChatList.Items.Clear(); wChatList.Width = 215; wChatList.Height = 446; wChatList.Items.AddRange(data.ToArray()); //通讯录 this.panelList.Controls.Clear(); this.panelList.Controls.Add(wChatList); wChatList.Show(); }); string sync_flag = ""; JObject sync_result; _start = true; while (true) { #region 定时回复指定人 this.BeginInvoke((Action) delegate() { //指定时间回复指定人 if (DateTime.Now.ToString("HH:mm:ss:ff") == "15:30:00:00") //如果当前时间是XXXX { WXMsg assignMsg = new WXMsg(); WXUser assignUser = new WXUser(); assignMsg.From = MessageSync.userInfo.UserName; assignMsg.Msg = "时间到了"; assignMsg.Readed = false; assignMsg.To = userName; assignMsg.Type = 1; assignMsg.Time = DateTime.Now; assignUser.SendMsg(assignMsg, false); } }); #endregion sync_flag = MessageSync.WxSyncCheck(); //同步检查 if (sync_flag == null) { continue; } //这里应该判断 sync_flag中selector的值 else //有消息 { sync_result = MessageSync.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(); string MsgId = m["MsgId"].ToString(); WXMsg msg = new WXMsg(); msg.From = from; #region 指定回复某人 this.BeginInvoke((Action) delegate() { //指定回复某人消息 WXMsg assignMsg = new WXMsg(); WXUser assignUser = new WXUser(); if (msg.From == userName) { assignMsg.From = MessageSync.userInfo.UserName; assignMsg.Msg = "指定消息"; assignMsg.Readed = false; assignMsg.To = msg.From; assignMsg.Type = 1; assignMsg.Time = DateTime.Now; assignUser.SendMsg(assignMsg, false); } }); #endregion if (type == "3") { string imgBase64 = MessageSync.Image(MsgId); content = "<msg><img length=\"6503\" src=\"data:image/png;base64," + imgBase64 + "\" hdlength = \"0\" /> <commenturl></commenturl></msg>"; } msg.Msg = (type == "1" || type == "3") ? 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 wChatList.Items) { user = u as WXUser; if (user != null) { if (user.UserName == msg.From && msg.To == MessageSync.userInfo.UserName) //接收别人消息 { wChatList.Items.Remove(user); wChatList.Items.Insert(0, user); exist_latest_contact = true; user.ReceiveMsg(msg); break; } else if (user.UserName == msg.To && msg.From == MessageSync.userInfo.UserName) //同步自己在其他设备上发送的消息 { wChatList.Items.Remove(user); wChatList.Items.Insert(0, user); exist_latest_contact = true; user.SendMsg(msg, true); break; } } } if (!exist_latest_contact) { foreach (object o in wFriendsList.Items) { WXUser friend = o as WXUser; if (friend != null && friend.UserName == msg.From && msg.To == MessageSync.userInfo.UserName) { wChatList.Items.Insert(0, friend); friend.ReceiveMsg(msg); break; } if (friend != null && friend.UserName == msg.To && msg.From == MessageSync.userInfo.UserName) { wChatList.Items.Insert(0, friend); friend.SendMsg(msg, true); break; } } } wChatList.Invalidate(); // System.Threading.Thread.Sleep(5); }); } } } } System.Threading.Thread.Sleep(10); } })).BeginInvoke(null, null); }