Example #1
0
        /// <summary>
        /// 获取已关注用户列表
        /// </summary>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        internal static List <string> GetSubscribeUserList(string accessToken)
        {
            var result      = new List <string>();
            var next_openid = string.Empty;
            var api         = "https://api.weixin.qq.com/cgi-bin/user/get";
            var total       = 0;
            var readCount   = 0;

            do
            {
                var url = string.Empty;
                if (string.IsNullOrEmpty(next_openid))
                {
                    url = string.Format("{0}?access_token={1}", api, accessToken);
                }
                else
                {
                    url = string.Format("{0}?access_token={1}&next_openid={2}", api, accessToken, next_openid);
                }
                var json = HttpRequestHelper.GetHttp_ForamtByJson(url);
                Log.Debug("\r\n获取已关注列表 json返回值:>>\r\n{0}", json);
                var returnCode = GlobalReturnCode.GetReturnCode(json);
                if (returnCode.IsRequestSuccess)
                {
                    var jsonObj = JsonSerializerHelper.Deserialize(json);
                    total = Convert.ToInt32(jsonObj["total"]);
                    var count = Convert.ToInt32(jsonObj["count"]);
                    next_openid = Convert.ToString(jsonObj["next_openid"]);
                    readCount  += count;
                    if (count > 0)
                    {
                        var openids = (jsonObj["data"] as Dictionary <string, object>)["openid"];
                        if (openids is ArrayList)
                        {
                            foreach (var item in openids as ArrayList)
                            {
                                result.Add(item.ToString());
                            }
                        }
                        else
                        {
                            Log.Warning("获取已关注列表转换 openid 类型失败,实际类型:" + openids.ToString());
                        }
                    }
                }
                else
                {
                    throw new WeixinRequestApiException(string.Format("获取已关注列表失败\r\n全局返回值:{0}\r\n对应说明:{1}\r\nJson:{2}\r\n请求路径:{3}", returnCode.ErrCode, returnCode.Msg, returnCode.Json, url), returnCode);
                }
            } while (total > readCount);
            return(result);
        }
Example #2
0
        internal static void SendCustomerMessage(string accessToken, CustomerJsonMessage msg)
        {
            var    json = msg.GetJson();
            string url  = string.Format("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={0}", accessToken);

            Log.Debug("\r\n发送客服消息 json 数据:>>\r\n{0}", json);
            var result = HttpRequestHelper.PostHttp_ForamtByJson(url, json);

            Log.Debug("\r\n发送客服消息 json 返回值:>>\r\n{0}", result);
            var returnCode = GlobalReturnCode.GetReturnCode(json);

            if (!returnCode.IsRequestSuccess)
            {
                throw new WeixinRequestApiException(string.Format("获取 access_token 失败\r\n全局返回值:{0}\r\n对应说明:{1}\r\nJson:{2}\r\n请求路径:{3}", returnCode.ErrCode, returnCode.Msg, returnCode.Json, url), returnCode);
            }
        }
Example #3
0
        private static Dictionary <string, object> GetAccessTokenDic(string appId, string _AppSecret)
        {
            var url  = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", appId, _AppSecret);
            var json = HttpRequestHelper.GetHttp_ForamtByJson(url);

            Log.Debug("\r\n获取 access_token json返回值:>>\r\n{0}", json);
            var returnCode = GlobalReturnCode.GetReturnCode(json);

            if (!returnCode.IsRequestSuccess)
            {
                throw new WeixinRequestApiException(string.Format("获取 access_token 失败\r\n全局返回值:{0}\r\n对应说明:{1}\r\nJson:{2}\r\n请求路径:{3}", returnCode.ErrCode, returnCode.Msg, returnCode.Json, url), returnCode);
            }
            var jsonObj = JsonSerializerHelper.Deserialize(json);

            return(jsonObj);
        }
Example #4
0
        internal static OAuth2AccessToken GetOAuthAccessToken(string code, string appId, string appSecret)
        {
            var url  = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", appId, appSecret, code);
            var json = HttpRequestHelper.GetHttp_ForamtByJson(url);

            Log.Debug("\r\n获取 OAuth2.0 access_token json返回值:>>\r\n{0}", json);
            var returnCode = GlobalReturnCode.GetReturnCode(json);

            if (!returnCode.IsRequestSuccess)
            {
                throw new WeixinRequestApiException(string.Format("获取 OAuth2.0 access_token 失败\r\n全局返回值:{0}\r\n对应说明:{1}\r\nJson:{2}\r\n请求路径:{3}", returnCode.ErrCode, returnCode.Msg, returnCode.Json, url), returnCode);
            }
            var oAuth2AccessToken = JsonSerializerHelper.ConvertJsonStringToObjectByJsonPropertyAttribute <OAuth2AccessToken>(json);

            return(oAuth2AccessToken);
        }
Example #5
0
        internal static OAuth2UserInfo GetUserInfo(string openId, string oAuthAccessToken)
        {
            var url  = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN", oAuthAccessToken, openId);
            var json = HttpRequestHelper.GetHttp_ForamtByJson(url);

            Log.Debug("\r\n获取用户信息 json返回值:>>\r\n{0}", json);
            var returnCode = GlobalReturnCode.GetReturnCode(json);

            if (!returnCode.IsRequestSuccess)
            {
                throw new WeixinRequestApiException(string.Format("获取用户信息失败\r\n全局返回值:{0}\r\n对应说明:{1}\r\nJson:{2}\r\n请求路径:{3}", returnCode.ErrCode, returnCode.Msg, returnCode.Json, url), returnCode);
            }
            var userInfo = JsonSerializerHelper.ConvertJsonStringToObjectByJsonPropertyAttribute <OAuth2UserInfo>(json);

            return(userInfo);
        }
Example #6
0
        private static QRCodeInfo CreateQR(object obj, string accessToken)
        {
            var url  = string.Format("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={0}", accessToken);
            var json = JsonSerializerHelper.Serialize(obj);

            Log.Debug("\r\n创建参数二维码 json数据:>>\r\n{0}", json);
            var resultJson = HttpRequestHelper.PostHttp_ForamtByJson(url, json);

            Log.Debug("\r\n创建参数二维码 json返回值:>>\r\n{0}", resultJson);
            var returnCode = GlobalReturnCode.GetReturnCode(resultJson);

            if (!returnCode.IsRequestSuccess)
            {
                throw new WeixinRequestApiException(string.Format("创建二维码失败\r\n全局返回值:{0}\r\n对应说明:{1}\r\nJson:{2}\r\n请求路径:{3}", returnCode.ErrCode, returnCode.Msg, returnCode.Json, url), returnCode);
            }
            var qrCodeInfo = JsonSerializerHelper.ConvertJsonStringToObjectByJsonPropertyAttribute <QRCodeInfo>(resultJson);

            qrCodeInfo.ImgUrl = string.Format("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket={0}", HttpUtility.UrlEncode(qrCodeInfo.Ticket));
            return(qrCodeInfo);
        }