Exemplo n.º 1
0
        /// <summary>
        /// 获取联系人信息,例如初始化、群聊里面。
        /// </summary>
        /// <param name="statusNotifyUserName">需要获取的UserName列表,包括群,个人用户,用英文,分割</param>
        /// <param name="EncryChatRoomId">默认为空,如果是获取群内成员详细信息,则填写encryChatRoomId,也就是群的UserName</param>
        public void GetBatchGetContactAsync(string statusNotifyUserName, string encryChatRoomId = "")
        {
            Task.Factory.StartNew(() =>
            {
                try
                {
                    //获取历史会话列表
                    string webwxbatchgetcontactUrl = string.Format(host + "/cgi-bin/mmwebwx-bin/webwxbatchgetcontact?type=ex&r={0}", Utils.GetJavaTimeStamp());
                    string[] chatNameArr           = statusNotifyUserName.Split(',');
                    bool finishGetChatList         = false;
                    BatchGetContactRequest batchGetContactRequest = new BatchGetContactRequest();
                    batchGetContactRequest.BaseRequest            = baseRequest;
                    int count = chatNameArr.Length;
                    int index = 0;
                    //一批次最多获取50条,多出来分批获取
                    while (!finishGetChatList)
                    {
                        batchGetContactRequest.List = new List <ChatRoom>();
                        if (((index + 1) * 50) < count)
                        {
                            for (int i = index * 50; i < (index + 1) * 50; i++)
                            {
                                batchGetContactRequest.List.Add(new ChatRoom {
                                    UserName = chatNameArr[i], EncryChatRoomId = encryChatRoomId
                                });
                            }
                        }
                        else
                        {
                            for (int i = index * 50; i < count; i++)
                            {
                                batchGetContactRequest.List.Add(new ChatRoom {
                                    UserName = chatNameArr[i], EncryChatRoomId = encryChatRoomId
                                });
                            }
                            finishGetChatList = true;
                        }
                        BatchGetContactResponse batchGetContactMsg = httpClient.PostJson <BatchGetContactResponse>(webwxbatchgetcontactUrl, batchGetContactRequest);
                        asyncOperation.Post(
                            new SendOrPostCallback((list) =>
                        {
                            BatchGetContactComplete?.Invoke(this, new TEventArgs <List <Contact> >((List <Contact>)list));
                        }), batchGetContactMsg.ContactList);

                        index++;
                    }
                }
                catch (Exception e)
                {
                    asyncOperation.Post(
                        new SendOrPostCallback((obj) =>
                    {
                        ExceptionCatched?.Invoke(this, new TEventArgs <Exception>((Exception)obj));
                    }), e);
                }
            });
        }
Exemplo n.º 2
0
        /// <summary>
        /// 开始初始化所有关键内容
        /// </summary>
        private void Init()
        {
            try
            {
                string  webwxinitUrl = string.Format(host + "/cgi-bin/mmwebwx-bin/webwxinit?r={0}pass_ticket={1}", Utils.Get_r(), passTicket);
                JObject postjson     = JObject.FromObject(new
                {
                    BaseRequest = baseRequest
                });
                InitResponse initMsg = httpClient.PostJson <InitResponse>(webwxinitUrl, postjson);
                if (initMsg.BaseResponse.Ret != 0)
                {
                    throw new Exception("程序初始化失败");
                }
                //初始化2次,官网也是初始化2次,这样貌似比较稳定
                httpClient.PostJson <InitResponse>(webwxinitUrl, postjson);
                user = initMsg.User;
                mpSubscribeMsgList = initMsg.MPSubscribeMsgList;
                syncKey            = initMsg.SyncKey;

                //初始化的时候会返回一个最近联系人列表,但是主要还是以第一次sync获得的最近联系人为准。
                asyncOperation.Post(
                    new SendOrPostCallback((list) =>
                {
                    BatchGetContactComplete?.Invoke(this, new TEventArgs <List <Contact> >((List <Contact>)list));
                }), initMsg.ContactList);

                asyncOperation.Post(
                    new SendOrPostCallback((obj) =>
                {
                    LoginComplete?.Invoke(this, new TEventArgs <User>((User)obj));
                }), user);
            }
            catch (Exception ex)
            {
                FileLog.Exception("Init", ex);
                asyncOperation.Post(
                    new SendOrPostCallback((obj) =>
                {
                    ExceptionCatched?.Invoke(this, new TEventArgs <Exception>((Exception)obj));
                }), ex);
                //throw ex;
            }
        }