Exemplo n.º 1
0
 /// <summary>
 /// 微信同步(获取最新消息)
 /// </summary>
 /// <returns></returns>
 public List <object> WxSync()
 {
     try
     {
         WXResult      wxResult    = new WXResult();
         JObject       sync_result = MessageSync.WxSync(); //同步检查
         List <object> msgList     = new List <object>();
         if (sync_result != null)
         {
             if (sync_result["AddMsgCount"] != null && sync_result["AddMsgCount"].ToString() != "0")
             {
                 foreach (JObject m in sync_result["AddMsgList"])
                 {
                     WXMsg msg = new WXMsg();
                     msg.From = m["FromUserName"].ToString();
                     msg.To   = m["ToUserName"].ToString();
                     msg.Msg  = m["Content"].ToString();
                     msg.Type = int.Parse(m["MsgType"].ToString());
                     msgList.Add(msg);
                 }
                 return(msgList);
             }
         }
         return(null);
     }
     catch (Exception ex)
     {
         return(null);
     }
 }
Exemplo n.º 2
0
        /// <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);
        }