예제 #1
0
        /// <summary>
        /// 获取用户相关Keys
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static PassTicketXmlInfo GetUserKeys(string url, ref string cookie)
        {
            var httpreuest  = new HttpHelper();
            var responseXml = httpreuest.HttpGet(url);

            if (!String.IsNullOrEmpty(responseXml))
            {
                //返回Cookie
                cookie = httpreuest.ReturnCookie;
                //解析返回值
                XmlDocument xdoc = new XmlDocument();
                xdoc.LoadXml(responseXml);
                var ret     = Convert.ToInt32(xdoc.SelectSingleNode("//ret").InnerText);
                var message = xdoc.SelectSingleNode("//message").InnerText;
                if (ret != 0)
                {
                    ///限制登陆
                    throw new CustomerException(message, 500);
                }
                PassTicketXmlInfo Keys = new PassTicketXmlInfo
                {
                    ret         = ret,
                    message     = message,
                    skey        = xdoc.SelectSingleNode("//skey").InnerText,
                    wxsid       = xdoc.SelectSingleNode("//wxsid").InnerText,
                    wxuin       = xdoc.SelectSingleNode("//wxuin").InnerText,
                    pass_ticket = xdoc.SelectSingleNode("//pass_ticket").InnerText,
                    isgrayscale = xdoc.SelectSingleNode("//isgrayscale").InnerText
                };//返回对象
                return(Keys);
            }
            return(null);
        }
예제 #2
0
        /// <summary>
        /// 存储用户操作秘钥
        /// </summary>
        /// <param name="redisHelper"></param>
        /// <param name="userName"></param>
        /// <param name="keys"></param>
        public void SetUserKeys(string wxuin, PassTicketXmlInfo keys)
        {
            var    tablename = userRedisKey(wxuin);
            string keysJson  = (JsonConvert.SerializeObject(keys));

            _redisHelper.SetHash(tablename, _keys_redis_key_, keysJson);
        }
예제 #3
0
        public static bool GetWxStatus(string wxHosts, string wxCookie, PassTicketXmlInfo keys, SyncKey syncKey)
        {
            var param = new
            {
                r        = TimeHelper.GetTimeStamp(),
                skey     = keys.skey,
                sid      = keys.wxsid,
                uin      = keys.wxuin,
                deviceid = GetWxDeviceId(),
                synckey  = String.Join("|", syncKey.List.Select(m => String.Format("{0}_{1}", m.Key, m.Val)))
            };
            var result     = new HttpHelper(param, GetWxCookie(wxCookie)).HttpGet(WxStatus(wxHosts));
            var statusJson = result.Replace("window.synccheck=", "");
            var status     = JsonConvert.DeserializeObject <WxStatus>(statusJson);

            return(status.RetCode.Equals("0"));
        }
예제 #4
0
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="wxHosts"></param>
        /// <param name="passTicket"></param>
        /// <param name="wxSid"></param>
        /// <param name="skey"></param>
        /// <param name="wxUin"></param>
        /// <returns></returns>
        public static WxUserInitInfo GetWxUserInfo(string wxHosts, PassTicketXmlInfo xml)
        {
            var param = new
            {
                BaseRequest = new
                {
                    DeviceID = GetWxDeviceId(),
                    Sid      = xml.wxsid,
                    Skey     = xml.skey,
                    Uin      = xml.wxuin
                }
            };
            var userJson  = new HttpHelper(param).HttpPost(InitUrl(wxHosts, xml.pass_ticket));
            var userModel = JsonConvert.DeserializeObject <WxUserInitInfo>(userJson);

            return(userModel);
        }
예제 #5
0
        /// <summary>
        /// 获取新消息,延长Cookie时间
        /// </summary>
        /// <param name="wxHosts"></param>
        /// <param name="wxCookie"></param>
        /// <param name="keys"></param>
        /// <param name="syncKey"></param>
        public static string GetNews(string wxHosts, string wxCookie, PassTicketXmlInfo keys, SyncKey syncKey)
        {
            var param = new
            {
                BaseRequest = new
                {
                    skey     = keys.skey,
                    sid      = keys.wxsid,
                    uin      = keys.wxuin,
                    deviceid = GetWxDeviceId(),
                },
                SyncKey = syncKey
            };
            var httpHelper = new HttpHelper(param, GetWxCookie(wxCookie));
            var result     = httpHelper.HttpPost(WxNews(wxHosts, keys.wxsid, keys.skey));
            var setcookie  = httpHelper.ReturnCookie;

            return(result);
        }
예제 #6
0
        public static bool SetRemarkName(string wxHosts, string wxCookie, PassTicketXmlInfo keys, string userName, string remarkName)
        {
            var param = new
            {
                BaseRequest = new
                {
                    Uin      = keys.wxuin,
                    Sid      = keys.wxsid,
                    Skey     = keys.skey,
                    DeviceID = GetWxDeviceId(),
                },
                CmdId      = 2,
                RemarkName = remarkName,
                UserName   = userName
            };
            var responseJson  = new HttpHelper(param, GetWxCookie(wxCookie)).HttpPost(WxSetRemarkName(wxHosts, keys.pass_ticket));
            var responseModel = JsonConvert.DeserializeObject <dynamic>(responseJson);

            return(responseModel.BaseResponse.Ret == 0);
        }
