Beispiel #1
0
        private async Task GetContact()
        {
            var url = $"{_baseUri}/webwxgetcontact?pass_ticket={_loginXml.PassTicket}&skey={_loginXml.Skey}&r={Common.ConvertDateTimeToInt(DateTime.Now)}";

            var result = await WechatHttp.Post(url, "{}");

            var response = JsonConvert.DeserializeObject <GetContactResponse>(result);

            if (response.MemberCount > 0)
            {
                _contactList = response.MemberList;
            }
        }
Beispiel #2
0
 private async Task SendMessage(string content, string to)
 {
     var url  = $"{_baseUri}/webwxsendmsg?lang=zh_CN&pass_ticket={_loginXml.PassTicket}";
     var time = Common.ConvertDateTimeToInt(DateTime.Now);
     var obj  = new
     {
         BaseRequest = _loginXml.GetBaseRequest(),
         Msg         = new { ClientMsgId = time, Content = content, FromUserName = _me.UserName, LocalID = time, ToUserName = to, Type = 1 },
         Scene       = 0
     };//1 文字消息,3 图片消息(先把图片上传得到MediaId再调用webwxsendmsg发送),其他消息类型没试
     var data   = JsonConvert.SerializeObject(obj);
     var result = await WechatHttp.Post(url, data);
 }
Beispiel #3
0
        private async Task <bool> StatusNotify()
        {
            var url = $"{_baseUri}/webwxstatusnotify?lang=zh_CN&pass_ticket={_loginXml.PassTicket}";

            var obj  = new { BaseRequest = _loginXml.GetBaseRequest(), Code = 3, FromUserName = _me.UserName, ToUserName = _me.UserName, ClientMsgId = Common.ConvertDateTimeToInt(DateTime.Now) };
            var data = JsonConvert.SerializeObject(obj);

            var result = await WechatHttp.Post(url, data);

            var response = JsonConvert.DeserializeObject <StatusNotifyResponse>(result);

            return(response.BaseResponse.Ret == 0);
        }
Beispiel #4
0
        private async Task HandleMessage(SyncResponse rsp)
        {
            foreach (var message in rsp.AddMsgList)
            {
                switch (message.MsgType)
                {
                case 1:     //普通消息
                    var cmds = message.Content.Split(' ').Where(s => !string.IsNullOrEmpty(s)).ToArray();
                    if (cmds.Length == 2)
                    {
                        int type = 1;
                        if (cmds[0] == "监控行业")
                        {
                            type = 2;
                        }
                        else if (cmds[0] == "监控公司")
                        {
                            type = 1;
                        }
                        else
                        {
                            return;
                        }



                        var user = _contactList.FirstOrDefault(p => p.UserName == message.FromUserName);
                        if (user != null)
                        {
                            var result = await WechatHttp.PostAsJson("http://syzb.qianjifang.com.cn/api/Monitor", JsonConvert.SerializeObject(new { type, name = cmds[1], nickName = user.RemarkName ?? user.NickName }));
                            await SendMessage(result, message.FromUserName);
                        }
                    }
                    //await SendMessage("知道了", message.FromUserName);
                    break;

                case 3:     //图片
                case 34:    //语音
                case 37:    //friend request
                case 42:    //名片
                case 47:    //表情
                case 49:    //分享
                case 51:    // 联系人信息
                case 62:    //小视频
                case 10002: // 撤回消息
                default:
                    break;
                }
            }
            //return Task.CompletedTask;
        }
Beispiel #5
0
        private async Task <SyncResponse> Sync()
        {
            var url  = $"{_baseUri}/webwxsync?sid={_loginXml.Sid}&lang=zh_CN&skey={_loginXml.Skey}&pass_ticket={_loginXml.PassTicket}";
            var data = _loginXml.CreateSycnRequestData(_syncKey);

            var result = await WechatHttp.Post(url, data);

            var response = JsonConvert.DeserializeObject <SyncResponse>(result);

            if (response.SyncKey.Count > 0)
            {
                _syncKey = response.SyncKey;
            }

            return(response);
        }
