/// <summary> /// 获取值类型的结果 /// </summary> /// <typeparam name="T">返回结果的类型</typeparam> /// <param name="responseContent">响应内容</param> /// <param name="propertyNameInJson">返回结果在json中的键名</param> /// <param name="errorMessage">返回请求是否成功</param> /// <returns>返回结果;如果请求失败,或者发生错误,返回default(T)。</returns> private static T ConvertValueTypeResult <T>(string responseContent, string propertyNameInJson, out ErrorMessage errorMessage) where T : struct { if (string.IsNullOrWhiteSpace(responseContent)) { errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "请求失败。"); return(default(T)); } if (ErrorMessage.IsErrorMessage(responseContent)) { errorMessage = ErrorMessage.Parse(responseContent); } else { errorMessage = new ErrorMessage(ErrorMessage.SuccessCode, "请求成功。"); } JObject jo = JObject.Parse(responseContent); JToken jt; if (jo.TryGetValue(propertyNameInJson, out jt)) { return(ConvertValueTypeResult <T>((string)jt)); } else { return(default(T)); } }
/// <summary> /// 获取自定义菜单配置 /// </summary> /// <param name="userName">公众号</param> /// <param name="isOpened">返回是否开启了菜单;如果获取失败,返回false。</param> /// <param name="errorMessage">返回获取是否成功</param> /// <returns>返回是菜单数组;如果获取失败,或者尚未开启菜单,返回null。</returns> public static BaseMenu[] GetSelfMenuInfo(string userName, out bool isOpened, out ErrorMessage errorMessage) { isOpened = false; BaseMenu[] menus = null; string responseContent = HttpHelper.RequestResponseContent(urlForGettingSelfMenuInfo, userName, null, httpMethodForGettingSelfMenuInfo, null); if (string.IsNullOrWhiteSpace(responseContent)) { errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "请求失败。"); } else if (ErrorMessage.IsErrorMessage(responseContent)) { errorMessage = ErrorMessage.Parse(responseContent); } else { errorMessage = new ErrorMessage(ErrorMessage.SuccessCode, "查询菜单成功"); JObject jo = JObject.Parse(responseContent); isOpened = (int)jo["is_menu_open"] == 1; if (isOpened) { menus = Parse(responseContent); } } return(menus); }
/// <summary> /// 校验网页access token的有效性 /// </summary> /// <param name="accessToken">网页许可令牌</param> /// <param name="openId">用户id</param> /// <returns>返回网页access token是否有效</returns> public static ErrorMessage CheckValidate(string accessToken, string openId) { if (string.IsNullOrWhiteSpace(accessToken)) { return(new ErrorMessage(ErrorMessage.ExceptionCode, "网页access token不能为空。")); } if (string.IsNullOrWhiteSpace(openId)) { return(new ErrorMessage(ErrorMessage.ExceptionCode, "用户id不能为空。")); } string url = string.Format(urlForCheckingValidate, accessToken, openId); string responseContent; if (!HttpHelper.Request(url, out responseContent, httpMethod, (string)null)) { return(new ErrorMessage(ErrorMessage.ExceptionCode, "请求失败。")); } else if (ErrorMessage.IsErrorMessage(responseContent)) { return(ErrorMessage.Parse(responseContent)); } else { return(new ErrorMessage(ErrorMessage.ExceptionCode, "解析结果失败。")); } }
/// <summary> /// 获取公众号中的在线客服接待信息列表 /// </summary> /// <param name="userName">公众号</param> /// <param name="errorMessage">返回获取是否成功</param> /// <returns>返回在线客服接待信息列表;如果获取失败,返回null。</returns> public static List <CustomerServiceOnlineInfo> GetOnlineKfList(string userName, out ErrorMessage errorMessage) { List <CustomerServiceOnlineInfo> infos = null; string responseContent = HttpHelper.RequestResponseContent(urlForGettingOnlineKfList, userName, null, httpMethodForGettingOnlineKfList, null); if (string.IsNullOrWhiteSpace(responseContent)) { errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "请求失败。"); } else if (ErrorMessage.IsErrorMessage(responseContent)) { errorMessage = ErrorMessage.Parse(responseContent); } else { var kf_online_list = new { kf_online_list = new List <CustomerServiceOnlineInfo>() }; var kfOnlineList = JsonConvert.DeserializeAnonymousType(responseContent, kf_online_list); infos = kfOnlineList.kf_online_list; errorMessage = new ErrorMessage(ErrorMessage.SuccessCode, "获取在线客服接待信息成功。"); } return(infos); }
/// <summary> /// 获取公众号中的客服聊天记录列表 /// </summary> /// <param name="userName">公众号</param> /// <param name="openId">普通用户的标识</param> /// <param name="startTime">查询开始时间</param> /// <param name="endTime">查询结束时间</param> /// <param name="pageSize">每页大小,每页最多拉取1000条</param> /// <param name="pageIndex">查询第几页,从1开始</param> /// <param name="errorMessage">返回获取是否成功</param> /// <returns>返回在线客服接待信息列表;如果获取失败,返回null。</returns> public static List <CustomerServiceRecord> GetRecord(string userName, string openId, DateTime startTime, DateTime endTime, int pageSize, int pageIndex, out ErrorMessage errorMessage) { List <CustomerServiceRecord> records = null; errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, ""); if (startTime > endTime) { errorMessage.errmsg = "查询开始时间不能大于查询结束时间。"; return(null); } if (pageSize <= 0 || pageSize > 1000) { errorMessage.errmsg = "每页大小错误。"; return(null); } if (pageIndex < 1) { errorMessage.errmsg = "查询页码错误。"; return(null); } var postData = new { starttime = MyWay.Areas.WeiXin.Models.Utility.ToWeixinTime(startTime), endtime = MyWay.Areas.WeiXin.Models.Utility.ToWeixinTime(endTime), openid = openId ?? string.Empty, pagesize = pageSize, pageindex = pageIndex }; string json = JsonConvert.SerializeObject(postData); string responseContent = HttpHelper.RequestResponseContent(urlForGettingRecord, userName, null, json); if (string.IsNullOrWhiteSpace(responseContent)) { errorMessage.errmsg = "从微信服务器获取响应失败。"; } else if (ErrorMessage.IsErrorMessage(responseContent)) { errorMessage = ErrorMessage.Parse(responseContent); } else { var recordlist = new { recordlist = new List <CustomerServiceRecord>() }; var recordList = JsonConvert.DeserializeAnonymousType(responseContent, recordlist); records = recordlist.recordlist; errorMessage = new ErrorMessage(ErrorMessage.SuccessCode, "请求成功。"); } return(records); }
/// <summary> /// 组合url,发送数据,然后返回响应的错误消息。 /// 注:错误消息不一定代表失败或者错误。 /// </summary> /// <param name="urlFormat">url格式字符串,第一个参数为userName获取到的许可令牌,然后依次为parameters中的参数</param> /// <param name="userName">公众号</param> /// <param name="urlParameters">参数</param> /// <param name="httpMethod">执行请求的http方法</param> /// <param name="data">请求的内容</param> /// <returns>返回响应的错误消息</returns> public static ErrorMessage RequestErrorMessage(string urlFormat, string userName, IEnumerable <object> urlParameters = null, string httpMethod = WebRequestMethods.Http.Get, string data = null) { string responseContent = RequestResponseContent(urlFormat, userName, urlParameters, httpMethod, data); if (string.IsNullOrWhiteSpace(responseContent)) { return(new ErrorMessage(ErrorMessage.ExceptionCode, "请求失败。")); } else if (ErrorMessage.IsErrorMessage(responseContent)) { return(ErrorMessage.Parse(responseContent)); } else { return(new ErrorMessage(ErrorMessage.ExceptionCode, "解析响应失败。")); } }
/// <summary> /// 查询菜单 /// </summary> /// <param name="userName">公众号</param> /// <param name="errorMessage">返回查询是否成功</param> /// <returns>返回菜单数组;如果获取失败,返回null。</returns> public static BaseMenu[] Get(string userName, out ErrorMessage errorMessage) { BaseMenu[] menus = null; string responseContent = HttpHelper.RequestResponseContent(urlForGetting, userName, null, httpMethodForGetting, null); if (string.IsNullOrWhiteSpace(responseContent)) { errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "请求失败。"); } else if (ErrorMessage.IsErrorMessage(responseContent)) { errorMessage = ErrorMessage.Parse(responseContent); } else { errorMessage = new ErrorMessage(ErrorMessage.SuccessCode, "查询菜单成功"); menus = Parse(responseContent); } return(menus); }
/// <summary> /// 获取客服的会话列表 /// </summary> /// <param name="userName">公众号</param> /// <param name="kfAccount">客服账号</param> /// <param name="errorMessage">返回获取是否成功</param> /// <returns>返回客服的会话列表</returns> public static CustomerServiceSession[] GetSessionList(string userName, string kfAccount, out ErrorMessage errorMessage) { string responseContent = HttpHelper.RequestResponseContent(urlForGettingSessionList, userName, new object[] { kfAccount }, httpMethodForGettingSessionList, null); if (string.IsNullOrWhiteSpace(responseContent)) { errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "请求失败。"); return(null); } else if (ErrorMessage.IsErrorMessage(responseContent)) { errorMessage = ErrorMessage.Parse(responseContent); return(null); } else { JObject jo = JObject.Parse(responseContent); JToken jt; if (jo.TryGetValue("sessionlist", out jt) && jt.Type == JTokenType.Array) { JArray ja = (JArray)jt; CustomerServiceSession[] sessions = new CustomerServiceSession[ja.Count]; for (int i = 0; i < ja.Count; i++) { sessions[i] = new CustomerServiceSession(); sessions[i].Parse((JObject)ja[i]); } errorMessage = new ErrorMessage(ErrorMessage.SuccessCode, "请求成功。"); return(sessions); } else { errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "解析结果失败。"); return(null); } } }
/// <summary> /// 组合url,发送数据,然后返回结果。 /// 注:结果为需要解析的类。 /// </summary> /// <typeparam name="T">返回结果的类型</typeparam> /// <param name="urlFormat">url格式字符串,第一个参数为userName获取到的许可令牌,然后依次为parameters中的参数</param> /// <param name="userName">公众号</param> /// <param name="errorMessage">返回请求是否成功</param> /// <param name="urlParameters">参数</param> /// <param name="httpMethod">执行请求的http方法</param> /// <param name="data">请求的内容</param> /// <returns>返回结果;如果请求失败,或者发生错误,返回null。</returns> public static T RequestParsableResult <T>(string urlFormat, string userName, out ErrorMessage errorMessage, IEnumerable <object> urlParameters = null, string httpMethod = WebRequestMethods.Http.Get, string data = null) where T : IParsable, new() { T result = default(T); errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "请求失败。"); string responseContent = RequestResponseContent(urlFormat, userName, urlParameters, httpMethod, data); if (string.IsNullOrWhiteSpace(responseContent)) { return(result); } if (ErrorMessage.IsErrorMessage(responseContent)) { errorMessage = ErrorMessage.Parse(responseContent); } else { try { result = MyWay.Areas.WeiXin.Models.Utility.Parse <T>(responseContent); if (result != null) { errorMessage = new ErrorMessage(ErrorMessage.SuccessCode, "请求成功。"); } else { errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "解析失败。"); } } catch { errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "解析失败。"); } } return(result); }
/// <summary> /// 从微信服务器获取access token /// </summary> /// <param name="userName">公众号</param> /// <param name="msg">从服务器返回的错误信息。</param> /// <returns>返回许可令牌;如果获取失败,返回null。</returns> private static AccessToken GetFromWeixinServer(string userName, out ErrorMessage msg) { AccessToken token = null; msg = new ErrorMessage(ErrorMessage.ExceptionCode, ""); AccountInfo account = AccountInfoCollection.GetAccountInfo(userName); if (account == null) { msg.errmsg = "获取公众号参数失败。"; return(token); } string url = string.Format(urlForGettingAccessToken, account.AppId, account.AppSecret); string result; if (!HttpHelper.Request(url, out result, httpMethodForGettingAccessToken, string.Empty)) { msg.errmsg = "从微信服务器获取响应失败。"; return(token); } if (ErrorMessage.IsErrorMessage(result)) { msg = ErrorMessage.Parse(result); } else { try { token = AccessToken.ParseFromJson(result); } catch (Exception e) { msg = new ErrorMessage(e); } } return(token); }
/// <summary> /// 刷新access token /// </summary> /// <param name="userName">公众号</param> /// <param name="refreshToken">用户刷新token</param> /// <param name="errorMessage">返回获取是否成功</param> /// <returns>返回access token;如果获取失败,返回null。</returns> public static OAuthAccessToken Refresh(string userName, string refreshToken, out ErrorMessage errorMessage) { OAuthAccessToken token = null; if (string.IsNullOrWhiteSpace(refreshToken)) { errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "用户刷新token不能为空。"); return(token); } AccountInfo account = AccountInfoCollection.GetAccountInfo(userName); if (account == null) { errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "获取公众号信息失败。"); return(token); } string url = string.Format(urlForRefreshingAccessToken, account.AppId, refreshToken); 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 { var result = JsonConvert.DeserializeAnonymousType(responseContent, new { access_token = "", expires_in = 0, refresh_token = "", openid = "", scope = "" }); token = new OAuthAccessToken(result.access_token, result.expires_in, result.refresh_token, result.openid, result.scope); errorMessage = new ErrorMessage(ErrorMessage.SuccessCode, "获取access token成功。"); } return(token); }
/// <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); }