Esempio n. 1
0
        /// <summary>
        /// 查询所有分组
        /// </summary>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        public static GroupsJson Groups_GetAll(string accessToken)
        {
            var urlFormat = "https://api.weixin.qq.com/cgi-bin/groups/get?access_token={0}";
            var url       = string.Format(urlFormat, accessToken);

            return(HTTPGet.GetJson <GroupsJson>(url));
        }
        /// <summary>
        /// 向需要AccessToken的API发送消息的公共方法
        /// </summary>
        /// <param name="accessToken">这里的AccessToken是通用接口的AccessToken,非OAuth的。如果不需要,可以为null,此时urlFormat不要提供{0}参数</param>
        /// <param name="urlFormat">用accessToken参数填充{0}</param>
        /// <param name="data">如果是Get方式,可以为null</param>
        /// <param name="sendType"></param>
        /// <param name="timeOut">代理请求超时时间(毫秒)</param>
        /// <param name="checkValidationResult"></param>
        /// <param name="jsonSetting"></param>
        /// <returns></returns>
        public static T Send <T>(string accessToken, string urlFormat, object data, CommonJsonSendType sendType = CommonJsonSendType.POST, int timeOut = Config.TIME_OUT, bool checkValidationResult = false, JsonSetting jsonSetting = null)
        {
            //TODO:此方法可以设定一个日志记录开关

            try
            {
                var url = string.IsNullOrEmpty(accessToken) ? urlFormat : string.Format(urlFormat, accessToken);
                switch (sendType)
                {
                case CommonJsonSendType.GET:
                    return(HTTPGet.GetJson <T>(url));

                case CommonJsonSendType.POST:
                    SerializerHelper serializerHelper = new SerializerHelper();
                    var jsonString = serializerHelper.GetJsonString(data, jsonSetting);
                    using (MemoryStream ms = new MemoryStream())
                    {
                        var bytes = Encoding.UTF8.GetBytes(jsonString);
                        ms.Write(bytes, 0, bytes.Length);
                        ms.Seek(0, SeekOrigin.Begin);

                        return(HTTPPost.PostGetJson <T>(url, null, ms, timeOut: timeOut, checkValidationResult: checkValidationResult));
                    }

                //TODO:对于特定的错误类型自动进行一次重试,如40001(目前的问题是同样40001会出现在不同的情况下面)
                default:
                    throw new ArgumentOutOfRangeException("sendType");
                }
            }
            catch (ErrorJsonResultException ex)
            {
                ex.Url = urlFormat;
                throw;
            }
        }
        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        public static WxJsonResult DeleteMenu(string accessToken)
        {
            var url    = string.Format("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token={0}", accessToken);
            var result = HTTPGet.GetJson <WxJsonResult>(url);

            return(result);
        }
Esempio n. 4
0
        /// <summary>
        /// 用户信息接口
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="openId"></param>
        /// <returns></returns>
        public static WeixinUserInfoResult GetUserInfo(string accessToken, string openId)
        {
            var url = string.Format("http://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}",
                                    accessToken, openId);
            WeixinUserInfoResult result = HTTPGet.GetJson <WeixinUserInfoResult>(url);

            return(result);
        }
Esempio n. 5
0
        /// <summary>
        /// 获取凭证接口
        /// </summary>
        /// <param name="grant_type">获取access_token填写client_credential</param>
        /// <param name="appid">第三方用户唯一凭证</param>
        /// <param name="secret">第三方用户唯一凭证密钥,既appsecret</param>
        /// <returns></returns>
        public static AccessTokenResult GetToken(string appid, string secret, string grant_type = "client_credential")
        {
            var url = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type={0}&appid={1}&secret={2}",
                                    grant_type, appid, secret);
            AccessTokenResult result = HTTPGet.GetJson <AccessTokenResult>(url);

            return(result);
        }
Esempio n. 6
0
        /// <summary>
        /// 获取用户基本信息(包括UnionID机制)
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="openId">普通用户的标识,对当前公众号唯一</param>
        /// <param name="lang">zh_CN 简体,zh_TW 繁体,en 英语</param>
        /// <returns></returns>
        public static UserInfoJson User_Info(string accessToken, string openId, Language lang = Language.zh_CN)
        {
            string url = string.Format("https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}&lang={2}",
                                       accessToken, openId, lang.ToString());

            return(HTTPGet.GetJson <UserInfoJson>(url));

            //错误时微信会返回错误码等信息,JSON数据包示例如下(该示例为AppID无效错误):
            //{"errcode":40013,"errmsg":"invalid appid"}
        }
Esempio n. 7
0
        /// <summary>
        /// 文档:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1421823488&token=&lang=zh_CN
        /// 获取调用微信JS接口的临时票据 OPEN
        /// </summary>
        /// <param name="authorizerAccessToken">authorizer_access_token</param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static JsApiTicketResult GetJsApiTicket(string authorizerAccessToken, string type = "jsapi")
        {
            //获取第三方平台的授权公众号token(公众号授权给第三方平台后,第三方平台通过“接口说明”中的api_authorizer_token接口得到的token)
            var url = string.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type={1}",
                                    authorizerAccessToken, type);

            JsApiTicketResult result = HTTPGet.GetJson <JsApiTicketResult>(url);

            return(result);
        }
Esempio n. 8
0
        private static OpenIdResultJson User_Get(string accessToken, string nextOpenId)
        {
            string url = string.Format("https://api.weixin.qq.com/cgi-bin/user/get?access_token={0}",
                                       accessToken);

            if (!string.IsNullOrEmpty(nextOpenId))
            {
                url += "&next_openid=" + nextOpenId;
            }
            return(HTTPGet.GetJson <OpenIdResultJson>(url));
        }
Esempio n. 9
0
        /// <summary>
        /// 获取素材总数
        /// 永久素材的总数,也会计算公众平台官网素材管理中的素材
        /// 图片和图文消息素材(包括单图文和多图文)的总数上限为5000,其他素材的总数上限为1000
        /// </summary>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        public static GetMediaCountResultJson GetMediaCount(string accessToken)
        {
            string url = string.Format("https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token={0}", accessToken);

            return(HTTPGet.GetJson <GetMediaCountResultJson>(url));
        }