/// <summary>
        /// 根据OpenId列表群发消息
        /// </summary>
        /// <param name="userName">公众号</param>
        /// <param name="tousers">OpenId列表</param>
        /// <param name="messageType">群发消息类型</param>
        /// <param name="mediaIdOrContent">多媒体id或者文本内容</param>
        /// <param name="errorMessage">返回发送是否成功</param>
        /// <returns>如果发送成功,返回消息ID;否则,返回-1。</returns>
        public static long Send(string userName, IEnumerable <string> tousers, MassMessageTypeEnum messageType, string mediaIdOrContent, out ErrorMessage errorMessage)
        {
            long messageId = -1;

            if (tousers == null)
            {
                errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, string.Format("接收者不正确,数目必须大于0,小于等于{0}。", maxUserCount));
                return(messageId);
            }
            List <string> touserList = new List <string>(tousers);

            if (touserList.Count == 0 || touserList.Count > maxUserCount)
            {
                errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, string.Format("接收者不正确,数目必须大于0,小于等于{0}。", maxUserCount));
                return(messageId);
            }
            if (string.IsNullOrWhiteSpace(mediaIdOrContent))
            {
                errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode,
                                                string.Format("缺少{0}。", messageType == MassMessageTypeEnum.text ? "文本内容" : "媒体ID"));
                return(messageId);
            }
            string json = GetMassMessageJsonString(touserList.ToArray(), messageType, mediaIdOrContent);

            return(Send(userName, urlForSending, json, out errorMessage));
        }
        /// <summary>
        /// 预览群发消息
        /// </summary>
        /// <param name="userName">公众号</param>
        /// <param name="touser">OpenId</param>
        /// <param name="messageType">群发消息类型</param>
        /// <param name="mediaIdOrContent">多媒体id或者文本内容</param>
        /// <param name="errorMessage">返回发送是否成功</param>
        /// <returns>如果发送成功,返回消息ID;否则,返回-1。</returns>
        public static long Preview(string userName, string touser, MassMessageTypeEnum messageType, string mediaIdOrContent, out ErrorMessage errorMessage)
        {
            long messageId = -1;

            if (string.IsNullOrWhiteSpace(mediaIdOrContent))
            {
                errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode,
                                                string.Format("缺少{0}。", messageType == MassMessageTypeEnum.text ? "文本内容" : "媒体ID"));
                return(messageId);
            }
            string json = GetMassMessageJsonString(touser, messageType, mediaIdOrContent);

            return(Send(userName, urlForPreviewing, json, out errorMessage));
        }
        /// <summary>
        /// 根据分组群发消息
        /// </summary>
        /// <param name="userName">公众号</param>
        /// <param name="isToAll">是否群发给所有用户</param>
        /// <param name="groupId">如果群发给所有用户,忽略该参数;否则群发给该组中的用户</param>
        /// <param name="messageType">群发消息类型</param>
        /// <param name="mediaIdOrContent">多媒体id或者文本内容</param>
        /// <param name="errorMessage">返回发送是否成功</param>
        /// <returns>如果发送成功,返回消息ID;否则,返回-1。</returns>
        public static long Send(string userName, bool isToAll, string groupId, MassMessageTypeEnum messageType, string mediaIdOrContent, out ErrorMessage errorMessage)
        {
            long messageId = -1;

            if (!isToAll && string.IsNullOrWhiteSpace(groupId))
            {
                errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "缺少分组ID。");
                return(messageId);
            }
            if (string.IsNullOrWhiteSpace(mediaIdOrContent))
            {
                errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode,
                                                string.Format("缺少{0}。", messageType == MassMessageTypeEnum.text ? "文本内容" : "媒体ID"));
                return(messageId);
            }
            string json = GetMassMessageJsonString(isToAll, groupId, messageType, mediaIdOrContent);

            return(Send(userName, urlForSendingAll, json, out errorMessage));
        }
Exemplo n.º 4
0
 /// <summary>
 /// 获取消息发送分布月数据
 /// </summary>
 /// <param name="userName">公众号</param>
 /// <param name="beginDate">起始日期</param>
 /// <param name="endDate">结束日期</param>
 /// <param name="errorMessage">返回获取是否成功</param>
 /// <returns>返回消息发送分布月数据</returns>
 public static UpstreamMsgDist[] GetUpstreamMsgDistMonth(string userName, DateTime beginDate, DateTime endDate, out ErrorMessage errorMessage)
 {
     return(Get <UpstreamMsgDist>(userName, beginDate, endDate, urlForGettingUpstreamMsgDistMonth, timeSpanForGettingUpstreamMsgDistMonth, out errorMessage));
 }