예제 #7
0
 /// <summary>
 /// 开启微信状态通知
 /// </summary>
 public static void StartWxStatusNotify(string wxHosts, string wxCookie, PassTicketXmlInfo keys, string userName)
 {
     var param = new
     {
         BaseRequest = new
         {
             DeviceID = GetWxDeviceId(),
             Sid      = keys.wxsid,
             Skey     = keys.skey,
             Uin      = keys.wxuin
         },
         Code         = 3,
         FromUserName = userName,
         ToUserName   = userName,
         ClientMsgId  = TimeHelper.GetTimeStamp()
     };
     var result = new HttpHelper(param, GetWxCookie(wxCookie)).HttpPost(WxStatusNotify(wxHosts));
     //TODO 检测开启状态
     //if()
 }
예제 #8
0
파일: WxUser.cs 프로젝트: ks3dev/WechatApi
        public static WxUsers SetWxUserInfo(IWxUsersRepository repository, WxUserInfo userModel, PassTicketXmlInfo keys)
        {
            var result = repository.GetWxUsers(keys.wxuin) ?? new WxUsers();

            result.Uin        = keys.wxuin;
            result.UserName   = userModel.UserName;
            result.NickName   = userModel.NickName;
            result.HeadImgUrl = userModel.HeadImgUrl;
            result.PYInitial  = userModel.PYInitial;
            result.PYQuanPin  = userModel.PYQuanPin;
            result.StarFriend = userModel.StarFriend;
            result.Sex        = userModel.Sex;
            result.UpdateTime = DateTime.Now;

            //设置keys
            result.Detail.WxKeys.Ret         = keys.ret;
            result.Detail.WxKeys.Message     = keys.message;
            result.Detail.WxKeys.Skey        = keys.skey;
            result.Detail.WxKeys.WxSid       = keys.wxsid;
            result.Detail.WxKeys.WxUin       = keys.wxuin;
            result.Detail.WxKeys.PassTicket  = keys.pass_ticket;
            result.Detail.WxKeys.Isgrayscale = keys.isgrayscale;
            result.Detail.WxKeysJson         = JsonConvert.SerializeObject(result.Detail.WxKeys);

            if (result.Id <= 0)
            {
                repository.Add(result);
            }
            else
            {
                repository.Update(result);
            }
            return(result);
        }
예제 #9
0
        /// <summary>
        /// SendMsg
        /// </summary>
        /// <param name="wxHosts">微信域名</param>
        /// <param name="msg">发送信息 TODO 处理信息类型</param>
        /// <param name="toUserName">接收人</param>
        /// <param name="userName">发送人</param>
        /// <param name="wxUin">Uin</param>
        /// <param name="wxSid">Sid</param>
        /// <param name="skey">Skey</param>
        /// <returns></returns>
        public static bool SendMsg(string wxHosts, string msg, string toUserName, string userName, PassTicketXmlInfo keys, string wxCookie)
        {
            var param = new
            {
                BaseRequest = new
                {
                    Uin      = keys.wxuin,
                    Sid      = keys.wxsid,
                    Skey     = keys.skey,
                    DeviceID = GetWxDeviceId(),
                },
                Msg = new
                {
                    Type         = 1,
                    Content      = msg,
                    FromUserName = userName,
                    ToUserName   = toUserName,
                    LocalID      = TimeHelper.GetTimeStamp(),
                    ClientMsgId  = TimeHelper.GetTimeStamp()
                }
            };
            var responseJson  = new HttpHelper(param, GetWxCookie(wxCookie)).HttpPost(WxSendMsgUrl(wxHosts));
            var responseModel = JsonConvert.DeserializeObject <dynamic>(responseJson);

            return(responseModel.BaseResponse.Ret == 0);
        }
예제 #10
0
        public static bool CheckStatus(HttpResponse httpResponse, string wxHosts, string wxCookie, PassTicketXmlInfo keys, SyncKey syncKey)
        {
            var status = WeiXinHelper.GetWxStatus(wxHosts, wxCookie, keys, syncKey);

            if (!status)
            {
                AuthCore.DeleteUin(httpResponse);
                throw new CustomerException("登录过期,请重新登录", -2);
            }
            return(status);
        }
예제 #11
0
        /// <summary>
        /// 设置备注名称
        /// </summary>
        /// <param name="repository"></param>
        /// <param name="wxHosts"></param>
        /// <param name="wxCookie"></param>
        /// <param name="keys"></param>
        /// <param name="userName"></param>
        /// <param name="remarkName"></param>
        /// <returns></returns>
        public static bool SetRemarkName(IWxFriendsRepository repository, string wxHosts, string wxCookie, PassTicketXmlInfo keys, string userName, string remarkName)
        {
            var status = WeiXinHelper.SetRemarkName(wxHosts, wxCookie, keys, userName, remarkName);

            if (status)
            {
                var users = repository.GetByName(keys.wxuin, userName);
                if (users == null)
                {
                    throw new CustomerException("本地同步失败", -1);
                }
                users.SetRemarkName(repository, remarkName);
            }
            return(status);
        }
예제 #12
0
        /// <summary>
        /// 心跳检测微信状态
        /// </summary>
        public static void HeartbeatWxStatus(HttpResponse httpResponse, string wxHosts, string wxCookie, PassTicketXmlInfo keys, SyncKey syncKey)
        {
            var status = WeiXinHelper.GetWxStatus(wxHosts, wxCookie, keys, syncKey);
            var wxnews = WeiXinHelper.GetNews(wxHosts, wxCookie, keys, syncKey);

            if (status)
            {
                Thread.Sleep(20000);
                HeartbeatWxStatus(httpResponse, wxHosts, wxCookie, keys, syncKey);
            }
            else
            {
                throw new CustomerException(String.Format("当前登录状态消失 时间:", DateTime.Now), -2);
            }
        }