Example #1
0
 private bool _wxSerivice_UpdateMsgToWxUsering(WXUserViewModel user, WXMsg msg)
 {
     if (_chatWindowsDic.ContainsKey(user.UserName))
     {
         msg.Readed = true;
     }
     return(true);
 }
Example #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);
        }
Example #3
0
        private void OnSync(HttpWebRequest req, HttpWebResponse res)
        {
            SyncCheck();

            StreamReader sr      = new StreamReader(res.GetResponseStream());
            string       str_ret = sr.ReadToEnd();

            sr.Close();

            WXSync wxs = (WXSync)Common.DecodeJson(str_ret, typeof(WXSync));

            if (null == wxs ||
                0 != wxs.m_base_response.m_ret)
            {
                MessageBox.Show("同步消息发生错误:" + wxs.m_base_response.m_ret);
                Logout(false);
                return;
            }

            AddLog("已成功获取消息!");

            m_wx_sync_key       = wxs.m_sync_key;
            m_wx_sync_check_key = wxs.m_sync_check_key;

            // 解析信息内容
            lvwContacts.BeginUpdate();

            if (wxs.m_add_msg_count > 0)
            {
                for (int i = 0; i < wxs.m_add_msg_count; i++)
                {
                    //string to_user = wxs.m_add_msg_list[i].m_to_user_name;
                    string from_user = wxs.m_add_msg_list[i].m_from_user_name;
                    if (!lvwContacts.Items.ContainsKey(from_user))
                    {
                        continue;
                    }

                    if (from_user == m_wx_init.m_user.m_user_name) // 自己在其他设备发给他人的信息,同步过来
                    {
                    }

                    if (!m_wx_msg.ContainsKey(from_user))
                    {
                        m_wx_msg.Add(from_user, new List <WXMsg>());
                    }

                    WXMsg msg = wxs.m_add_msg_list[i];
                    m_wx_msg[from_user].Add(msg);

                    string now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ");
                    string from_nick_name = m_wx_names[from_user];
                    string show_title = "", content = "";
                    if (from_user.StartsWith("@@")) // 群组信息
                    {
                        Regex           re_content = new Regex(@"(?<=(@(?:[0-9a-f]{32}|[0-9a-f]{64})):\<br/\>)(.+)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
                        GroupCollection cc         = re_content.Match(msg.m_content).Groups;
                        if (3 != cc.Count)
                        {
                            continue;
                        }

                        string member_user_name = cc[1].Value;
                        string member_name      = m_wx_names[member_user_name];
                        content    = cc[2].Value;
                        show_title = member_name + "(" + from_nick_name + ")" + " 发来";
                    }
                    else
                    {
                        content    = msg.m_content;
                        show_title = from_nick_name + " 发来";
                    }

                    switch (msg.m_msg_type)
                    {
                    case 1:     // 文本信息
                        rtbContent.Select(rtbContent.TextLength, 1);
                        rtbContent.AppendText(now + show_title + "文本信息:\r\n");
                        rtbContent.AppendText(content + "\r\n\r\n");
                        break;

                    case 3:     // 图像
                        GetImage(msg.m_msg_id);

                        rtbContent.Select(rtbContent.TextLength, 1);
                        rtbContent.AppendText(now + show_title + "图片信息:\r\n");

                        System.Diagnostics.Debug.Print(now + show_title);
                        break;

                    case 34:     // 语音
                        GetVoice(msg.m_msg_id);

                        TimeSpan ts_voice  = new TimeSpan(msg.m_voice_length * TimeSpan.TicksPerMillisecond);
                        string   str_voice = "播放语音(" + msg.m_msg_id + ")";
                        rtbContent.Select(rtbContent.TextLength, 1);
                        rtbContent.AppendText(now + show_title + "语音信息:\r\n");
                        rtbContent.AppendText("语音长度:" + ts_voice.ToString(@"mm\:ss\.fff") + "\r\n");
                        rtbContent.AppendText(str_voice + "\r\n\r\n");

                        RichTextBoxLinkCreater.CreateLink(rtbContent, rtbContent.TextLength - str_voice.Length - 2, str_voice.Length);
                        break;

                    case 43:     // 视频
                        GetVideo(msg.m_msg_id);

                        TimeSpan ts_video  = new TimeSpan(msg.m_play_length * TimeSpan.TicksPerSecond);
                        string   str_video = "播放视频(" + msg.m_msg_id + ")";
                        rtbContent.Select(rtbContent.TextLength, 1);
                        rtbContent.AppendText(now + show_title + "视频信息:\r\n");
                        rtbContent.AppendText("视频长度:" + ts_video.ToString(@"hh\:mm\:ss") + "\r\n");
                        rtbContent.AppendText(str_video + "\r\n\r\n");

                        RichTextBoxLinkCreater.CreateLink(rtbContent, rtbContent.TextLength - str_video.Length - 2, str_video.Length);
                        break;

                    case 49:     // 超链接
                        string url = WebUtility.UrlDecode(msg.m_url);

                        rtbContent.Select(rtbContent.TextLength, 1);
                        rtbContent.AppendText(now + show_title + "超链接:\r\n");
                        rtbContent.AppendText("标题:" + WebUtility.UrlDecode(msg.m_encry_file_name) + "\r\n");
                        rtbContent.AppendText("地址:" + url + "\r\n\r\n");

                        RichTextBoxLinkCreater.CreateLink(rtbContent, rtbContent.TextLength - url.Length - 2, url.Length);
                        break;

                    case 10000:     // 红包!!!
                        if ("收到红包,请在手机上查看" == msg.m_content)
                        {
                            niTray.BalloonTipIcon  = ToolTipIcon.Info;
                            niTray.BalloonTipTitle = "¥¥¥收到红包¥¥¥";
                            niTray.BalloonTipText  = show_title + "红包!!!";
                            niTray.ShowBalloonTip(10000);
                        }

                        rtbContent.Select(rtbContent.TextLength, 1);
                        rtbContent.AppendText(now + show_title + "系统信息:\r\n");
                        rtbContent.AppendText(content + "\r\n\r\n");
                        break;

                    default:
                        rtbContent.Select(rtbContent.TextLength, 1);
                        rtbContent.AppendText(now + show_title + "未知信息:\r\n");
                        rtbContent.AppendText(Common.EncodeJson(msg) + "\r\n\r\n");
                        break;
                    }

                    m_sound.Play();
                    rtbContent.Select(rtbContent.TextLength, 0);
                }
            }

            if (wxs.m_mod_contact_count > 0)
            {
                GetContacts(0);
            }

            lvwContacts.EndUpdate();
        }