Exemplo n.º 5
0
 /// <summary>
 /// 获取接口分析分时数据
 /// </summary>
 /// <param name="userName">公众号</param>
 /// <param name="beginDate">起始日期</param>
 /// <param name="endDate">结束日期</param>
 /// <param name="errorMessage">返回获取是否成功</param>
 /// <returns>返回接口分析分时数据</returns>
 public static InterfaceSummaryHour[] GetInterfaceSummaryHour(string userName, DateTime beginDate, DateTime endDate, out ErrorMessage errorMessage)
 {
     return(Get <InterfaceSummaryHour>(userName, beginDate, endDate, urlForGettingInterfaceSummaryHour, timeSpanForGettingInterfaceSummaryHour, out errorMessage));
 }
Exemplo n.º 6
0
 /// <summary>
 /// 获取图文统计数据
 /// </summary>
 /// <param name="userName">公众号</param>
 /// <param name="beginDate">起始日期</param>
 /// <param name="endDate">结束日期</param>
 /// <param name="errorMessage">返回获取是否成功</param>
 /// <returns>返回图文统计数据</returns>
 public static UserRead[] GetUserRead(string userName, DateTime beginDate, DateTime endDate, out ErrorMessage errorMessage)
 {
     return(Get <UserRead>(userName, beginDate, endDate, urlForGettingUserRead, timeSpanForGettingUserRead, out errorMessage));
 }
Exemplo n.º 7
0
 /// <summary>
 /// 获取图文分享转发分时数据
 /// </summary>
 /// <param name="userName">公众号</param>
 /// <param name="beginDate">起始日期</param>
 /// <param name="endDate">结束日期</param>
 /// <param name="errorMessage">返回获取是否成功</param>
 /// <returns>返回图文分享转发分时数据</returns>
 public static UserShareHour[] GetUserShareHour(string userName, DateTime beginDate, DateTime endDate, out ErrorMessage errorMessage)
 {
     return(Get <UserShareHour>(userName, beginDate, endDate, urlForGettingUserShareHour, timeSpanForGettingUserShareHour, out errorMessage));
 }
Exemplo n.º 8
0
 /// <summary>
 /// 获取图文群发总数据
 /// </summary>
 /// <param name="userName">公众号</param>
 /// <param name="beginDate">起始日期</param>
 /// <param name="endDate">结束日期</param>
 /// <param name="errorMessage">返回获取是否成功</param>
 /// <returns>返回图文群发总数据</returns>
 public static ArticleTotal[] GetArticleTotal(string userName, DateTime beginDate, DateTime endDate, out ErrorMessage errorMessage)
 {
     return(Get <ArticleTotal>(userName, beginDate, endDate, urlForGettingArticleTotal, timeSpanForGettingArticleTotal, out errorMessage));
 }
