예제 #1
0
        /// <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);
        }
예제 #2
0
        /// <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);
        }