Beispiel #6
0
        /// <summary>
        /// 同步检查
        /// </summary>
        /// <returns></returns>
        private async Task <string[]> SyncCheck()
        {
            var url = $"https://{_syncHost}/cgi-bin/mmwebwx-bin/synccheck?sid={_loginXml.Sid}&uin={_loginXml.Uin}&synckey={_syncKey}&r={Common.ConvertDateTimeToInt(DateTime.Now)}&skey={_loginXml.Skey}&deviceid={_loginXml.DeviceID}&_={Common.ConvertDateTimeToInt(DateTime.Now.ToUniversalTime())}";

            try
            {
                var result = await WechatHttp.Get(url);

                var match = Regex.Match(result, "window.synccheck=\\{retcode:\"(?<retcode>\\d+)\",selector:\"(?<selector>\\d+)\"\\}");
                return(match.Success ? new[] { match.Groups["retcode"].Value, match.Groups["selector"].Value } : new[] { "", "" });
            }
            catch (Exception e)
            {
                Logger.Error(e);
                return(new[] { "-1", "-1" });
            }
        }
Beispiel #7
0
        /// <summary>
        /// 初始化
        /// </summary>
        /// <returns></returns>
        private async Task <bool> Init()
        {
            var url  = $"{_baseUri}/webwxinit?r={Common.ConvertDateTimeToInt(DateTime.Now)}&lang=en_US&pass_ticket={_loginXml.PassTicket}";
            var data = _loginXml.CreateBaseRequestData();

            var returnValue = await WechatHttp.Post(url, data);

            var result = JsonConvert.DeserializeObject <InitResponse>(returnValue);

            _syncKey = result.SyncKey;
            _me      = result.User;
            if (result.Count > 0)
            {
                _contactList = result.ContactList;
            }

            return(result?.BaseResponse?.Ret == 0);
        }
Beispiel #8
0
        /// <summary>
        /// 获取uuid
        /// </summary>
        /// <returns></returns>
        private async Task <bool> GetUUID()
        {
            var url = $"https://login.weixin.qq.com/jslogin?appid=wx782c26e4c19acffb&fun=new&lang=zh_CN&_={Common.ConvertDateTimeToInt(DateTime.Now)}";

            var returnValue = await WechatHttp.Get(url);

            var match = Regex.Match(returnValue, "window.QRLogin.code = (?<code>\\d+); window.QRLogin.uuid = \"(?<uuid>\\S+?)\"");

            if (match.Success)
            {
                var code = match.Groups["code"].Value;
                _uuid = match.Groups["uuid"].Value;

                return(code == "200");
            }
            else
            {
                return(false);
            }
        }
Beispiel #9
0
        /// <summary>
        /// 等待扫描检测
        /// </summary>
        /// <returns></returns>
        private async Task <string> WaitLogin()
        {
            //     http comet:
            //tip=1, 等待用户扫描二维码,
            //       201: scaned
            //       408: timeout
            //tip=0, 等待用户确认登录,
            //       200: confirmed

            var tip       = "1";
            var retryTime = MaxRetryTimes;

            string code = null;

            while (retryTime > 0)
            {
                var url =
                    $"https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?tip={tip}&uuid={_uuid}&_={Common.ConvertDateTimeToInt(DateTime.Now)}";

                var loginResult = await WechatHttp.Get(url);

                var match = Regex.Match(loginResult, "window.code=(?<code>\\d+)");
                if (match.Success)
                {
                    code = match.Groups["code"].Value;
                }

                switch (code)
                {
                case Scaned:
                    Logger.Info("请在手机上确认登录");
                    tip = "0";
                    break;

                case Success:
                    match = Regex.Match(loginResult, "window.redirect_uri=\"(?<redirect_uri>\\S+?)\"");
                    if (match.Success)
                    {
                        var redirectUri = $"{match.Groups["redirect_uri"].Value}&fun=new";
                        _redirectUri = redirectUri;
                        _baseUri     = redirectUri.Substring(0, redirectUri.LastIndexOf('/'));
                        var tempHost = _baseUri.Substring(8);
                        _baseHost = tempHost.Substring(0, tempHost.IndexOf('/'));
                        return(code);
                    }
                    break;

                case Timeout:
                    Logger.Warn("微信登录异常:{0}.{1}秒后重试", code, TryLaterSecs);
                    tip        = "1";
                    retryTime -= 1;
                    Thread.Sleep(TryLaterSecs * 1000);
                    break;

                default:
                    return(null);
                }
                Thread.Sleep(800);
            }

            return(code);
        }