Exemplo n.º 9
0
        /// <summary>
        /// 获取统计数据
        /// </summary>
        /// <param name="userName">公众号</param>
        /// <param name="beginDate">起始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <param name="url">获取统计数据的地址</param>
        /// <param name="timeSpan">最大时间跨度</param>
        /// <param name="errorMessage">返回获取是否成功</param>
        /// <returns>返回包含统计数据的JArray对象</returns>
        private static T[] Get <T>(string userName, DateTime beginDate, DateTime endDate, string url, int timeSpan, out ErrorMessage errorMessage)
            where T : IParsable, new()
        {
            errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "");
            if (!CheckTimeSpan(beginDate, endDate, timeSpan))
            {
                errorMessage.errmsg = "起止日期不正确或者超过范围。";
                return(null);
            }
            string json         = JsonConvert.SerializeObject(new { begin_date = beginDate.ToString("yyyy-MM-dd"), end_date = endDate.ToString("yyyy-MM-dd") });
            string responseData = HttpHelper.RequestResponseContent(url, userName, null, httpMethod, json);

            if (string.IsNullOrWhiteSpace(responseData))
            {
                errorMessage.errmsg = "请求失败。";
                return(null);
            }
            else if (ErrorMessage.IsErrorMessage(responseData))
            {
                errorMessage = ErrorMessage.Parse(responseData);
                return(null);
            }
            else
            {
                JObject jo = JObject.Parse(responseData);
                JToken  jt;
                if (jo.TryGetValue("list", out jt) && jt.Type == JTokenType.Array)
                {
                    errorMessage = new ErrorMessage(ErrorMessage.SuccessCode, "请求成功。");
                    JArray ja     = (JArray)jt;
                    T[]    result = new T[ja.Count];
                    for (int i = 0; i < ja.Count; i++)
                    {
                        T item = new T();
                        item.Parse((JObject)ja[i]);
                        result[i] = item;
                    }
                    return(result);
                }
                else
                {
                    errorMessage.errmsg = "解析结果失败。";
                    return(null);
                }
            }
        }
        /// <summary>
        /// 获取用户基本信息
        /// </summary>
        /// <param name="accessToken">网页access token</param>
        /// <param name="openId">用户id</param>
        /// <param name="language">语言</param>
        /// <param name="errorMessage">返回获取是否成功</param>
        /// <returns>返回用户基本信息;如果获取失败,返回null。</returns>
        private static UserInfo GetUserInfo(string accessToken, string openId, string language, out ErrorMessage errorMessage)
        {
            UserInfo info = null;

            if (string.IsNullOrWhiteSpace(accessToken))
            {
                errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "网页access token不能为空。");
                return(info);
            }
            if (string.IsNullOrWhiteSpace(openId))
            {
                errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "用户id不能为空。");
                return(info);
            }
            string url = string.Format(urlForGettingUserInfo, accessToken, openId, language);
            string responseContent;

            if (!HttpHelper.Request(url, out responseContent, httpMethod, (string)null))
            {
                errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "请求失败。");
            }
            else if (ErrorMessage.IsErrorMessage(responseContent))
            {
                errorMessage = ErrorMessage.Parse(responseContent);
            }
            else
            {
                JObject jo = JObject.Parse(responseContent);
                JToken  jt;
                if (jo.TryGetValue("openid", out jt) && (string)jt == openId)
                {
                    string   nickname   = jo.TryGetValue("nickname", out jt) ? (string)jt : string.Empty;
                    int      sex        = jo.TryGetValue("sex", out jt) ? (int)jt : (int)SexEnum.Unknown;
                    string   lang       = jo.TryGetValue("language", out jt) ? (string)jt : string.Empty;
                    string   city       = jo.TryGetValue("city", out jt) ? (string)jt : string.Empty;
                    string   province   = jo.TryGetValue("province", out jt) ? (string)jt : string.Empty;
                    string   country    = jo.TryGetValue("country", out jt) ? (string)jt : string.Empty;
                    string   headimgurl = jo.TryGetValue("headimgurl", out jt) ? (string)jt : string.Empty;
                    string[] privilege  = null;
                    if (jo.TryGetValue("privilege", out jt))
                    {
                        if (jt.Type == JTokenType.Array)
                        {
                            JArray ja = (JArray)jt;
                            privilege = new string[ja.Count];
                            int idx = 0;
                            foreach (JValue jv in ja)
                            {
                                privilege[idx] = (string)jv.Value;
                                idx++;
                            }
                        }
                        else if (jt.Type == JTokenType.String)
                        {
                            privilege = new string[] { (string)jt }
                        }
                        ;
                    }
                    string unionid = jo.TryGetValue("unionid", out jt) ? (string)jt : string.Empty;
                    info         = new UserInfo(openId, nickname, sex, lang, city, province, country, headimgurl, privilege, unionid);
                    errorMessage = new ErrorMessage(ErrorMessage.SuccessCode, "请求成功。");
                }
                else
                {
                    errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "获取用户基本信息失败。");
                }
            }
            return(info);
        }
 /// <summary>
 /// 获取用户基本信息
 /// </summary>
 /// <param name="accessToken">网页access token</param>
 /// <param name="openId">用户id</param>
 /// <param name="errorMessage">返回获取是否成功</param>
 /// <returns>返回用户基本信息;如果获取失败,返回null。</returns>
 public static UserInfo GetUserInfo(string accessToken, string openId, out ErrorMessage errorMessage)
 {
     return(GetUserInfo(accessToken, openId, string.Empty, out errorMessage));
 }
 /// <summary>
 /// 获取用户基本信息
 /// </summary>
 /// <param name="accessToken">网页access token</param>
 /// <param name="openId">用户id</param>
 /// <param name="language">语言</param>
 /// <param name="errorMessage">返回获取是否成功</param>
 /// <returns>返回用户基本信息;如果获取失败,返回null。</returns>
 public static UserInfo GetUserInfo(string accessToken, string openId, LanguageEnum language, out ErrorMessage errorMessage)
 {
     return(GetUserInfo(accessToken, openId, language.ToString("g"), out errorMessage));
 }